[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
This commit is contained in:
Sanjay Patel 2018-07-04 17:44:04 +00:00
parent 0dd27042c6
commit 9c2e7ceb1a
2 changed files with 17 additions and 20 deletions

View File

@ -671,20 +671,6 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
if (Instruction *Result = commonCastTransforms(CI))
return Result;
// Test if the trunc is the user of a select which is part of a
// minimum or maximum operation. If so, don't do any more simplification.
// Even simplifying demanded bits can break the canonical form of a
// min/max.
Value *LHS, *RHS;
if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)))
if (matchSelectPattern(SI, LHS, RHS).Flavor != SPF_UNKNOWN)
return nullptr;
// See if we can simplify any instructions used by the input whose sole
// purpose is to compute bits we don't care about.
if (SimplifyDemandedInstructionBits(CI))
return &CI;
Value *Src = CI.getOperand(0);
Type *DestTy = CI.getType(), *SrcTy = Src->getType();
@ -706,6 +692,20 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
return replaceInstUsesWith(CI, Res);
}
// Test if the trunc is the user of a select which is part of a
// minimum or maximum operation. If so, don't do any more simplification.
// Even simplifying demanded bits can break the canonical form of a
// min/max.
Value *LHS, *RHS;
if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)))
if (matchSelectPattern(SI, LHS, RHS).Flavor != SPF_UNKNOWN)
return nullptr;
// See if we can simplify any instructions used by the input whose sole
// purpose is to compute bits we don't care about.
if (SimplifyDemandedInstructionBits(CI))
return &CI;
// Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
if (DestTy->getScalarSizeInBits() == 1) {
Constant *One = ConstantInt::get(SrcTy, 1);

View File

@ -6,12 +6,9 @@
define i16 @foo(i16 %x) {
; CHECK-LABEL: @foo(
; CHECK-NEXT: [[T1:%.*]] = and i16 [[X:%.*]], 255
; CHECK-NEXT: [[T2:%.*]] = zext i16 [[T1]] to i32
; CHECK-NEXT: [[T3:%.*]] = icmp ult i32 [[T2]], 255
; CHECK-NEXT: [[T4:%.*]] = select i1 [[T3]], i32 [[T2]], i32 255
; CHECK-NEXT: [[T5:%.*]] = trunc i32 [[T4]] to i16
; CHECK-NEXT: [[T6:%.*]] = and i16 [[T5]], 255
; CHECK-NEXT: ret i16 [[T6]]
; CHECK-NEXT: [[T3:%.*]] = icmp ult i16 [[T1]], 255
; CHECK-NEXT: [[T4:%.*]] = select i1 [[T3]], i16 [[T1]], i16 255
; CHECK-NEXT: ret i16 [[T4]]
;
%t1 = and i16 %x, 255
%t2 = zext i16 %t1 to i32