[llvm] [X86] Fix potential CSE issues in commuteSelect multi-use path (PR #214092)

Timur Golubovich via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 06:14:52 PDT 2026


https://github.com/timurgol007 updated https://github.com/llvm/llvm-project/pull/214092

>From 650e30770b24be4a383137d8914c6fe39170b9c3 Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Wed, 5 Aug 2026 00:13:12 +0200
Subject: [PATCH 1/3] [X86] Fix potential CSE issues in commuteSelect multi-use
 path

Snapshot the validated user list before ReplaceAllUsesOfValueWith.
If getSetCC returns a CSE'd node that already has users, iterating
NewCond->users() would visit pre-existing unvalidated users and
swap their operands incorrectly.

Also assert that UpdateNodeOperands does not CSE to an existing
node, which would silently leave the original node unmodified
with an inverted condition but unswapped operands.

Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>
---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 84a38c3f5167a..073f90850639f 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -48594,11 +48594,14 @@ static SDValue commuteSelect(SDNode *N, SelectionDAG &DAG, const SDLoc &DL,
     return DAG.getSelect(DL, LHS.getValueType(), NewCond, RHS, LHS);
 
   // Invert the setcc for all users and commute all vselects.
+  SmallVector<SDNode *> UsersToUpdate(Cond->users());
   DAG.ReplaceAllUsesOfValueWith(Cond, NewCond);
-  for (SDNode *User : NewCond->users()) {
+  for (SDNode *User : UsersToUpdate) {
     SDValue UserLHS = User->getOperand(1);
     SDValue UserRHS = User->getOperand(2);
-    DAG.UpdateNodeOperands(User, NewCond, UserRHS, UserLHS);
+    [[maybe_unused]] SDNode *Updated =
+        DAG.UpdateNodeOperands(User, NewCond, UserRHS, UserLHS);
+    assert(Updated == User && "Unexpected CSE in commuteSelect");
   }
   return SDValue(N, 0);
 }

>From 429384562d0fbde8fb1d7e305efc5547a0bd5976 Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Wed, 5 Aug 2026 14:03:10 +0200
Subject: [PATCH 2/3] removed dummy RAUW

---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 073f90850639f..a155020becbaa 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -48595,7 +48595,6 @@ static SDValue commuteSelect(SDNode *N, SelectionDAG &DAG, const SDLoc &DL,
 
   // Invert the setcc for all users and commute all vselects.
   SmallVector<SDNode *> UsersToUpdate(Cond->users());
-  DAG.ReplaceAllUsesOfValueWith(Cond, NewCond);
   for (SDNode *User : UsersToUpdate) {
     SDValue UserLHS = User->getOperand(1);
     SDValue UserRHS = User->getOperand(2);

>From 1b875887b72e2259836f352b956d16d76642e412 Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Wed, 5 Aug 2026 15:14:37 +0200
Subject: [PATCH 3/3] added make_early_inc_range

---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a155020becbaa..439f7f5038c5a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -48594,8 +48594,7 @@ static SDValue commuteSelect(SDNode *N, SelectionDAG &DAG, const SDLoc &DL,
     return DAG.getSelect(DL, LHS.getValueType(), NewCond, RHS, LHS);
 
   // Invert the setcc for all users and commute all vselects.
-  SmallVector<SDNode *> UsersToUpdate(Cond->users());
-  for (SDNode *User : UsersToUpdate) {
+  for (SDNode *User : llvm::make_early_inc_range(Cond->users())) {
     SDValue UserLHS = User->getOperand(1);
     SDValue UserRHS = User->getOperand(2);
     [[maybe_unused]] SDNode *Updated =



More information about the llvm-commits mailing list