Skip to content

Commit 9c2e7ce

Browse files
committed
[InstCombine] allow narrowing of min/max/abs
We have bailout hacks based on min/max in various places in instcombine that shouldn't be necessary. The affected test was added for: D48930 ...which is a consequence of the improvement in: D48584 (https://reviews.llvm.org/rL336172) I'm assuming the visitTrunc bailout in this patch was added specifically to avoid a change from SimplifyDemandedBits, so I'm just moving that below the EvaluateInDifferentType optimization. A narrow min/max is still a min/max. llvm-svn: 336293
1 parent 0dd2704 commit 9c2e7ce

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -671,20 +671,6 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
671671
if (Instruction *Result = commonCastTransforms(CI))
672672
return Result;
673673

674-
// Test if the trunc is the user of a select which is part of a
675-
// minimum or maximum operation. If so, don't do any more simplification.
676-
// Even simplifying demanded bits can break the canonical form of a
677-
// min/max.
678-
Value *LHS, *RHS;
679-
if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)))
680-
if (matchSelectPattern(SI, LHS, RHS).Flavor != SPF_UNKNOWN)
681-
return nullptr;
682-
683-
// See if we can simplify any instructions used by the input whose sole
684-
// purpose is to compute bits we don't care about.
685-
if (SimplifyDemandedInstructionBits(CI))
686-
return &CI;
687-
688674
Value *Src = CI.getOperand(0);
689675
Type *DestTy = CI.getType(), *SrcTy = Src->getType();
690676

@@ -706,6 +692,20 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
706692
return replaceInstUsesWith(CI, Res);
707693
}
708694

695+
// Test if the trunc is the user of a select which is part of a
696+
// minimum or maximum operation. If so, don't do any more simplification.
697+
// Even simplifying demanded bits can break the canonical form of a
698+
// min/max.
699+
Value *LHS, *RHS;
700+
if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)))
701+
if (matchSelectPattern(SI, LHS, RHS).Flavor != SPF_UNKNOWN)
702+
return nullptr;
703+
704+
// See if we can simplify any instructions used by the input whose sole
705+
// purpose is to compute bits we don't care about.
706+
if (SimplifyDemandedInstructionBits(CI))
707+
return &CI;
708+
709709
// Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
710710
if (DestTy->getScalarSizeInBits() == 1) {
711711
Constant *One = ConstantInt::get(SrcTy, 1);

llvm/test/Transforms/InstCombine/max_known_bits.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
define i16 @foo(i16 %x) {
77
; CHECK-LABEL: @foo(
88
; CHECK-NEXT: [[T1:%.*]] = and i16 [[X:%.*]], 255
9-
; CHECK-NEXT: [[T2:%.*]] = zext i16 [[T1]] to i32
10-
; CHECK-NEXT: [[T3:%.*]] = icmp ult i32 [[T2]], 255
11-
; CHECK-NEXT: [[T4:%.*]] = select i1 [[T3]], i32 [[T2]], i32 255
12-
; CHECK-NEXT: [[T5:%.*]] = trunc i32 [[T4]] to i16
13-
; CHECK-NEXT: [[T6:%.*]] = and i16 [[T5]], 255
14-
; CHECK-NEXT: ret i16 [[T6]]
9+
; CHECK-NEXT: [[T3:%.*]] = icmp ult i16 [[T1]], 255
10+
; CHECK-NEXT: [[T4:%.*]] = select i1 [[T3]], i16 [[T1]], i16 255
11+
; CHECK-NEXT: ret i16 [[T4]]
1512
;
1613
%t1 = and i16 %x, 255
1714
%t2 = zext i16 %t1 to i32

0 commit comments

Comments
 (0)