[llvm] [VectorCombine] Fold reduce(trunc(x)) -> trunc(reduce(x)) iff cost effective (PR #81852)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 15 08:56:59 PST 2024
================
@@ -1526,6 +1527,67 @@ bool VectorCombine::foldShuffleFromReductions(Instruction &I) {
return foldSelectShuffle(*Shuffle, true);
}
+/// Determine if its more efficient to fold:
+/// reduce(trunc(x)) -> trunc(reduce(x)).
+bool VectorCombine::foldTruncFromReductions(Instruction &I) {
+ auto *II = dyn_cast<IntrinsicInst>(&I);
+ if (!II)
+ return false;
+
+ unsigned ReductionOpc = 0;
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::vector_reduce_add:
+ ReductionOpc = Instruction::Add;
+ break;
+ case Intrinsic::vector_reduce_mul:
+ ReductionOpc = Instruction::Mul;
+ break;
+ case Intrinsic::vector_reduce_and:
+ ReductionOpc = Instruction::And;
+ break;
+ case Intrinsic::vector_reduce_or:
+ ReductionOpc = Instruction::Or;
+ break;
+ case Intrinsic::vector_reduce_xor:
+ ReductionOpc = Instruction::Xor;
+ break;
+ default:
+ return false;
+ }
+ Value *ReductionSrc = I.getOperand(0);
+
+ Value *TruncSrc;
+ if (!match(ReductionSrc, m_Trunc(m_OneUse(m_Value(TruncSrc)))))
+ return false;
+
+ auto *Trunc = cast<CastInst>(ReductionSrc);
+ auto *TruncTy = cast<VectorType>(TruncSrc->getType());
----------------
nikic wrote:
```suggestion
auto *TruncSrcTy = cast<VectorType>(TruncSrc->getType());
```
Or `SrcTy`. As is that sounds like the trunc result type.
https://github.com/llvm/llvm-project/pull/81852
More information about the llvm-commits
mailing list