summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2018-06-25 21:39:41 +0000
committerSanjay Patel <spatel@rotateright.com>2018-06-25 21:39:41 +0000
commit6a96d90acd4ea15b18ab526966dcd4f1fe86754a (patch)
tree203cbd58f3471d6ef99645d5f21c0fcefb5e27e7
parent46f9b8c333d641f5d386e21d0ca3cc34b8b90e34 (diff)
downloadbcm5719-llvm-6a96d90acd4ea15b18ab526966dcd4f1fe86754a.tar.gz
bcm5719-llvm-6a96d90acd4ea15b18ab526966dcd4f1fe86754a.zip
[InstCombine] fold sdiv with sext bool divisor
llvm-svn: 335527
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp11
-rw-r--r--llvm/test/Transforms/InstCombine/div.ll6
2 files changed, 9 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index c2293f08309..326f499ed9f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1019,12 +1019,15 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
return Common;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ Value *X;
+ // sdiv Op0, -1 --> -Op0
+ // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
+ if (match(Op1, m_AllOnes()) ||
+ (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
+ return BinaryOperator::CreateNeg(Op0);
+
const APInt *Op1C;
if (match(Op1, m_APInt(Op1C))) {
- // sdiv X, -1 == -X
- if (Op1C->isAllOnesValue())
- return BinaryOperator::CreateNeg(Op0);
-
// sdiv exact X, C --> ashr exact X, log2(C)
if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) {
Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2());
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index 805eb466804..13f751d0d9c 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -48,8 +48,7 @@ define <2 x i64> @sdiv_by_minus1_vec_undef_elt(<2 x i64> %x) {
define i32 @sdiv_by_sext_minus1(i1 %x, i32 %y) {
; CHECK-LABEL: @sdiv_by_sext_minus1(
-; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[X:%.*]] to i32
-; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[Y:%.*]], [[SEXT]]
+; CHECK-NEXT: [[DIV:%.*]] = sub i32 0, [[Y:%.*]]
; CHECK-NEXT: ret i32 [[DIV]]
;
%sext = sext i1 %x to i32
@@ -59,8 +58,7 @@ define i32 @sdiv_by_sext_minus1(i1 %x, i32 %y) {
define <2 x i32> @sdiv_by_sext_minus1_vec(<2 x i1> %x, <2 x i32> %y) {
; CHECK-LABEL: @sdiv_by_sext_minus1_vec(
-; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i32>
-; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[Y:%.*]], [[SEXT]]
+; CHECK-NEXT: [[DIV:%.*]] = sub <2 x i32> zeroinitializer, [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[DIV]]
;
%sext = sext <2 x i1> %x to <2 x i32>
OpenPOWER on IntegriCloud