Skip to content

Commit 2bb11d1

Browse files
committed
[InstCombine] Pull vector reverse through fneg
This follows on from llvm#144933 (comment), and allows us to remove the reverse (fneg (reverse x)) combine. A separate patch will handle the case for fabs. I haven't checked if we perform this canonicalization for either unops or binops for vp.reverse
1 parent 003145d commit 2bb11d1

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,6 +3049,12 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
30493049
if (match(OneUse, m_Shuffle(m_Value(X), m_Poison(), m_Mask(Mask))))
30503050
return new ShuffleVectorInst(Builder.CreateFNegFMF(X, &I), Mask);
30513051

3052+
// fneg (reverse x) --> reverse (fneg x)
3053+
if (match(OneUse, m_VecReverse(m_Value(X)))) {
3054+
Value *Reverse = Builder.CreateVectorReverse(Builder.CreateFNegFMF(X, &I));
3055+
return replaceInstUsesWith(I, Reverse);
3056+
}
3057+
30523058
return nullptr;
30533059
}
30543060

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,30 +3549,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
35493549
}
35503550
break;
35513551
}
3552-
case Intrinsic::vector_reverse: {
3553-
Value *Vec = II->getArgOperand(0);
3554-
// Note: We canonicalize reverse after binops, so we don't need a
3555-
// corresponding binop case here. TODO: Consider canonicalizing
3556-
// reverse after fneg?
3557-
3558-
// rev(unop rev(X)) --> unop X
3559-
Value *X;
3560-
if (match(Vec, m_OneUse(m_UnOp(m_VecReverse(m_Value(X)))))) {
3561-
auto *OldUnOp = cast<UnaryOperator>(Vec);
3562-
auto *NewUnOp = UnaryOperator::CreateWithCopiedFlags(
3563-
OldUnOp->getOpcode(), X, OldUnOp, OldUnOp->getName(),
3564-
II->getIterator());
3565-
return replaceInstUsesWith(CI, NewUnOp);
3566-
}
3567-
break;
3568-
}
35693552
case Intrinsic::experimental_vp_reverse: {
35703553
Value *X;
35713554
Value *Vec = II->getArgOperand(0);
35723555
Value *Mask = II->getArgOperand(1);
35733556
if (!match(Mask, m_AllOnes()))
35743557
break;
35753558
Value *EVL = II->getArgOperand(2);
3559+
// TODO: Canonicalize experimental.vp.reverse after unop/binops?
35763560
// rev(unop rev(X)) --> unop X
35773561
if (match(Vec,
35783562
m_OneUse(m_UnOp(m_Intrinsic<Intrinsic::experimental_vp_reverse>(

llvm/test/Transforms/InstCombine/vector-reverse.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ define <vscale x 4 x i32> @binop_reverse_splat_LHS_1(<vscale x 4 x i32> %a, i32
161161

162162
define <vscale x 4 x float> @unop_reverse(<vscale x 4 x float> %a) {
163163
; CHECK-LABEL: @unop_reverse(
164-
; CHECK-NEXT: [[A_REV:%.*]] = tail call <vscale x 4 x float> @llvm.vector.reverse.nxv4f32(<vscale x 4 x float> [[A:%.*]])
165-
; CHECK-NEXT: [[NEG:%.*]] = fneg fast <vscale x 4 x float> [[A_REV]]
166-
; CHECK-NEXT: ret <vscale x 4 x float> [[NEG]]
164+
; CHECK-NEXT: [[NEG:%.*]] = fneg fast <vscale x 4 x float> [[A_REV:%.*]]
165+
; CHECK-NEXT: [[NEG1:%.*]] = call <vscale x 4 x float> @llvm.vector.reverse.nxv4f32(<vscale x 4 x float> [[NEG]])
166+
; CHECK-NEXT: ret <vscale x 4 x float> [[NEG1]]
167167
;
168168
%a.rev = tail call <vscale x 4 x float> @llvm.vector.reverse.nxv4f32(<vscale x 4 x float> %a)
169169
%neg = fneg fast <vscale x 4 x float> %a.rev

0 commit comments

Comments
 (0)