Skip to content

Commit a2ba438

Browse files
authored
[InstCombine] Preserve the flag from RHS only if the and is bitwise (#113164)
Fixes #113123 Alive proof: https://alive2.llvm.org/ce/z/hnqeLC
1 parent 67ff5ba commit a2ba438

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,10 @@ Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
15311531
}
15321532
if (IsLessThanOrLessEqual(IsAnd ? PredL : PredR)) {
15331533
BuilderTy::FastMathFlagGuard Guard(Builder);
1534-
Builder.setFastMathFlags(LHS->getFastMathFlags() |
1535-
RHS->getFastMathFlags());
1534+
FastMathFlags NewFlag = LHS->getFastMathFlags();
1535+
if (!IsLogicalSelect)
1536+
NewFlag |= RHS->getFastMathFlags();
1537+
Builder.setFastMathFlags(NewFlag);
15361538

15371539
Value *FAbs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, LHS0);
15381540
return Builder.CreateFCmp(PredL, FAbs,

llvm/test/Transforms/InstCombine/fcmp-range-check-idiom.ll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,42 @@ define i1 @test_and_olt_fmf_propagation_union(float %x) {
359359
%cond = and i1 %cmp1, %cmp2
360360
ret i1 %cond
361361
}
362+
363+
define i1 @test_and_olt_fmf_propagation_union_logical_rhs_poison(float %x) {
364+
; CHECK-LABEL: define i1 @test_and_olt_fmf_propagation_union_logical_rhs_poison(
365+
; CHECK-SAME: float [[X:%.*]]) {
366+
; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[X]])
367+
; CHECK-NEXT: [[COND:%.*]] = fcmp olt float [[TMP1]], 0x3C00000000000000
368+
; CHECK-NEXT: ret i1 [[COND]]
369+
;
370+
%cmp1 = fcmp ninf olt float %x, 0x3C00000000000000
371+
%cmp2 = fcmp ogt float %x, 0xBC00000000000000
372+
%cond = select i1 %cmp2, i1 %cmp1, i1 false
373+
ret i1 %cond
374+
}
375+
376+
define i1 @test_and_olt_fmf_propagation_union_logical_lhs_poison(float %x) {
377+
; CHECK-LABEL: define i1 @test_and_olt_fmf_propagation_union_logical_lhs_poison(
378+
; CHECK-SAME: float [[X:%.*]]) {
379+
; CHECK-NEXT: [[TMP1:%.*]] = call ninf float @llvm.fabs.f32(float [[X]])
380+
; CHECK-NEXT: [[COND:%.*]] = fcmp ninf olt float [[TMP1]], 0x3C00000000000000
381+
; CHECK-NEXT: ret i1 [[COND]]
382+
;
383+
%cmp1 = fcmp olt float %x, 0x3C00000000000000
384+
%cmp2 = fcmp ninf ogt float %x, 0xBC00000000000000
385+
%cond = select i1 %cmp2, i1 %cmp1, i1 false
386+
ret i1 %cond
387+
}
388+
389+
define i1 @test_and_olt_fmf_propagation_union_logical_both_poison(float %x) {
390+
; CHECK-LABEL: define i1 @test_and_olt_fmf_propagation_union_logical_both_poison(
391+
; CHECK-SAME: float [[X:%.*]]) {
392+
; CHECK-NEXT: [[TMP1:%.*]] = call ninf float @llvm.fabs.f32(float [[X]])
393+
; CHECK-NEXT: [[COND:%.*]] = fcmp ninf olt float [[TMP1]], 0x3C00000000000000
394+
; CHECK-NEXT: ret i1 [[COND]]
395+
;
396+
%cmp1 = fcmp ninf olt float %x, 0x3C00000000000000
397+
%cmp2 = fcmp ninf ogt float %x, 0xBC00000000000000
398+
%cond = select i1 %cmp2, i1 %cmp1, i1 false
399+
ret i1 %cond
400+
}

0 commit comments

Comments
 (0)