[InstCombine] use m_APInt to allow icmp (mul X, Y), C folds for splat constant vectors
authorSanjay Patel <spatel@rotateright.com>
Thu, 18 Aug 2016 15:44:44 +0000 (15:44 +0000)
committerSanjay Patel <spatel@rotateright.com>
Thu, 18 Aug 2016 15:44:44 +0000 (15:44 +0000)
This is a sibling of:
https://reviews.llvm.org/rL278859
https://reviews.llvm.org/rL278935
https://reviews.llvm.org/rL278945
https://reviews.llvm.org/rL279066

llvm-svn: 279077

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp.ll

index 12a72c4..fa683e9 100644 (file)
@@ -1906,26 +1906,22 @@ Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, Instruction *Or,
   return nullptr;
 }
 
-Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &ICI, Instruction *LHSI,
-                                               const APInt *RHSV) {
-  // FIXME: This check restricts all folds under here to scalar types.
-  ConstantInt *RHS = dyn_cast<ConstantInt>(ICI.getOperand(1));
-  if (!RHS)
-    return nullptr;
-
-  ConstantInt *Val = dyn_cast<ConstantInt>(LHSI->getOperand(1));
-  if (!Val)
+/// Fold icmp (mul X, Y), C.
+Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp, Instruction *Mul,
+                                               const APInt *C) {
+  const APInt *MulC;
+  if (!match(Mul->getOperand(1), m_APInt(MulC)))
     return nullptr;
 
-  // If this is a signed comparison to 0 and the mul is sign preserving,
-  // use the mul LHS operand instead.
-  ICmpInst::Predicate pred = ICI.getPredicate();
-  if (isSignTest(pred, *RHSV) && !Val->isZero() &&
-      cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
-    return new ICmpInst(Val->isNegative() ?
-                        ICmpInst::getSwappedPredicate(pred) : pred,
-                        LHSI->getOperand(0),
-                        Constant::getNullValue(RHS->getType()));
+  // If this is a test of the sign bit and the multiply is sign-preserving with
+  // a constant operand, use the multiply LHS operand instead.
+  ICmpInst::Predicate Pred = Cmp.getPredicate();
+  if (isSignTest(Pred, *C) && cast<BinaryOperator>(Mul)->hasNoSignedWrap()) {
+    if (MulC->isNegative())
+      Pred = ICmpInst::getSwappedPredicate(Pred);
+    return new ICmpInst(Pred, Mul->getOperand(0),
+                        Constant::getNullValue(Mul->getType()));
+  }
 
   return nullptr;
 }
index 0dbd386..b1f5e50 100644 (file)
@@ -1305,11 +1305,9 @@ define i1 @icmp_mul_nsw_neg1(i32 %x) {
   ret i1 %cmp
 }
 
-; FIXME: Vectors should fold the same way.
 define <2 x i1> @icmp_mul_nsw_neg1_vec(<2 x i32> %x) {
 ; CHECK-LABEL: @icmp_mul_nsw_neg1_vec(
-; CHECK-NEXT:    [[MUL:%.*]] = mul nsw <2 x i32> %x, <i32 -12, i32 -12>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt <2 x i32> [[MUL]], zeroinitializer
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i32> %x, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %mul = mul nsw <2 x i32> %x, <i32 -12, i32 -12>