Skip to content

Commit 77fc700

Browse files
committed
Added intersectRewrite and unionValue apis in FastMath class.
1 parent f389247 commit 77fc700

File tree

3 files changed

+77
-67
lines changed

3 files changed

+77
-67
lines changed

llvm/include/llvm/IR/FMF.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ class FastMathFlags {
108108

109109
/// Print fast-math flags to \p O.
110110
void print(raw_ostream &O) const;
111+
112+
/// Intersect rewrite-based flags
113+
static inline FastMathFlags intersectRewrite(FastMathFlags LHS,
114+
FastMathFlags RHS) {
115+
const unsigned RewriteMask =
116+
AllowReassoc | AllowReciprocal | AllowContract | ApproxFunc;
117+
return FastMathFlags(RewriteMask & LHS.Flags & RHS.Flags);
118+
}
119+
120+
/// Union value flags
121+
static inline FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS) {
122+
const unsigned ValueMask = NoNaNs | NoInfs | NoSignedZeros;
123+
return FastMathFlags(ValueMask & (LHS.Flags | RHS.Flags));
124+
}
111125
};
112126

113127
inline FastMathFlags operator|(FastMathFlags LHS, FastMathFlags RHS) {

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,30 +3680,13 @@ static Value *foldSelectIntoAddConstant(SelectInst &SI,
36803680
Value *NewFAdd = Builder.CreateFAdd(NewSelect, C);
36813681
NewFAdd->takeName(FAdd);
36823682

3683-
// Propagate rewrite-based flags
3684-
auto SelectFMF = SI.getFastMathFlags();
3685-
auto FAddFMF = FAdd->getFastMathFlags();
3686-
FastMathFlags CommonFMF, NewFAddFMF, NewSelectFMF;
3687-
3688-
CommonFMF.setAllowReassoc(SelectFMF.allowReassoc() &&
3689-
FAddFMF.allowReassoc());
3690-
CommonFMF.setAllowReciprocal(SelectFMF.allowReciprocal() &&
3691-
FAddFMF.allowReciprocal());
3692-
CommonFMF.setAllowContract(SelectFMF.allowContract() &&
3693-
FAddFMF.allowContract());
3694-
CommonFMF.setApproxFunc(SelectFMF.approxFunc() && FAddFMF.approxFunc());
3695-
NewSelectFMF = NewFAddFMF = CommonFMF;
3696-
36973683
// Propagate FastMath flags
3698-
NewFAddFMF.setNoNaNs(FAddFMF.noNaNs());
3699-
NewFAddFMF.setNoInfs(FAddFMF.noInfs());
3700-
NewFAddFMF.setNoSignedZeros(FAddFMF.noSignedZeros());
3701-
cast<Instruction>(NewFAdd)->setFastMathFlags(NewFAddFMF);
3702-
3703-
NewSelectFMF.setNoNaNs(SelectFMF.noNaNs());
3704-
NewSelectFMF.setNoInfs(SelectFMF.noInfs());
3705-
NewSelectFMF.setNoSignedZeros(SelectFMF.noSignedZeros());
3706-
cast<Instruction>(NewSelect)->setFastMathFlags(NewSelectFMF);
3684+
FastMathFlags SelectFMF = SI.getFastMathFlags();
3685+
FastMathFlags FAddFMF = FAdd->getFastMathFlags();
3686+
FastMathFlags NewFMF = FastMathFlags::intersectRewrite(SelectFMF, FAddFMF) |
3687+
FastMathFlags::unionValue(SelectFMF, FAddFMF);
3688+
cast<Instruction>(NewFAdd)->setFastMathFlags(NewFMF);
3689+
cast<Instruction>(NewSelect)->setFastMathFlags(NewFMF);
37073690

37083691
return NewFAdd;
37093692
}

0 commit comments

Comments
 (0)