Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2()));
}

if (Cmp.isEquality() && Trunc->hasOneUse()) {
if (Cmp.isEquality() && (Trunc->hasOneUse() || Trunc->hasNoUnsignedWrap())) {
// Canonicalize to a mask and wider compare if the wide type is suitable:
// (trunc X to i8) == C --> (X & 0xff) == (zext C)
if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-trunc.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
; RUN: opt < %s -passes=instcombine -S -data-layout="n8" | FileCheck %s --check-prefixes=CHECK,DL8

declare void @use(i8)
declare void @use2(i4)

define i1 @ult_2(i32 %x) {
; CHECK-LABEL: @ult_2(
Expand Down Expand Up @@ -785,3 +786,17 @@ define <2 x i1> @uge_nsw_non_splat(<2 x i32> %x) {
ret <2 x i1> %r
}

define i1 @trunc_icmp(i8 %a0) {
; CHECK-LABEL: @trunc_icmp(
; CHECK-NEXT: [[TZ:%.*]] = tail call range(i8 0, 9) i8 @llvm.cttz.i8(i8 [[A0:%.*]], i1 false)
; CHECK-NEXT: [[TR:%.*]] = trunc nuw i8 [[TZ]] to i4
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[A0]], 0
; CHECK-NEXT: call void @use2(i4 [[TR]])
; CHECK-NEXT: ret i1 [[C]]
;
%tz = tail call range(i8 0, 9) i8 @llvm.cttz.i8(i8 %a0, i1 false)
%tr = trunc i8 %tz to i4
%c = icmp eq i4 %tr, 8
call void @use2(i4 %tr)
ret i1 %c
}