[PATCH] D89628: Don't fold overflowing arithmetic ops on vectors

LemonBoy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 17 10:42:24 PDT 2020


LemonBoy created this revision.
LemonBoy added reviewers: RKSimon, lebedev.ri, majnemer.
LemonBoy added a project: LLVM.
Herald added subscribers: llvm-commits, hiraditya.
LemonBoy requested review of this revision.

Feeding vector values to `InstCombiner::OptimizeOverflowCheck` produces a scalar boolean flag if it proves the overflow check can be eliminated.
This causes `InstCombiner::CreateOverflowTuple` to crash as it correctly expects a vector of i1 values instead.

Skip the overflow check elision for the time being as the code produces incorrect results for vector types anyway.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89628

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/with_overflow.ll


Index: llvm/test/Transforms/InstCombine/with_overflow.ll
===================================================================
--- llvm/test/Transforms/InstCombine/with_overflow.ll
+++ llvm/test/Transforms/InstCombine/with_overflow.ll
@@ -597,3 +597,49 @@
   %a = call { i8, i1 } @llvm.smul.with.overflow.i8(i8 %y, i8 2)
   ret { i8, i1 } %a
 }
+
+; Vector ops between constants, should be folded (but atm they're not).
+; Make sure the overflow vector is not folded into a scalar value.
+
+declare { <4 x i32>, <4 x i1> } @llvm.sadd.with.overflow.v4i32(<4 x i32>, <4 x i32>)
+declare { <4 x i32>, <4 x i1> } @llvm.uadd.with.overflow.v4i32(<4 x i32>, <4 x i32>)
+declare { <4 x i32>, <4 x i1> } @llvm.ssub.with.overflow.v4i32(<4 x i32>, <4 x i32>)
+declare { <4 x i32>, <4 x i1> } @llvm.usub.with.overflow.v4i32(<4 x i32>, <4 x i32>)
+declare { <4 x i32>, <4 x i1> } @llvm.smul.with.overflow.v4i32(<4 x i32>, <4 x i32>)
+declare { <4 x i32>, <4 x i1> } @llvm.umul.with.overflow.v4i32(<4 x i32>, <4 x i32>)
+
+define <4 x i1> @sadd_const_vector() nounwind {
+  %x = call { <4 x i32>, <4 x i1> } @llvm.sadd.with.overflow.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 1, i32 6, i32 7, i32 8>)
+  %y = extractvalue { <4 x i32>, <4 x i1> } %x, 1
+  ret <4 x i1> %y
+}
+
+define <4 x i1> @uadd_const_vector() nounwind {
+  %x = call { <4 x i32>, <4 x i1> } @llvm.uadd.with.overflow.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 1, i32 6, i32 7, i32 8>)
+  %y = extractvalue { <4 x i32>, <4 x i1> } %x, 1
+  ret <4 x i1> %y
+}
+
+define <4 x i1> @ssub_const_vector() nounwind {
+  %x = call { <4 x i32>, <4 x i1> } @llvm.ssub.with.overflow.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 1, i32 6, i32 7, i32 8>)
+  %y = extractvalue { <4 x i32>, <4 x i1> } %x, 1
+  ret <4 x i1> %y
+}
+
+define <4 x i1> @usub_const_vector() nounwind {
+  %x = call { <4 x i32>, <4 x i1> } @llvm.usub.with.overflow.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 1, i32 6, i32 7, i32 8>)
+  %y = extractvalue { <4 x i32>, <4 x i1> } %x, 1
+  ret <4 x i1> %y
+}
+
+define <4 x i1> @smul_const_vector() nounwind {
+  %x = call { <4 x i32>, <4 x i1> } @llvm.smul.with.overflow.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 1, i32 6, i32 7, i32 8>)
+  %y = extractvalue { <4 x i32>, <4 x i1> } %x, 1
+  ret <4 x i1> %y
+}
+
+define <4 x i1> @umul_const_vector() nounwind {
+  %x = call { <4 x i32>, <4 x i1> } @llvm.umul.with.overflow.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 1, i32 6, i32 7, i32 8>)
+  %y = extractvalue { <4 x i32>, <4 x i1> } %x, 1
+  ret <4 x i1> %y
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -4585,6 +4585,10 @@
     return true;
   }
 
+  // FIXME: support for integer vectors.
+  if (LHS->getType()->isVectorTy())
+    return false;
+
   switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
     case OverflowResult::MayOverflow:
       return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89628.298837.patch
Type: text/x-patch
Size: 3163 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201017/d93b0b0d/attachment.bin>


More information about the llvm-commits mailing list