[PATCH] D125969: [InstCombine] Prevent the transform of the comparison for all undef elements

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 06:26:29 PDT 2022


Allen created this revision.
Allen added reviewers: david-arm, nikic, paulwalker-arm, RKSimon.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Allen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Some pass such as Loop Vectorization(-fvectorize) may generate undef elements
constant, which will ball out the combine optimization.
Note: Part undef elements don't prevent the transform of the comparison.


https://reviews.llvm.org/D125969

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/icmp-bc-vec-inseltpoison.ll


Index: llvm/test/Transforms/InstCombine/icmp-bc-vec-inseltpoison.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-bc-vec-inseltpoison.ll
+++ llvm/test/Transforms/InstCombine/icmp-bc-vec-inseltpoison.ll
@@ -125,3 +125,14 @@
   %cmp = icmp slt i27 %cast, 262657 ; 0x040201
   ret i1 %cmp
 }
+
+; Verify the case that all elements are under define
+define <4 x i1> @test_i32_ule_pattern(i32 %val) {
+; CHECK-LABEL: @test_i32_ule_pattern(
+; CHECK-NEXT:    ret <4 x i1> <i1 true, i1 true, i1 true, i1 true>
+;
+  %insvec = insertelement <4 x i32> poison, i32 %val, i32 0
+  %vec = shufflevector <4 x i32> %insvec, <4 x i32> poison, <4 x i32> zeroinitializer
+  %cond = icmp ule <4 x i32> %vec, <i32 undef, i32 poison, i32 poison, i32 poison>
+  ret <4 x i1> %cond
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5679,12 +5679,15 @@
     return llvm::None;
   }
 
+  // It may be all elements are under value.
+  if (C->containsUndefOrPoisonElement() && SafeReplacementConstant == nullptr)
+    return llvm::None;
+
   // It may not be safe to change a compare predicate in the presence of
   // undefined elements, so replace those elements with the first safe constant
   // that we found.
   // TODO: in case of poison, it is safe; let's replace undefs only.
   if (C->containsUndefOrPoisonElement()) {
-    assert(SafeReplacementConstant && "Replacement constant not set");
     C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125969.430651.patch
Type: text/x-patch
Size: 1720 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220519/af004adc/attachment.bin>


More information about the llvm-commits mailing list