Skip to content

Commit 94fee13

Browse files
authored
[InstCombine] Simplify FMF propagation. NFC. (#121899)
This patch uses new FMF interfaces introduced by #121657 to simplify existing code with `andIRFlags` and `copyFastMathFlags`.
1 parent c10e826 commit 94fee13

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,12 +1683,9 @@ static Instruction *reassociateFCmps(BinaryOperator &BO,
16831683

16841684
// and (fcmp ord X, 0), (and (fcmp ord Y, 0), Z) --> and (fcmp ord X, Y), Z
16851685
// or (fcmp uno X, 0), (or (fcmp uno Y, 0), Z) --> or (fcmp uno X, Y), Z
1686-
Value *NewFCmp = Builder.CreateFCmp(NanPred, X, Y);
1687-
if (auto *NewFCmpInst = dyn_cast<FCmpInst>(NewFCmp)) {
1688-
// Intersect FMF from the 2 source fcmps.
1689-
NewFCmpInst->copyIRFlags(Op0);
1690-
NewFCmpInst->andIRFlags(BO10);
1691-
}
1686+
// Intersect FMF from the 2 source fcmps.
1687+
Value *NewFCmp =
1688+
Builder.CreateFCmpFMF(NanPred, X, Y, FMFSource::intersect(Op0, BO10));
16921689
return BinaryOperator::Create(Opcode, NewFCmp, BO11);
16931690
}
16941691

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,13 +2522,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
25222522
default:
25232523
llvm_unreachable("unexpected intrinsic ID");
25242524
}
2525-
Value *V = Builder.CreateBinaryIntrinsic(
2526-
IID, X, ConstantFP::get(Arg0->getType(), Res), II);
25272525
// TODO: Conservatively intersecting FMF. If Res == C2, the transform
25282526
// was a simplification (so Arg0 and its original flags could
25292527
// propagate?)
2530-
if (auto *CI = dyn_cast<CallInst>(V))
2531-
CI->andIRFlags(M);
2528+
Value *V = Builder.CreateBinaryIntrinsic(
2529+
IID, X, ConstantFP::get(Arg0->getType(), Res),
2530+
FMFSource::intersect(II, M));
25322531
return replaceInstUsesWith(*II, V);
25332532
}
25342533
}
@@ -2623,13 +2622,11 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
26232622
}
26242623
case Intrinsic::fmuladd: {
26252624
// Try to simplify the underlying FMul.
2626-
if (Value *V = simplifyFMulInst(II->getArgOperand(0), II->getArgOperand(1),
2627-
II->getFastMathFlags(),
2628-
SQ.getWithInstruction(II))) {
2629-
auto *FAdd = BinaryOperator::CreateFAdd(V, II->getArgOperand(2));
2630-
FAdd->copyFastMathFlags(II);
2631-
return FAdd;
2632-
}
2625+
if (Value *V =
2626+
simplifyFMulInst(II->getArgOperand(0), II->getArgOperand(1),
2627+
II->getFastMathFlags(), SQ.getWithInstruction(II)))
2628+
return BinaryOperator::CreateFAddFMF(V, II->getArgOperand(2),
2629+
II->getFastMathFlags());
26332630

26342631
[[fallthrough]];
26352632
}
@@ -2656,11 +2653,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
26562653
// Try to simplify the underlying FMul. We can only apply simplifications
26572654
// that do not require rounding.
26582655
if (Value *V = simplifyFMAFMul(Src0, Src1, II->getFastMathFlags(),
2659-
SQ.getWithInstruction(II))) {
2660-
auto *FAdd = BinaryOperator::CreateFAdd(V, Src2);
2661-
FAdd->copyFastMathFlags(II);
2662-
return FAdd;
2663-
}
2656+
SQ.getWithInstruction(II)))
2657+
return BinaryOperator::CreateFAddFMF(V, Src2, II->getFastMathFlags());
26642658

26652659
// fma x, y, 0 -> fmul x, y
26662660
// This is always valid for -0.0, but requires nsz for +0.0 as
@@ -2754,8 +2748,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
27542748
m_CopySign(m_Value(Magnitude), m_Value(Sign)))) {
27552749
// fabs (copysign x, y) -> (fabs x)
27562750
CallInst *AbsSign =
2757-
Builder.CreateCall(II->getCalledFunction(), {Magnitude});
2758-
AbsSign->copyFastMathFlags(II);
2751+
Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Magnitude, II);
27592752
return replaceInstUsesWith(*II, AbsSign);
27602753
}
27612754

@@ -2862,16 +2855,15 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
28622855
Value *NewLdexp = nullptr;
28632856
Value *Select = nullptr;
28642857
if (match(SelectRHS, m_ZeroInt())) {
2865-
NewLdexp = Builder.CreateLdexp(Src, SelectLHS);
2858+
NewLdexp = Builder.CreateLdexp(Src, SelectLHS, II);
28662859
Select = Builder.CreateSelect(SelectCond, NewLdexp, Src);
28672860
} else if (match(SelectLHS, m_ZeroInt())) {
2868-
NewLdexp = Builder.CreateLdexp(Src, SelectRHS);
2861+
NewLdexp = Builder.CreateLdexp(Src, SelectRHS, II);
28692862
Select = Builder.CreateSelect(SelectCond, Src, NewLdexp);
28702863
}
28712864

28722865
if (NewLdexp) {
28732866
Select->takeName(II);
2874-
cast<Instruction>(NewLdexp)->copyFastMathFlags(II);
28752867
return replaceInstUsesWith(*II, Select);
28762868
}
28772869
}

0 commit comments

Comments
 (0)