[llvm] [AMDGPU] Swap select operands to allow later v_cndmask shrinking into vop2 (PR #142354)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 9 05:28:59 PDT 2025


================
@@ -4727,7 +4732,33 @@ SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
   SDValue True = N->getOperand(1);
   SDValue False = N->getOperand(2);
 
-  if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
+  int ShouldSwap = 0;
+  for (auto it = Cond->use_begin(); it != Cond->use_end(); it++) {
+    auto User = it->getUser();
+
+    if (User->getOpcode() != ISD::SELECT) {
+      ShouldSwap = 0;
+      break;
+    }
+
+    auto Op1 = User->getOperand(1);
+    auto Op2 = User->getOperand(2);
+
+    // if the operand is defined by fneg or fabs it means the instruction
+    // will have source modifiers and therefore can't be shrinked to vop2
+    if (isFnegOrFabs(Op1) || isFnegOrFabs(Op2))
+      continue;
+
+    bool IsOp1Divergent = Op1->isDivergent();
+    bool IsOp2Divergent = Op2->isDivergent();
+
+    if (!IsOp1Divergent && IsOp2Divergent)
+      ShouldSwap++;
+    else if (IsOp1Divergent && !IsOp2Divergent)
+      ShouldSwap--;
----------------
jayfoad wrote:

This simplifies to:
```suggestion
    ShouldSwap += Op2->isDivergent() - Op1->isDivergent();
```

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


More information about the llvm-commits mailing list