[PATCH] D141031: [InstCombine] Combine (zext a) mul (zext b) to llvm.umul.with.overflow only if mul has NUW flag
luxufan via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 7 22:42:33 PST 2023
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGeda8e999dd01: [InstCombine] Combine (zext a) mul (zext b) to llvm.umul.with.overflow only if… (authored by StephenFan).
Changed prior to commit:
https://reviews.llvm.org/D141031?vs=486473&id=487140#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D141031/new/
https://reviews.llvm.org/D141031
Files:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/overflow-mul.ll
Index: llvm/test/Transforms/InstCombine/overflow-mul.ll
===================================================================
--- llvm/test/Transforms/InstCombine/overflow-mul.ll
+++ llvm/test/Transforms/InstCombine/overflow-mul.ll
@@ -241,3 +241,41 @@
%cmp = icmp ne i32 %mul, %and
ret i1 %cmp
}
+
+; Negative test: mul(zext x, zext y) may overflow.
+define i32 @mul_may_overflow(i32 %x, i32 %y) {
+; CHECK-LABEL: @mul_may_overflow(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[L:%.*]] = zext i32 [[X:%.*]] to i34
+; CHECK-NEXT: [[R:%.*]] = zext i32 [[Y:%.*]] to i34
+; CHECK-NEXT: [[MUL34:%.*]] = mul i34 [[L]], [[R]]
+; CHECK-NEXT: [[OVERFLOW:%.*]] = icmp ult i34 [[MUL34]], 4294967296
+; CHECK-NEXT: [[RETVAL:%.*]] = zext i1 [[OVERFLOW]] to i32
+; CHECK-NEXT: ret i32 [[RETVAL]]
+;
+entry:
+ %l = zext i32 %x to i34
+ %r = zext i32 %y to i34
+ %mul34 = mul i34 %l, %r
+ %overflow = icmp ule i34 %mul34, 4294967295
+ %retval = zext i1 %overflow to i32
+ ret i32 %retval
+}
+
+define i32 @mul_known_nuw(i32 %x, i32 %y) {
+; CHECK-LABEL: @mul_known_nuw(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[UMUL:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT: [[TMP0:%.*]] = extractvalue { i32, i1 } [[UMUL]], 1
+; CHECK-NEXT: [[OVERFLOW:%.*]] = xor i1 [[TMP0]], true
+; CHECK-NEXT: [[RETVAL:%.*]] = zext i1 [[OVERFLOW]] to i32
+; CHECK-NEXT: ret i32 [[RETVAL]]
+;
+entry:
+ %l = zext i32 %x to i34
+ %r = zext i32 %y to i34
+ %mul34 = mul nuw i34 %l, %r
+ %overflow = icmp ule i34 %mul34, 4294967295
+ %retval = zext i1 %overflow to i32
+ ret i32 %retval
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6338,11 +6338,11 @@
}
// (zext a) * (zext b) --> llvm.umul.with.overflow.
- if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
+ if (match(Op0, m_NUWMul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
if (Instruction *R = processUMulZExtIdiom(I, Op0, Op1, *this))
return R;
}
- if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
+ if (match(Op1, m_NUWMul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
if (Instruction *R = processUMulZExtIdiom(I, Op1, Op0, *this))
return R;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141031.487140.patch
Type: text/x-patch
Size: 2463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230108/2305da4f/attachment.bin>
More information about the llvm-commits
mailing list