[llvm] [SLP]Fix perfect diamond match with extractelements in scalars (PR #132466)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 24 02:58:02 PDT 2025


================
@@ -5339,8 +5338,19 @@ static InstructionCost getScalarizationOverhead(const TargetTransformInfo &TTI,
     }
     return Cost;
   }
-  return TTI.getScalarizationOverhead(Ty, DemandedElts, Insert, Extract,
-                                      CostKind, VL);
+  APInt NewDemandedElts = DemandedElts;
+  InstructionCost Cost = 0;
+  if (!ForPoisonSrc && Insert) {
+    // Handle insert into non-poison vector.
+    unsigned LeftMostBit = NewDemandedElts.countr_zero();
+    NewDemandedElts.clearBit(LeftMostBit);
+    Cost += TTI.getVectorInstrCost(Instruction::InsertElement, Ty, CostKind,
+                                   LeftMostBit, Constant::getNullValue(Ty));
+  }
+  return Cost + (NewDemandedElts.isZero()
+                     ? 0
+                     : TTI.getScalarizationOverhead(Ty, NewDemandedElts, Insert,
+                                                    Extract, CostKind, VL));
----------------
RKSimon wrote:

Is this easier to read?
```
if (!NewDemandedElts.isZero())
  Cost += TTI.getScalarizationOverhead(Ty, NewDemandedElts, Insert, Extract, CostKind, VL);
return Cost;
```

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


More information about the llvm-commits mailing list