Skip to content

Commit 4811b7d

Browse files
committed
fix redundant umax
1 parent 6786858 commit 4811b7d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,7 +2981,7 @@ static Value *simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS,
29812981
}
29822982

29832983
static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
2984-
Value *RHS, const InstrInfoQuery &IIQ) {
2984+
Value *RHS, const SimplifyQuery &Q) {
29852985
Type *ITy = getCompareTy(RHS); // The return type.
29862986

29872987
Value *X;
@@ -3007,7 +3007,7 @@ static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
30073007
return ConstantInt::getTrue(ITy);
30083008

30093009
ConstantRange LHS_CR =
3010-
computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
3010+
computeConstantRange(LHS, CmpInst::isSigned(Pred), Q.IIQ.UseInstrInfo);
30113011
if (!LHS_CR.isFullSet()) {
30123012
if (RHS_CR.contains(LHS_CR))
30133013
return ConstantInt::getTrue(ITy);
@@ -3018,13 +3018,16 @@ static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
30183018
// (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
30193019
// (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
30203020
const APInt *MulC;
3021-
if (IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3021+
if (Q.IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
30223022
((match(LHS, m_NUWMul(m_Value(), m_APIntAllowPoison(MulC))) &&
30233023
*MulC != 0 && C->urem(*MulC) != 0) ||
30243024
(match(LHS, m_NSWMul(m_Value(), m_APIntAllowPoison(MulC))) &&
30253025
*MulC != 0 && C->srem(*MulC) != 0)))
30263026
return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
30273027

3028+
if (ICmpInst::isGE(Pred) && C->isOne() && isKnownNonZero(LHS, Q))
3029+
return ConstantInt::getTrue(ITy);
3030+
30283031
return nullptr;
30293032
}
30303033

@@ -3776,7 +3779,7 @@ static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
37763779
if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
37773780
return V;
37783781

3779-
if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3782+
if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q))
37803783
return V;
37813784

37823785
// If both operands have range metadata, use the metadata

llvm/test/Transforms/InstCombine/ceil-shift.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,34 @@ define i1 @ceil_shift_not_add_or(i32 %arg0) {
275275
%res = icmp eq i32 %quot_and_rem, 0
276276
ret i1 %res
277277
}
278+
279+
define i32 @ceil_shift_should_infer_ge_zero(i32 %x) {
280+
; CHECK-LABEL: define i32 @ceil_shift_should_infer_ge_zero(
281+
; CHECK-SAME: i32 [[X:%.*]]) {
282+
; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i32 [[X]], 0
283+
; CHECK-NEXT: br i1 [[COND_NOT]], label %[[IF_ELSE:.*]], label %[[IF_THEN:.*]]
284+
; CHECK: [[IF_THEN]]:
285+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 20
286+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[X]], 1048575
287+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0
288+
; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32
289+
; CHECK-NEXT: [[TMP5:%.*]] = add nuw nsw i32 [[TMP1]], [[TMP4]]
290+
; CHECK-NEXT: ret i32 [[TMP5]]
291+
; CHECK: [[IF_ELSE]]:
292+
; CHECK-NEXT: ret i32 0
293+
;
294+
%cond = icmp ne i32 %x, 0
295+
br i1 %cond, label %if.then, label %if.else
296+
297+
if.then:
298+
%12 = lshr i32 %x, 20
299+
%13 = and i32 %x, 1048575
300+
%14 = icmp ne i32 %13, 0
301+
%15 = zext i1 %14 to i32
302+
%16 = add nuw nsw i32 %12, %15
303+
%max = call i32 @llvm.umax.i32(i32 %16, i32 1)
304+
ret i32 %max
305+
306+
if.else:
307+
ret i32 0
308+
}

0 commit comments

Comments
 (0)