[InstCombine] Do not keep samesign when speculatively executing icmps… · llvm/llvm-project@9e02cc4 (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -5609,6 +5609,11 @@ Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
5609 5609 return false;
5610 5610 return std::nullopt;
5611 5611 };
5612 +// Remove samesign here since it is illegal to keep it when we speculatively
5613 +// execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
5614 +// cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
5615 +// -46, -32)`. `X` is allowed to be non-negative here.
5616 + Pred = static_castCmpInst::Predicate(Pred);
5612 5617 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5613 5618 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5614 5619 if (!CmpXZ.has_value() && !CmpYZ.has_value())
Original file line number Diff line number Diff line change
@@ -804,4 +804,28 @@ end:
804 804 ret void
805 805 }
806 806
807 +define i1 @pr126974(i8 %x) {
808 +; CHECK-LABEL: @pr126974(
809 +; CHECK-NEXT: entry:
810 +; CHECK-NEXT: [[COND:%.*]] = icmp sgt i8 [[X:%.*]], -2
811 +; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
812 +; CHECK: if.then:
813 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X]], -1
814 +; CHECK-NEXT: ret i1 [[CMP]]
815 +; CHECK: if.else:
816 +; CHECK-NEXT: ret i1 false
817 +;
818 +entry:
819 +%cond = icmp sgt i8 %x, -2
820 +br i1 %cond, label %if.then, label %if.else
821 +
822 +if.then:
823 +%umax = call i8 @llvm.umax.i8(i8 %x, i8 -46)
824 +%cmp = icmp samesign ult i8 %umax, -32
825 +ret i1 %cmp
826 +
827 +if.else:
828 +ret i1 false
829 +}
830 +
807 831 declare i32 @llvm.umax.i32(i32, i32)