Skip to content

Commit ee0882b

Browse files
committed
[SimplifyCFG] propagate fast-math-flags (FMF) from phi to select
This is another step towards having FMF apply only to FP values rather than those + fcmp. See PR38086 for one of the original discussions/motivations: https://bugs.llvm.org/show_bug.cgi?id=38086 And the test here is derived from PR39535: https://bugs.llvm.org/show_bug.cgi?id=39535 Currently, we lose FMF when converting any phi to select in SimplifyCFG. There are a small number of similar changes needed to correct within SimplifyCFG, so it should be quick to patch this pass up. FMF was extended to select and phi with: D61917 D67564 Differential Revision: https://reviews.llvm.org/D70208
1 parent 782392d commit ee0882b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,10 +1404,16 @@ static bool HoistThenElseCodeToIf(BranchInst *BI,
14041404
// These values do not agree. Insert a select instruction before NT
14051405
// that determines the right value.
14061406
SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1407-
if (!SI)
1407+
if (!SI) {
1408+
// Propagate fast-math-flags from phi node to its replacement select.
1409+
IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
1410+
if (isa<FPMathOperator>(PN))
1411+
Builder.setFastMathFlags(PN.getFastMathFlags());
1412+
14081413
SI = cast<SelectInst>(
14091414
Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
14101415
BB1V->getName() + "." + BB2V->getName(), BI));
1416+
}
14111417

14121418
// Make the PHI node use the select for all incoming values for BB1/BB2
14131419
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)

llvm/test/Transforms/SimplifyCFG/HoistCode.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ define float @PR39535min(float %x) {
1919
; CHECK-LABEL: @PR39535min(
2020
; CHECK-NEXT: entry:
2121
; CHECK-NEXT: [[TOBOOL:%.*]] = fcmp une float [[X:%.*]], 0.000000e+00
22-
; CHECK-NEXT: [[DOTX:%.*]] = select i1 [[TOBOOL]], float 0.000000e+00, float [[X]]
22+
; CHECK-NEXT: [[DOTX:%.*]] = select fast i1 [[TOBOOL]], float 0.000000e+00, float [[X]]
2323
; CHECK-NEXT: ret float [[DOTX]]
2424
;
2525
entry:

0 commit comments

Comments
 (0)