Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 91 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,9 @@ static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI,
return Result;
}

static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
InstCombiner::BuilderTy &Builder) {
if (!Cmp->hasOneUse())
return nullptr;
static Value *
canonicalizeSaturatedAddUnsigned(ICmpInst *Cmp, Value *TVal, Value *FVal,
InstCombiner::BuilderTy &Builder) {

// Match unsigned saturated add with constant.
Value *Cmp0 = Cmp->getOperand(0);
Expand Down Expand Up @@ -1130,6 +1129,94 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
return nullptr;
}

static Value *canonicalizeSaturatedAddSigned(ICmpInst *Cmp, Value *TVal,
Value *FVal,
InstCombiner::BuilderTy &Builder) {
// Match saturated add with constant.
Value *Cmp0 = Cmp->getOperand(0);
Value *Cmp1 = Cmp->getOperand(1);
ICmpInst::Predicate Pred = Cmp->getPredicate();
Value *X;
const APInt *C;

// Canonicalize INT_MAX to true value of the select.
if (match(FVal, m_MaxSignedValue())) {
std::swap(TVal, FVal);
Pred = CmpInst::getInversePredicate(Pred);
}

if (!match(TVal, m_MaxSignedValue()))
return nullptr;

// sge maximum signed value is canonicalized to eq maximum signed value and
// requires special handling (a == INT_MAX) ? INT_MAX : a + 1 -> sadd.sat(a,
// 1)
if (Pred == ICmpInst::ICMP_EQ) {
if (match(FVal, m_Add(m_Specific(Cmp0), m_One())) && Cmp1 == TVal) {
return Builder.CreateBinaryIntrinsic(
Intrinsic::sadd_sat, Cmp0, ConstantInt::get(Cmp0->getType(), 1));
}
return nullptr;
}

// (X > Y) ? INT_MAX : (X + C) --> sadd.sat(X, C)
// (X >= Y) ? INT_MAX : (X + C) --> sadd.sat(X, C)
// where Y is INT_MAX - C or INT_MAX - C - 1, and C > 0
if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) &&
match(FVal, m_Add(m_Specific(Cmp0), m_StrictlyPositive(C)))) {
APInt IntMax =
APInt::getSignedMaxValue(Cmp1->getType()->getScalarSizeInBits());

// For SGE, try to flip to SGT to normalize the comparison constant.
if (Pred == ICmpInst::ICMP_SGE) {
if (auto Flipped = getFlippedStrictnessPredicateAndConstant(
Pred, cast<Constant>(Cmp1))) {
Pred = Flipped->first;
Cmp1 = Flipped->second;
}
}

// Check the pattern: X > INT_MAX - C or X > INT_MAX - C - 1
if (Pred == ICmpInst::ICMP_SGT &&
(match(Cmp1, m_SpecificIntAllowPoison(IntMax - *C)) ||
match(Cmp1, m_SpecificIntAllowPoison(IntMax - *C - 1))))
return Builder.CreateBinaryIntrinsic(
Intrinsic::sadd_sat, Cmp0, ConstantInt::get(Cmp0->getType(), *C));
}

// Canonicalize predicate to less-than or less-or-equal-than.
if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
std::swap(Cmp0, Cmp1);
Pred = CmpInst::getSwappedPredicate(Pred);
}

if (Pred != ICmpInst::ICMP_SLT && Pred != ICmpInst::ICMP_SLE)
return nullptr;

if (match(Cmp0, m_NSWSub(m_MaxSignedValue(), m_Value(X))) &&
match(FVal, m_c_Add(m_Specific(X), m_Specific(Cmp1)))) {
// (INT_MAX - X s< Y) ? INT_MAX : (X + Y) --> sadd.sat(X, Y)
// (INT_MAX - X s< Y) ? INT_MAX : (Y + X) --> sadd.sat(X, Y)
return Builder.CreateBinaryIntrinsic(Intrinsic::sadd_sat, X, Cmp1);
}

return nullptr;
}

static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
InstCombiner::BuilderTy &Builder) {
if (!Cmp->hasOneUse())
return nullptr;

if (Value *V = canonicalizeSaturatedAddUnsigned(Cmp, TVal, FVal, Builder))
return V;

if (Value *V = canonicalizeSaturatedAddSigned(Cmp, TVal, FVal, Builder))
return V;

return nullptr;
}

/// Try to match patterns with select and subtract as absolute difference.
static Value *foldAbsDiff(ICmpInst *Cmp, Value *TVal, Value *FVal,
InstCombiner::BuilderTy &Builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ define i8 @udiv_slt_exact(i8 %x) {
define i8 @canonicalize_icmp_operands(i8 %x) {
; CHECK-LABEL: define i8 @canonicalize_icmp_operands(
; CHECK-SAME: i8 [[X:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.smin.i8(i8 [[X]], i8 119)
; CHECK-NEXT: [[S:%.*]] = add nsw i8 [[TMP1]], 8
; CHECK-NEXT: [[S:%.*]] = call i8 @llvm.sadd.sat.i8(i8 [[X]], i8 8)
; CHECK-NEXT: ret i8 [[S]]
;
%add = add nsw i8 %x, 8
Expand Down
Loading
Loading