Skip to content
Open
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
25 changes: 25 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7435,6 +7435,31 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
return replaceInstUsesWith(I, V);

// Try to fold pattern: u <= (u <= b), b is boolean-like.
// This fold handles cases where Clang produces code like:
// %cmp1 = icmp ule i32 %u, zext i1 %b to i32
// %zext = zext i1 %cmp1 to i32
// %cmp2 = icmp ule i32 %u, %zext
// fold the comparison into: u <= b
if (I.getPredicate() == ICmpInst::ICMP_ULE) {
if (auto *Z = dyn_cast<ZExtInst>(Op1)) {
if (Z->getSrcTy()->isIntegerTy(1)) {
if (auto *Inner = dyn_cast<ICmpInst>(Z->getOperand(0))) {
if (Inner->getPredicate() == ICmpInst::ICMP_ULE &&
Inner->getOperand(0) == Op0 && Inner->hasOneUse()) {
Value *Bnd = Inner->getOperand(1);
bool IsBoolExt = isa<ZExtInst>(Bnd) &&
cast<ZExtInst>(Bnd)->getSrcTy()->isIntegerTy(1);
if (match(Bnd, m_ZeroInt()) || match(Bnd, m_One()) || IsBoolExt) {
Value *NewCmp = Builder.CreateICmpULE(Op0, Bnd, I.getName());
return replaceInstUsesWith(I, NewCmp);
}
}
}
}
}
}

// Comparing -val or val with non-zero is the same as just comparing val
// ie, abs(val) != 0 -> val != 0
if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
Expand Down
35 changes: 35 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5400,3 +5400,38 @@ define i1 @icmp_samesign_logical_or(i32 %In) {
%V = select i1 %c1, i1 true, i1 %c2
ret i1 %V
}

;===----------------------------------------------------------------------===;
; Test folding of nested `icmp ule` when inner RHS is boolean-like
;===----------------------------------------------------------------------===;

define i32 @fold_nested_ule_bool(i32 %u, i1 %b) {
; CHECK-LABEL: @fold_nested_ule_bool(
; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[B:%.*]] to i32
; CHECK-NEXT: [[CMP:%.*]] = icmp ule i32 [[U:%.*]], [[CONV]]
; CHECK-NEXT: [[CONV1:%.*]] = zext i1 [[CMP]] to i32
; CHECK-NEXT: ret i32 [[CONV1]]
;
%conv = zext i1 %b to i32
%cmp = icmp ule i32 %u, %conv
%conv1 = zext i1 %cmp to i32
%cmp2 = icmp ule i32 %u, %conv1
%conv3 = zext i1 %cmp2 to i32
ret i32 %conv3
}

define i32 @no_fold_nested_ule(i32 %u, i32 %b) {
; CHECK-LABEL: @no_fold_nested_ule(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ule i32 [[U:%.*]], [[B:%.*]]
; CHECK-NEXT: [[Z1:%.*]] = zext i1 [[CMP1]] to i32
; CHECK-NEXT: [[CMP2:%.*]] = icmp ule i32 [[U]], [[Z1]]
; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP2]] to i32
; CHECK-NEXT: ret i32 [[CONV]]
;
%cmp1 = icmp ule i32 %u, %b
%z1 = zext i1 %cmp1 to i32
%cmp2 = icmp ule i32 %u, %z1
%conv = zext i1 %cmp2 to i32
ret i32 %conv
}

Loading