Skip to content

[InstCombine] Pull vector reverse through fneg #146349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,12 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
if (match(OneUse, m_Shuffle(m_Value(X), m_Poison(), m_Mask(Mask))))
return new ShuffleVectorInst(Builder.CreateFNegFMF(X, &I), Mask);

// fneg (reverse x) --> reverse (fneg x)
if (match(OneUse, m_VecReverse(m_Value(X)))) {
Value *Reverse = Builder.CreateVectorReverse(Builder.CreateFNegFMF(X, &I));
return replaceInstUsesWith(I, Reverse);
}

return nullptr;
}

Expand Down
18 changes: 1 addition & 17 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3549,30 +3549,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
break;
}
case Intrinsic::vector_reverse: {
Value *Vec = II->getArgOperand(0);
// Note: We canonicalize reverse after binops, so we don't need a
// corresponding binop case here. TODO: Consider canonicalizing
// reverse after fneg?

// rev(unop rev(X)) --> unop X
Value *X;
if (match(Vec, m_OneUse(m_UnOp(m_VecReverse(m_Value(X)))))) {
auto *OldUnOp = cast<UnaryOperator>(Vec);
auto *NewUnOp = UnaryOperator::CreateWithCopiedFlags(
OldUnOp->getOpcode(), X, OldUnOp, OldUnOp->getName(),
II->getIterator());
return replaceInstUsesWith(CI, NewUnOp);
}
break;
}
case Intrinsic::experimental_vp_reverse: {
Value *X;
Value *Vec = II->getArgOperand(0);
Value *Mask = II->getArgOperand(1);
if (!match(Mask, m_AllOnes()))
break;
Value *EVL = II->getArgOperand(2);
// TODO: Canonicalize experimental.vp.reverse after unop/binops?
// rev(unop rev(X)) --> unop X
if (match(Vec,
m_OneUse(m_UnOp(m_Intrinsic<Intrinsic::experimental_vp_reverse>(
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/InstCombine/vector-reverse.ll
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ define <vscale x 4 x i32> @binop_reverse_splat_LHS_1(<vscale x 4 x i32> %a, i32

define <vscale x 4 x float> @unop_reverse(<vscale x 4 x float> %a) {
; CHECK-LABEL: @unop_reverse(
; CHECK-NEXT: [[A_REV:%.*]] = tail call <vscale x 4 x float> @llvm.vector.reverse.nxv4f32(<vscale x 4 x float> [[A:%.*]])
; CHECK-NEXT: [[NEG:%.*]] = fneg fast <vscale x 4 x float> [[A_REV]]
; CHECK-NEXT: ret <vscale x 4 x float> [[NEG]]
; CHECK-NEXT: [[NEG:%.*]] = fneg fast <vscale x 4 x float> [[A_REV:%.*]]
; CHECK-NEXT: [[NEG1:%.*]] = call <vscale x 4 x float> @llvm.vector.reverse.nxv4f32(<vscale x 4 x float> [[NEG]])
; CHECK-NEXT: ret <vscale x 4 x float> [[NEG1]]
;
%a.rev = tail call <vscale x 4 x float> @llvm.vector.reverse.nxv4f32(<vscale x 4 x float> %a)
%neg = fneg fast <vscale x 4 x float> %a.rev
Expand Down
Loading