Skip to content

Commit 99422f8

Browse files
Zain Jaffalfhahn
authored andcommitted
[InstCombine] Optimize multiplication where both operands are negated
Handle the case where both operands are negated in matrix multiplication Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D133695
1 parent 56cc491 commit 99422f8

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,17 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
17571757

17581758
break;
17591759
}
1760+
case Intrinsic::matrix_multiply: {
1761+
// -A * -B -> A * B
1762+
Value *A, *B;
1763+
if (match(II->getArgOperand(0), m_FNeg(m_Value(A))) &&
1764+
match(II->getArgOperand(1), m_FNeg(m_Value(B)))) {
1765+
replaceOperand(*II, 0, A);
1766+
replaceOperand(*II, 1, B);
1767+
return II;
1768+
}
1769+
break;
1770+
}
17601771
case Intrinsic::fmuladd: {
17611772
// Canonicalize fast fmuladd to the separate fmul + fadd.
17621773
if (II->isFast()) {

llvm/test/Transforms/InstCombine/matrix-multiplication-negation.ll

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ define <15 x double> @test_negation_on_result(<3 x double> %a, <5 x double> %b)
114114
; both negations can be deleted
115115
define <2 x double> @test_with_two_operands_negated1(<6 x double> %a, <3 x double> %b){
116116
; CHECK-LABEL: @test_with_two_operands_negated1(
117-
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <6 x double> [[A:%.*]]
118-
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <3 x double> [[B:%.*]]
119-
; CHECK-NEXT: [[RES:%.*]] = tail call <2 x double> @llvm.matrix.multiply.v2f64.v6f64.v3f64(<6 x double> [[A_NEG]], <3 x double> [[B_NEG]], i32 2, i32 3, i32 1)
117+
; CHECK-NEXT: [[RES:%.*]] = tail call <2 x double> @llvm.matrix.multiply.v2f64.v6f64.v3f64(<6 x double> [[A:%.*]], <3 x double> [[B:%.*]], i32 2, i32 3, i32 1)
120118
; CHECK-NEXT: ret <2 x double> [[RES]]
121119
;
122120
%a.neg = fneg <6 x double> %a
@@ -128,9 +126,7 @@ define <2 x double> @test_with_two_operands_negated1(<6 x double> %a, <3 x doubl
128126
; both negations will be removed
129127
define <9 x double> @test_with_two_operands_negated2(<27 x double> %a, <3 x double> %b){
130128
; CHECK-LABEL: @test_with_two_operands_negated2(
131-
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <27 x double> [[A:%.*]]
132-
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <3 x double> [[B:%.*]]
133-
; CHECK-NEXT: [[RES:%.*]] = tail call <9 x double> @llvm.matrix.multiply.v9f64.v27f64.v3f64(<27 x double> [[A_NEG]], <3 x double> [[B_NEG]], i32 9, i32 3, i32 1)
129+
; CHECK-NEXT: [[RES:%.*]] = tail call <9 x double> @llvm.matrix.multiply.v9f64.v27f64.v3f64(<27 x double> [[A:%.*]], <3 x double> [[B:%.*]], i32 9, i32 3, i32 1)
134130
; CHECK-NEXT: ret <9 x double> [[RES]]
135131
;
136132
%a.neg = fneg <27 x double> %a
@@ -142,9 +138,7 @@ define <9 x double> @test_with_two_operands_negated2(<27 x double> %a, <3 x doub
142138
; both negations will be removed
143139
define <9 x double> @test_with_two_operands_negated_with_fastflags(<27 x double> %a, <3 x double> %b){
144140
; CHECK-LABEL: @test_with_two_operands_negated_with_fastflags(
145-
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <27 x double> [[A:%.*]]
146-
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <3 x double> [[B:%.*]]
147-
; CHECK-NEXT: [[RES:%.*]] = tail call fast <9 x double> @llvm.matrix.multiply.v9f64.v27f64.v3f64(<27 x double> [[A_NEG]], <3 x double> [[B_NEG]], i32 9, i32 3, i32 1)
141+
; CHECK-NEXT: [[RES:%.*]] = tail call fast <9 x double> @llvm.matrix.multiply.v9f64.v27f64.v3f64(<27 x double> [[A:%.*]], <3 x double> [[B:%.*]], i32 9, i32 3, i32 1)
148142
; CHECK-NEXT: ret <9 x double> [[RES]]
149143
;
150144
%a.neg = fneg <27 x double> %a
@@ -156,9 +150,7 @@ define <9 x double> @test_with_two_operands_negated_with_fastflags(<27 x double>
156150
; both negations should be removed
157151
define <9 x double> @test_with_two_operands_negated2_commute(<3 x double> %a, <27 x double> %b){
158152
; CHECK-LABEL: @test_with_two_operands_negated2_commute(
159-
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <3 x double> [[A:%.*]]
160-
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <27 x double> [[B:%.*]]
161-
; CHECK-NEXT: [[RES:%.*]] = call <9 x double> @llvm.matrix.multiply.v9f64.v3f64.v27f64(<3 x double> [[A_NEG]], <27 x double> [[B_NEG]], i32 1, i32 3, i32 9)
153+
; CHECK-NEXT: [[RES:%.*]] = call <9 x double> @llvm.matrix.multiply.v9f64.v3f64.v27f64(<3 x double> [[A:%.*]], <27 x double> [[B:%.*]], i32 1, i32 3, i32 9)
162154
; CHECK-NEXT: ret <9 x double> [[RES]]
163155
;
164156
%a.neg = fneg <3 x double> %a
@@ -169,9 +161,7 @@ define <9 x double> @test_with_two_operands_negated2_commute(<3 x double> %a, <2
169161

170162
define <4 x double> @matrix_multiply_two_operands_negated_with_same_size(<2 x double> %a, <2 x double> %b) {
171163
; CHECK-LABEL: @matrix_multiply_two_operands_negated_with_same_size(
172-
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <2 x double> [[A:%.*]]
173-
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <2 x double> [[B:%.*]]
174-
; CHECK-NEXT: [[RES:%.*]] = call <4 x double> @llvm.matrix.multiply.v4f64.v2f64.v2f64(<2 x double> [[A_NEG]], <2 x double> [[B_NEG]], i32 2, i32 1, i32 2)
164+
; CHECK-NEXT: [[RES:%.*]] = call <4 x double> @llvm.matrix.multiply.v4f64.v2f64.v2f64(<2 x double> [[A:%.*]], <2 x double> [[B:%.*]], i32 2, i32 1, i32 2)
175165
; CHECK-NEXT: ret <4 x double> [[RES]]
176166
;
177167
%a.neg = fneg <2 x double> %a
@@ -182,11 +172,9 @@ define <4 x double> @matrix_multiply_two_operands_negated_with_same_size(<2 x do
182172

183173
define <2 x double> @matrix_multiply_two_operands_with_multiple_uses(<6 x double> %a, <3 x double> %b) {
184174
; CHECK-LABEL: @matrix_multiply_two_operands_with_multiple_uses(
185-
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <6 x double> [[A:%.*]]
186-
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <3 x double> [[B:%.*]]
187-
; CHECK-NEXT: [[RES:%.*]] = tail call <2 x double> @llvm.matrix.multiply.v2f64.v6f64.v3f64(<6 x double> [[A_NEG]], <3 x double> [[B_NEG]], i32 2, i32 3, i32 1)
188-
; CHECK-NEXT: [[RES_2:%.*]] = shufflevector <6 x double> [[A_NEG]], <6 x double> undef, <2 x i32> <i32 0, i32 1>
189-
; CHECK-NEXT: [[RES_3:%.*]] = fadd <2 x double> [[RES_2]], [[RES]]
175+
; CHECK-NEXT: [[RES:%.*]] = tail call <2 x double> @llvm.matrix.multiply.v2f64.v6f64.v3f64(<6 x double> [[A:%.*]], <3 x double> [[B:%.*]], i32 2, i32 3, i32 1)
176+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <6 x double> [[A]], <6 x double> poison, <2 x i32> <i32 0, i32 1>
177+
; CHECK-NEXT: [[RES_3:%.*]] = fsub <2 x double> [[RES]], [[TMP1]]
190178
; CHECK-NEXT: ret <2 x double> [[RES_3]]
191179
;
192180
%a.neg = fneg <6 x double> %a
@@ -202,7 +190,7 @@ define <9 x double> @matrix_multiply_two_operands_with_multiple_uses2(<27 x doub
202190
; CHECK-LABEL: @matrix_multiply_two_operands_with_multiple_uses2(
203191
; CHECK-NEXT: [[A_NEG:%.*]] = fneg <27 x double> [[A:%.*]]
204192
; CHECK-NEXT: [[B_NEG:%.*]] = fneg <3 x double> [[B:%.*]]
205-
; CHECK-NEXT: [[RES:%.*]] = tail call <9 x double> @llvm.matrix.multiply.v9f64.v27f64.v3f64(<27 x double> [[A_NEG]], <3 x double> [[B_NEG]], i32 9, i32 3, i32 1)
193+
; CHECK-NEXT: [[RES:%.*]] = tail call <9 x double> @llvm.matrix.multiply.v9f64.v27f64.v3f64(<27 x double> [[A]], <3 x double> [[B]], i32 9, i32 3, i32 1)
206194
; CHECK-NEXT: store <27 x double> [[A_NEG]], ptr [[A_LOC:%.*]], align 256
207195
; CHECK-NEXT: store <3 x double> [[B_NEG]], ptr [[B_LOC:%.*]], align 32
208196
; CHECK-NEXT: ret <9 x double> [[RES]]

0 commit comments

Comments
 (0)