Skip to content

Commit 6f7370c

Browse files
authored
[InstCombine] Pull vector reverse through fneg (#146349)
This follows on from #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 425359a commit 6f7370c

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
@@ -3564,30 +3564,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
35643564
}
35653565
break;
35663566
}
3567-
case Intrinsic::vector_reverse: {
3568-
Value *Vec = II->getArgOperand(0);
3569-
// Note: We canonicalize reverse after binops, so we don't need a
3570-
// corresponding binop case here. TODO: Consider canonicalizing
3571-
// reverse after fneg?
3572-
3573-
// rev(unop rev(X)) --> unop X
3574-
Value *X;
3575-
if (match(Vec, m_OneUse(m_UnOp(m_VecReverse(m_Value(X)))))) {
3576-
auto *OldUnOp = cast<UnaryOperator>(Vec);
3577-
auto *NewUnOp = UnaryOperator::CreateWithCopiedFlags(
3578-
OldUnOp->getOpcode(), X, OldUnOp, OldUnOp->getName(),
3579-
II->getIterator());
3580-
return replaceInstUsesWith(CI, NewUnOp);
3581-
}
3582-
break;
3583-
}
35843567
case Intrinsic::experimental_vp_reverse: {
35853568
Value *X;
35863569
Value *Vec = II->getArgOperand(0);
35873570
Value *Mask = II->getArgOperand(1);
35883571
if (!match(Mask, m_AllOnes()))
35893572
break;
35903573
Value *EVL = II->getArgOperand(2);
3574+
// TODO: Canonicalize experimental.vp.reverse after unop/binops?
35913575
// rev(unop rev(X)) --> unop X
35923576
if (match(Vec,
35933577
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)