Skip to content

Commit

Permalink
[InstSimplify] fold min/max with limit constant
Browse files Browse the repository at this point in the history
This is already done within InstCombine:
https://alive2.llvm.org/ce/z/MiGE22

...but leaving it out of analysis makes it
harder to avoid infinite loops there.
  • Loading branch information
rotateright committed Aug 10, 2021
1 parent 188832f commit e260e10
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/ValueTracking.h
Expand Up @@ -744,6 +744,10 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6;
/// minimum/maximum flavor.
CmpInst::Predicate getInverseMinMaxPred(SelectPatternFlavor SPF);

/// Return the minimum or maximum constant value for the specified integer
/// min/max flavor and type.
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth);

/// Check if the values in \p VL are select instructions that can be converted
/// to a min or max (vector) intrinsic. Returns the intrinsic ID, if such a
/// conversion is possible, together with a bool indicating whether all select
Expand Down
16 changes: 16 additions & 0 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Expand Up @@ -4080,6 +4080,22 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
std::swap(TrueVal, FalseVal);
}

// Check for integer min/max with a limit constant:
// X > MIN_INT ? X : MIN_INT --> X
// X < MAX_INT ? X : MAX_INT --> X
if (TrueVal->getType()->isIntOrIntVectorTy()) {
Value *X, *Y;
SelectPatternFlavor SPF =
matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
X, Y).Flavor;
if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF),
X->getType()->getScalarSizeInBits());
if (match(Y, m_SpecificInt(LimitC)))
return X;
}
}

if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
Value *X;
const APInt *Y;
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -6253,6 +6253,16 @@ CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) {
return getMinMaxPred(getInverseMinMaxFlavor(SPF));
}

APInt llvm::getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth) {
switch (SPF) {
case SPF_SMAX: return APInt::getSignedMaxValue(BitWidth);
case SPF_SMIN: return APInt::getSignedMinValue(BitWidth);
case SPF_UMAX: return APInt::getMaxValue(BitWidth);
case SPF_UMIN: return APInt::getMinValue(BitWidth);
default: llvm_unreachable("Unexpected flavor");
}
}

std::pair<Intrinsic::ID, bool>
llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
// Check if VL contains select instructions that can be folded into a min/max
Expand Down
20 changes: 8 additions & 12 deletions llvm/test/Transforms/InstSimplify/maxmin.ll
Expand Up @@ -3,9 +3,7 @@

define i8 @smax_min_limit(i8 %x) {
; CHECK-LABEL: @smax_min_limit(
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -128
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 -128
; CHECK-NEXT: ret i8 [[SEL]]
; CHECK-NEXT: ret i8 [[X:%.*]]
;
%cmp = icmp sgt i8 %x, -128
%sel = select i1 %cmp, i8 %x, i8 -128
Expand All @@ -14,9 +12,7 @@ define i8 @smax_min_limit(i8 %x) {

define i8 @smin_max_limit(i8 %x) {
; CHECK-LABEL: @smin_max_limit(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 127
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 127
; CHECK-NEXT: ret i8 [[SEL]]
; CHECK-NEXT: ret i8 [[X:%.*]]
;
%cmp = icmp slt i8 %x, 127
%sel = select i1 %cmp, i8 %x, i8 127
Expand All @@ -25,9 +21,7 @@ define i8 @smin_max_limit(i8 %x) {

define <2 x i8> @umax_min_limit(<2 x i8> %x) {
; CHECK-LABEL: @umax_min_limit(
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i8> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[X]], <2 x i8> zeroinitializer
; CHECK-NEXT: ret <2 x i8> [[SEL]]
; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%cmp = icmp ugt <2 x i8> %x, zeroinitializer
%sel = select <2 x i1> %cmp, <2 x i8> %x, <2 x i8> zeroinitializer
Expand All @@ -36,15 +30,15 @@ define <2 x i8> @umax_min_limit(<2 x i8> %x) {

define i8 @umin_max_limit(i8 %x) {
; CHECK-LABEL: @umin_max_limit(
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[X:%.*]], -1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[X]], i8 -1
; CHECK-NEXT: ret i8 [[SEL]]
; CHECK-NEXT: ret i8 [[X:%.*]]
;
%cmp = icmp ult i8 %x, 255
%sel = select i1 %cmp, i8 %x, i8 255
ret i8 %sel
}

; negative test - wrong limit

define i8 @smax_not_min_limit(i8 %x) {
; CHECK-LABEL: @smax_not_min_limit(
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -127
Expand All @@ -56,6 +50,8 @@ define i8 @smax_not_min_limit(i8 %x) {
ret i8 %sel
}

; negative test - wrong limit

define i8 @smin_not_min_limit(i8 %x) {
; CHECK-LABEL: @smin_not_min_limit(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
Expand Down

0 comments on commit e260e10

Please sign in to comment.