[PATCH] D48930: [InstCombine] Apply foldICmpUsingKnownBits to early bailout case

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 4 04:18:45 PDT 2018


mkazantsev created this revision.
mkazantsev added reviewers: spatel, lebedev.ri, reames.

In the patch https://reviews.llvm.org/rL336172, we've sunk`foldICmpUsingKnownBits` down the pipeline
because it is expensive and sometimes produces instruction patterns that cannot be
analyzed by other parts of optimizer. However the method from which it is invoked
also contains a early bailout (it looks like it is needed to work around some endless
loop corner cases), in which case we don't do any transforms, including `foldICmpUsingKnownBits`.

There is a case where it is profitable to do `foldICmpUsingKnownBits` before the
early bailout. In this patch, we add its invocation to this execution path to help
the attached test.


https://reviews.llvm.org/D48930

Files:
  lib/Transforms/InstCombine/InstCombineCompares.cpp
  test/Transforms/InstCombine/max_known_bits.ll


Index: test/Transforms/InstCombine/max_known_bits.ll
===================================================================
--- test/Transforms/InstCombine/max_known_bits.ll
+++ test/Transforms/InstCombine/max_known_bits.ll
@@ -1,17 +1,12 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -instcombine < %s | FileCheck %s
-; TODO: The entire thing should be folded to and i16 %0, 255.
+; The entire thing should be folded to and i16 %0, 255.
 
 define i16 @foo(i16 )  {
 ; CHECK-LABEL: @foo(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP1:%.*]] = and i16 [[TMP0:%.*]], 255
-; CHECK-NEXT:    [[TMP2:%.*]] = zext i16 [[TMP1]] to i32
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult i32 [[TMP2]], 255
-; CHECK-NEXT:    [[TMP4:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 255
-; CHECK-NEXT:    [[TMP5:%.*]] = trunc i32 [[TMP4]] to i16
-; CHECK-NEXT:    [[TMP6:%.*]] = and i16 [[TMP5]], 255
-; CHECK-NEXT:    ret i16 [[TMP6]]
+; CHECK-NEXT:    ret i16 [[TMP1]]
 ;
 entry:
   %1 = and i16 %0, 255
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -4498,8 +4498,14 @@
     if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
       Value *A, *B;
       SelectPatternResult SPR = matchSelectPattern(SI, A, B);
-      if (SPR.Flavor != SPF_UNKNOWN)
+      if (SPR.Flavor != SPF_UNKNOWN) {
+        // This may be expensive in compile-time, and transforms based on known
+        // bits can make further analysis more difficult, so we use it as the
+        // last resort if we cannot do anything better.
+        if (Instruction *Res = foldICmpUsingKnownBits(I))
+          return Res;
         return nullptr;
+      }
     }
 
   // Do this after checking for min/max to prevent infinite looping.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48930.154088.patch
Type: text/x-patch
Size: 1924 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180704/39a4eb31/attachment.bin>


More information about the llvm-commits mailing list