diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1053,6 +1053,7 @@ /// with the parameters swapped. static Value *foldUnsignedUnderflowCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, + const SimplifyQuery &Q, InstCombiner::BuilderTy &Builder) { Value *Subtracted; ICmpInst::Predicate EqPred; @@ -1065,6 +1066,24 @@ return nullptr; ICmpInst::Predicate UnsignedPred; + + // Subtracted < Base && Subtracted != 0 --> Base > Offset iff Offset != 0 + // Subtracted >= Base || Subtracted == 0 --> Base <= Base iff Offset != 0 + if (match(UnsignedICmp, + m_c_ICmp(UnsignedPred, m_Specific(Subtracted), m_Specific(Base)))) { + if (UnsignedICmp->getOperand(0) != Subtracted) + UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); + + if (UnsignedPred == ICmpInst::ICMP_ULT && IsAnd && + EqPred == ICmpInst::ICMP_NE && + isKnownNonZero(Offset, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT)) + return Builder.CreateICmpUGT(Base, Offset); + if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd && + EqPred == ICmpInst::ICMP_EQ && + isKnownNonZero(Offset, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT)) + return Builder.CreateICmpULE(Base, Offset); + } + if (!match(UnsignedICmp, m_c_ICmp(UnsignedPred, m_Specific(Base), m_Specific(Offset))) || !ICmpInst::isUnsigned(UnsignedPred)) @@ -1146,9 +1165,11 @@ if (Value *V = foldIsPowerOf2(LHS, RHS, true /* JoinedByAnd */, Builder)) return V; - if (Value *X = foldUnsignedUnderflowCheck(LHS, RHS, /*IsAnd=*/true, Builder)) + if (Value *X = + foldUnsignedUnderflowCheck(LHS, RHS, /*IsAnd=*/true, SQ, Builder)) return X; - if (Value *X = foldUnsignedUnderflowCheck(RHS, LHS, /*IsAnd=*/true, Builder)) + if (Value *X = + foldUnsignedUnderflowCheck(RHS, LHS, /*IsAnd=*/true, SQ, Builder)) return X; // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). @@ -2253,9 +2274,11 @@ if (Value *V = foldIsPowerOf2(LHS, RHS, false /* JoinedByAnd */, Builder)) return V; - if (Value *X = foldUnsignedUnderflowCheck(LHS, RHS, /*IsAnd=*/false, Builder)) + if (Value *X = + foldUnsignedUnderflowCheck(LHS, RHS, /*IsAnd=*/false, SQ, Builder)) return X; - if (Value *X = foldUnsignedUnderflowCheck(RHS, LHS, /*IsAnd=*/false, Builder)) + if (Value *X = + foldUnsignedUnderflowCheck(RHS, LHS, /*IsAnd=*/false, SQ, Builder)) return X; // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). diff --git a/llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll b/llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll --- a/llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll +++ b/llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll @@ -434,8 +434,8 @@ ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i64 [[ADJUSTED]], 0 ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[OFFSET]], [[BASE]] +; CHECK-NEXT: ret i1 [[TMP1]] ; %offset = ptrtoint i64* %offsetptr to i64 @@ -457,8 +457,8 @@ ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i64 [[ADJUSTED]], 0 ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[OFFSET]], [[BASE]] +; CHECK-NEXT: ret i1 [[TMP1]] ; %offset = ptrtoint i64* %offsetptr to i64 @@ -481,8 +481,8 @@ ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i64 [[ADJUSTED]], 0 ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = or i1 [[NOT_NULL]], [[NO_UNDERFLOW]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i64 [[OFFSET]], [[BASE]] +; CHECK-NEXT: ret i1 [[TMP1]] ; %offset = ptrtoint i64* %offsetptr to i64 @@ -504,8 +504,8 @@ ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i64 [[ADJUSTED]], 0 ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = or i1 [[NOT_NULL]], [[NO_UNDERFLOW]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i64 [[OFFSET]], [[BASE]] +; CHECK-NEXT: ret i1 [[TMP1]] ; %offset = ptrtoint i64* %offsetptr to i64