[llvm] r336293 - [InstCombine] allow narrowing of min/max/abs

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 4 10:44:04 PDT 2018


Author: spatel
Date: Wed Jul  4 10:44:04 2018
New Revision: 336293

URL: http://llvm.org/viewvc/llvm-project?rev=336293&view=rev
Log:
[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.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/trunk/test/Transforms/InstCombine/max_known_bits.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=336293&r1=336292&r2=336293&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Wed Jul  4 10:44:04 2018
@@ -671,20 +671,6 @@ Instruction *InstCombiner::visitTrunc(Tr
   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(Tr
     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);

Modified: llvm/trunk/test/Transforms/InstCombine/max_known_bits.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/max_known_bits.ll?rev=336293&r1=336292&r2=336293&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/max_known_bits.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/max_known_bits.ll Wed Jul  4 10:44:04 2018
@@ -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




More information about the llvm-commits mailing list