Skip to content

Commit d80bdf7

Browse files
authored
[IRBuilder] Add a helper function to intersect FMFs from two instructions (#122059)
Address review comment in #121899 (comment)
1 parent b8337dc commit d80bdf7

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class FMFSource {
102102
FastMathFlags get(FastMathFlags Default) const {
103103
return FMF.value_or(Default);
104104
}
105+
/// Intersect the FMF from two instructions.
106+
static FMFSource intersect(Value *A, Value *B) {
107+
return FMFSource(cast<FPMathOperator>(A)->getFastMathFlags() &
108+
cast<FPMathOperator>(B)->getFastMathFlags());
109+
}
105110
};
106111

107112
/// Common base class shared among various IRBuilders.

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ static Value *getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS,
3939
/// This is the complement of getFCmpCode, which turns an opcode and two
4040
/// operands into either a FCmp instruction, or a true/false constant.
4141
static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
42-
InstCombiner::BuilderTy &Builder,
43-
FastMathFlags FMF) {
42+
InstCombiner::BuilderTy &Builder, FMFSource FMF) {
4443
FCmpInst::Predicate NewPred;
4544
if (Constant *TorF = getPredForFCmpCode(Code, LHS->getType(), NewPred))
4645
return TorF;
@@ -1406,8 +1405,7 @@ static Value *matchIsFiniteTest(InstCombiner::BuilderTy &Builder, FCmpInst *LHS,
14061405
return nullptr;
14071406

14081407
return Builder.CreateFCmpFMF(FCmpInst::getOrderedPredicate(PredR), RHS0, RHS1,
1409-
LHS->getFastMathFlags() &
1410-
RHS->getFastMathFlags());
1408+
FMFSource::intersect(LHS, RHS));
14111409
}
14121410

14131411
Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
@@ -1444,7 +1442,7 @@ Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
14441442
// Intersect the fast math flags.
14451443
// TODO: We can union the fast math flags unless this is a logical select.
14461444
return getFCmpValue(NewPred, LHS0, LHS1, Builder,
1447-
LHS->getFastMathFlags() & RHS->getFastMathFlags());
1445+
FMFSource::intersect(LHS, RHS));
14481446
}
14491447

14501448
// This transform is not valid for a logical select.
@@ -1461,8 +1459,8 @@ Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
14611459
// Ignore the constants because they are obviously not NANs:
14621460
// (fcmp ord x, 0.0) & (fcmp ord y, 0.0) -> (fcmp ord x, y)
14631461
// (fcmp uno x, 0.0) | (fcmp uno y, 0.0) -> (fcmp uno x, y)
1464-
return Builder.CreateFCmpFMF(
1465-
PredL, LHS0, RHS0, LHS->getFastMathFlags() & RHS->getFastMathFlags());
1462+
return Builder.CreateFCmpFMF(PredL, LHS0, RHS0,
1463+
FMFSource::intersect(LHS, RHS));
14661464
}
14671465
}
14681466

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,9 +2674,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
26742674
// copysign Mag, (copysign ?, X) --> copysign Mag, X
26752675
Value *X;
26762676
if (match(Sign, m_Intrinsic<Intrinsic::copysign>(m_Value(), m_Value(X)))) {
2677-
Value *CopySign = Builder.CreateCopySign(
2678-
Mag, X,
2679-
II->getFastMathFlags() & cast<Instruction>(Sign)->getFastMathFlags());
2677+
Value *CopySign =
2678+
Builder.CreateCopySign(Mag, X, FMFSource::intersect(II, Sign));
26802679
return replaceInstUsesWith(*II, CopySign);
26812680
}
26822681

0 commit comments

Comments
 (0)