Skip to content

Commit 9ff60ee

Browse files
Add support for FSub as well
1 parent 486bbe7 commit 9ff60ee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
20022002
if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I))
20032003
return FoldedFAdd;
20042004

2005-
// B = fadd A, 0
2005+
// B = fadd A, 0.0
20062006
// Z = Op B
20072007
// can be transformed into
20082008
// Z = Op A
@@ -3125,6 +3125,16 @@ Instruction *InstCombinerImpl::visitFSub(BinaryOperator &I) {
31253125
Value *X, *Y;
31263126
Constant *C;
31273127

3128+
// B = fsub A, 0.0
3129+
// Z = Op B
3130+
// can be transformed into
3131+
// Z = Op A
3132+
// Where Op is such that we can ignore sign of 0 in fsub
3133+
Value *A;
3134+
if (match(&I, m_OneUse(m_FSub(m_Value(A), m_AnyZeroFP()))) &&
3135+
canIgnoreSignBitOfZero(*I.use_begin()))
3136+
return replaceInstUsesWith(I, A);
3137+
31283138
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
31293139
// If Op0 is not -0.0 or we can ignore -0.0: Z - (X - Y) --> Z + (Y - X)
31303140
// Canonicalize to fadd to make analysis easier.

llvm/test/Transforms/InstCombine/fold-fadd-with-zero-gh154238.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ define float @src2(float %arg1) {
2424
%v4 = fsub float %v2, %v3
2525
ret float %v4
2626
}
27+
28+
define float @src_sub(float %arg1) {
29+
; CHECK-LABEL: define float @src_sub(
30+
; CHECK-SAME: float [[ARG1:%.*]]) {
31+
; CHECK-NEXT: [[V3:%.*]] = call float @llvm.fabs.f32(float [[ARG1]])
32+
; CHECK-NEXT: ret float [[V3]]
33+
;
34+
%v2 = fsub float %arg1, 0.000000e+00
35+
%v3 = call float @llvm.fabs.f32(float %v2)
36+
ret float %v3
37+
}

0 commit comments

Comments
 (0)