[llvm] 278b407 - [InstCombine] fold mul-with-overflow intrinsic with -1 operand
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 28 11:13:54 PST 2022
Author: Sanjay Patel
Date: 2022-02-28T14:13:48-05:00
New Revision: 278b407a30857ef12b8599cc1754179a289e8680
URL: https://github.com/llvm/llvm-project/commit/278b407a30857ef12b8599cc1754179a289e8680
DIFF: https://github.com/llvm/llvm-project/commit/278b407a30857ef12b8599cc1754179a289e8680.diff
LOG: [InstCombine] fold mul-with-overflow intrinsic with -1 operand
extractvalue (any_mul_with_overflow X, -1), 0 --> -X
There are similar other potential transforms that we could do as
noted by the last TODO in the test diffs.
Fixes #54053
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/with_overflow.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 69564e085ebc..65ee892154b7 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3249,6 +3249,15 @@ Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
makeArrayRef(exti, exte));
}
if (WithOverflowInst *WO = dyn_cast<WithOverflowInst>(Agg)) {
+ // extractvalue (any_mul_with_overflow X, -1), 0 --> -X
+ Intrinsic::ID OvID = WO->getIntrinsicID();
+ if (*EV.idx_begin() == 0 &&
+ (OvID == Intrinsic::smul_with_overflow ||
+ OvID == Intrinsic::umul_with_overflow) &&
+ match(WO->getArgOperand(1), m_AllOnes())) {
+ return BinaryOperator::CreateNeg(WO->getArgOperand(0));
+ }
+
// We're extracting from an overflow intrinsic, see if we're the only user,
// which allows us to simplify multiple result intrinsics to simpler
// things that just get one value.
diff --git a/llvm/test/Transforms/InstCombine/with_overflow.ll b/llvm/test/Transforms/InstCombine/with_overflow.ll
index 6cafe2f06fb1..2c2bde36082b 100644
--- a/llvm/test/Transforms/InstCombine/with_overflow.ll
+++ b/llvm/test/Transforms/InstCombine/with_overflow.ll
@@ -767,9 +767,8 @@ define { <4 x i8>, <4 x i1> } @neutral_umul_const_vector() nounwind {
define i8 @smul_neg1(i8 %x, i1* %p) {
; CHECK-LABEL: @smul_neg1(
-; CHECK-NEXT: [[M:%.*]] = tail call { i8, i1 } @llvm.smul.with.overflow.i8(i8 [[X:%.*]], i8 -1)
-; CHECK-NEXT: [[R:%.*]] = extractvalue { i8, i1 } [[M]], 0
-; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[M]], 1
+; CHECK-NEXT: [[R:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT: [[OV:%.*]] = icmp eq i8 [[X]], -128
; CHECK-NEXT: store i1 [[OV]], i1* [[P:%.*]], align 1
; CHECK-NEXT: ret i8 [[R]]
;
@@ -782,9 +781,8 @@ define i8 @smul_neg1(i8 %x, i1* %p) {
define <4 x i8> @smul_neg1_vec(<4 x i8> %x, <4 x i1>* %p) {
; CHECK-LABEL: @smul_neg1_vec(
-; CHECK-NEXT: [[M:%.*]] = tail call { <4 x i8>, <4 x i1> } @llvm.smul.with.overflow.v4i8(<4 x i8> [[X:%.*]], <4 x i8> <i8 -1, i8 -1, i8 -1, i8 -1>)
-; CHECK-NEXT: [[R:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 0
-; CHECK-NEXT: [[OV:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 1
+; CHECK-NEXT: [[R:%.*]] = sub <4 x i8> zeroinitializer, [[X:%.*]]
+; CHECK-NEXT: [[OV:%.*]] = icmp eq <4 x i8> [[X]], <i8 -128, i8 -128, i8 -128, i8 -128>
; CHECK-NEXT: store <4 x i1> [[OV]], <4 x i1>* [[P:%.*]], align 1
; CHECK-NEXT: ret <4 x i8> [[R]]
;
@@ -795,10 +793,12 @@ define <4 x i8> @smul_neg1_vec(<4 x i8> %x, <4 x i1>* %p) {
ret <4 x i8> %r
}
+; TODO: partly failed to match vector constant with poison element
+
define <4 x i8> @smul_neg1_vec_poison(<4 x i8> %x, <4 x i1>* %p) {
; CHECK-LABEL: @smul_neg1_vec_poison(
; CHECK-NEXT: [[M:%.*]] = tail call { <4 x i8>, <4 x i1> } @llvm.smul.with.overflow.v4i8(<4 x i8> [[X:%.*]], <4 x i8> <i8 -1, i8 -1, i8 poison, i8 -1>)
-; CHECK-NEXT: [[R:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 0
+; CHECK-NEXT: [[R:%.*]] = sub <4 x i8> zeroinitializer, [[X]]
; CHECK-NEXT: [[OV:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 1
; CHECK-NEXT: store <4 x i1> [[OV]], <4 x i1>* [[P:%.*]], align 1
; CHECK-NEXT: ret <4 x i8> [[R]]
@@ -827,9 +827,8 @@ define i8 @smul_neg2(i8 %x, i1* %p) {
define i8 @umul_neg1(i8 %x, i1* %p) {
; CHECK-LABEL: @umul_neg1(
-; CHECK-NEXT: [[M:%.*]] = tail call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 -1)
-; CHECK-NEXT: [[R:%.*]] = extractvalue { i8, i1 } [[M]], 0
-; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[M]], 1
+; CHECK-NEXT: [[R:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT: [[OV:%.*]] = icmp ugt i8 [[X]], 1
; CHECK-NEXT: store i1 [[OV]], i1* [[P:%.*]], align 1
; CHECK-NEXT: ret i8 [[R]]
;
@@ -842,9 +841,8 @@ define i8 @umul_neg1(i8 %x, i1* %p) {
define <4 x i8> @umul_neg1_vec(<4 x i8> %x, <4 x i1>* %p) {
; CHECK-LABEL: @umul_neg1_vec(
-; CHECK-NEXT: [[M:%.*]] = tail call { <4 x i8>, <4 x i1> } @llvm.umul.with.overflow.v4i8(<4 x i8> [[X:%.*]], <4 x i8> <i8 -1, i8 -1, i8 -1, i8 -1>)
-; CHECK-NEXT: [[R:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 0
-; CHECK-NEXT: [[OV:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 1
+; CHECK-NEXT: [[R:%.*]] = sub <4 x i8> zeroinitializer, [[X:%.*]]
+; CHECK-NEXT: [[OV:%.*]] = icmp ugt <4 x i8> [[X]], <i8 1, i8 1, i8 1, i8 1>
; CHECK-NEXT: store <4 x i1> [[OV]], <4 x i1>* [[P:%.*]], align 1
; CHECK-NEXT: ret <4 x i8> [[R]]
;
@@ -855,10 +853,12 @@ define <4 x i8> @umul_neg1_vec(<4 x i8> %x, <4 x i1>* %p) {
ret <4 x i8> %r
}
+; TODO: partly failed to match vector constant with poison element
+
define <4 x i8> @umul_neg1_vec_poison(<4 x i8> %x, <4 x i1>* %p) {
; CHECK-LABEL: @umul_neg1_vec_poison(
; CHECK-NEXT: [[M:%.*]] = tail call { <4 x i8>, <4 x i1> } @llvm.umul.with.overflow.v4i8(<4 x i8> [[X:%.*]], <4 x i8> <i8 poison, i8 -1, i8 -1, i8 poison>)
-; CHECK-NEXT: [[R:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 0
+; CHECK-NEXT: [[R:%.*]] = sub <4 x i8> zeroinitializer, [[X]]
; CHECK-NEXT: [[OV:%.*]] = extractvalue { <4 x i8>, <4 x i1> } [[M]], 1
; CHECK-NEXT: store <4 x i1> [[OV]], <4 x i1>* [[P:%.*]], align 1
; CHECK-NEXT: ret <4 x i8> [[R]]
@@ -870,6 +870,8 @@ define <4 x i8> @umul_neg1_vec_poison(<4 x i8> %x, <4 x i1>* %p) {
ret <4 x i8> %r
}
+; TODO: this could be 'shl' and 'icmp'
+
define i8 @umul_2(i8 %x, i1* %p) {
; CHECK-LABEL: @umul_2(
; CHECK-NEXT: [[M:%.*]] = tail call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 2)
@@ -889,10 +891,8 @@ define i8 @umul_2(i8 %x, i1* %p) {
define i8 @umul_neg1_select(i8 %x) {
; CHECK-LABEL: @umul_neg1_select(
-; CHECK-NEXT: [[M:%.*]] = tail call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 -1)
-; CHECK-NEXT: [[M0:%.*]] = extractvalue { i8, i1 } [[M]], 0
-; CHECK-NEXT: [[M1:%.*]] = extractvalue { i8, i1 } [[M]], 1
-; CHECK-NEXT: [[R:%.*]] = select i1 [[M1]], i8 -1, i8 [[M0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
+; CHECK-NEXT: [[R:%.*]] = sext i1 [[TMP1]] to i8
; CHECK-NEXT: ret i8 [[R]]
;
%m = tail call { i8, i1 } @llvm.umul.with.overflow.i8(i8 %x, i8 -1)
More information about the llvm-commits
mailing list