[llvm] r348072 - [InstCombine] Support ssub.sat canonicalization for non-splats

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 1 02:58:35 PST 2018


Author: nikic
Date: Sat Dec  1 02:58:34 2018
New Revision: 348072

URL: http://llvm.org/viewvc/llvm-project?rev=348072&view=rev
Log:
[InstCombine] Support ssub.sat canonicalization for non-splats

Extend ssub.sat(X, C) -> sadd.sat(X, -C) canonicalization to also
support non-splat vector constants. This is done by generalizing
the implementation of the isNotMinSignedValue() helper to return
true for constants that are non-splat, but don't contain any
signed min elements.

Differential Revision: https://reviews.llvm.org/D55011

Modified:
    llvm/trunk/lib/IR/Constants.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll

Modified: llvm/trunk/lib/IR/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Constants.cpp?rev=348072&r1=348071&r2=348072&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Constants.cpp (original)
+++ llvm/trunk/lib/IR/Constants.cpp Sat Dec  1 02:58:34 2018
@@ -184,18 +184,15 @@ bool Constant::isNotMinSignedValue() con
   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
     return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
 
-  // Check for constant vectors which are splats of INT_MIN values.
-  if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
-    if (Constant *Splat = CV->getSplatValue())
-      return Splat->isNotMinSignedValue();
-
-  // Check for constant vectors which are splats of INT_MIN values.
-  if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
-    if (CV->isSplat()) {
-      if (CV->getElementType()->isFloatingPointTy())
-        return !CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
-      return !CV->getElementAsAPInt(0).isMinSignedValue();
+  // Check that vectors don't contain INT_MIN
+  if (this->getType()->isVectorTy()) {
+    unsigned NumElts = this->getType()->getVectorNumElements();
+    for (unsigned i = 0; i != NumElts; ++i) {
+      Constant *Elt = this->getAggregateElement(i);
+      if (!Elt || !Elt->isNotMinSignedValue())
+        return false;
     }
+    return true;
   }
 
   // It *may* contain INT_MIN, we can't tell.

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=348072&r1=348071&r2=348072&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Sat Dec  1 02:58:34 2018
@@ -2104,11 +2104,10 @@ Instruction *InstCombiner::visitCallInst
     }
 
     // ssub.sat(X, C) -> sadd.sat(X, -C) if C != MIN
-    // TODO: Support non-splat C.
-    const APInt *C;
-    if (IID == Intrinsic::ssub_sat && match(Arg1, m_APInt(C)) &&
-        !C->isMinSignedValue()) {
-      Value *NegVal = ConstantInt::get(II->getType(), -*C);
+    Constant *C;
+    if (IID == Intrinsic::ssub_sat && match(Arg1, m_Constant(C)) &&
+        C->isNotMinSignedValue()) {
+      Value *NegVal = ConstantExpr::getNeg(C);
       return replaceInstUsesWith(
           *II, Builder.CreateBinaryIntrinsic(
               Intrinsic::sadd_sat, Arg0, NegVal));

Modified: llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll?rev=348072&r1=348071&r2=348072&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll Sat Dec  1 02:58:34 2018
@@ -337,11 +337,10 @@ define <2 x i8> @test_vector_ssub_canoni
   ret <2 x i8> %r
 }
 
-; Canonicalization for non-splat constants is not supported yet.
 define <2 x i8> @test_vector_ssub_canonical_min_non_splat(<2 x i8> %a) {
 ; CHECK-LABEL: @test_vector_ssub_canonical_min_non_splat(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.ssub.sat.v2i8(<2 x i8> [[A:%.*]], <2 x i8> <i8 10, i8 -10>)
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i8> @llvm.sadd.sat.v2i8(<2 x i8> [[A:%.*]], <2 x i8> <i8 -10, i8 10>)
+; CHECK-NEXT:    ret <2 x i8> [[TMP1]]
 ;
   %r = call <2 x i8> @llvm.ssub.sat.v2i8(<2 x i8> %a, <2 x i8> <i8 10, i8 -10>)
   ret <2 x i8> %r
@@ -630,8 +629,8 @@ define i8 @test_scalar_ssub_neg_nneg(i8
 define <2 x i8> @test_vector_ssub_neg_nneg(<2 x i8> %a) {
 ; CHECK-LABEL: @test_vector_ssub_neg_nneg(
 ; CHECK-NEXT:    [[A_NEG:%.*]] = or <2 x i8> [[A:%.*]], <i8 -128, i8 -128>
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.ssub.sat.v2i8(<2 x i8> [[A_NEG]], <2 x i8> <i8 10, i8 20>)
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i8> @llvm.sadd.sat.v2i8(<2 x i8> [[A_NEG]], <2 x i8> <i8 -10, i8 -20>)
+; CHECK-NEXT:    ret <2 x i8> [[TMP1]]
 ;
   %a_neg = or <2 x i8> %a, <i8 -128, i8 -128>
   %r = call <2 x i8> @llvm.ssub.sat.v2i8(<2 x i8> %a_neg, <2 x i8> <i8 10, i8 20>)




More information about the llvm-commits mailing list