[llvm] 78bb06b - [ConstraintElim] Port `mul nuw` for unsigned to `mul nsw` to signed.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 25 12:37:31 PDT 2023
Author: Florian Hahn
Date: 2023-04-25T20:36:09+01:00
New Revision: 78bb06b1270c5d266dc080e31a7fed2075b02052
URL: https://github.com/llvm/llvm-project/commit/78bb06b1270c5d266dc080e31a7fed2075b02052
DIFF: https://github.com/llvm/llvm-project/commit/78bb06b1270c5d266dc080e31a7fed2075b02052.diff
LOG: [ConstraintElim] Port `mul nuw` for unsigned to `mul nsw` to signed.
Add handling for `mul nsw` for signed systems based on the logic for
`mul nuw` for unsigned.
Added:
Modified:
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/mul-nsw.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index ba7a590ec2b6..71d3fda429bf 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -327,6 +327,13 @@ static Decomposition decompose(Value *V,
if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1))))
return MergeResults(Op0, Op1, IsSigned);
+ ConstantInt *CI;
+ if (match(V, m_NSWMul(m_Value(Op0), m_ConstantInt(CI)))) {
+ auto Result = decompose(Op0, Preconditions, IsSigned, DL);
+ Result.mul(CI->getSExtValue());
+ return Result;
+ }
+
return V;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/mul-nsw.ll b/llvm/test/Transforms/ConstraintElimination/mul-nsw.ll
index 83edc11cf75f..241798dac925 100644
--- a/llvm/test/Transforms/ConstraintElimination/mul-nsw.ll
+++ b/llvm/test/Transforms/ConstraintElimination/mul-nsw.ll
@@ -14,16 +14,16 @@ define void @slt_mul_nsw_3_known_positive_1(i8 %start, i8 %high) {
; CHECK-NEXT: br i1 [[AND]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[T_0:%.*]] = icmp slt i8 [[START]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_0]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_1:%.*]] = mul nsw i8 [[START]], 1
; CHECK-NEXT: [[T_1:%.*]] = icmp slt i8 [[START_1]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_2:%.*]] = mul nsw i8 [[START]], 2
; CHECK-NEXT: [[T_2:%.*]] = icmp slt i8 [[START_2]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_3:%.*]] = mul nsw i8 [[START]], 3
; CHECK-NEXT: [[T_3:%.*]] = icmp slt i8 [[START_3]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_3]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_4:%.*]] = mul nsw i8 [[START]], 4
; CHECK-NEXT: [[C_3:%.*]] = icmp slt i8 [[START_4]], [[HIGH]]
; CHECK-NEXT: call void @use(i1 [[C_3]])
@@ -336,16 +336,16 @@ define void @slt_mul_nsw_neg_3_known_negative_1(i8 %start, i8 %high) {
; CHECK-NEXT: br i1 [[AND]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[T_0:%.*]] = icmp slt i8 [[START]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_0]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_1:%.*]] = mul nsw i8 [[START]], -1
; CHECK-NEXT: [[T_1:%.*]] = icmp slt i8 [[START_1]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_2:%.*]] = mul nsw i8 [[START]], -2
; CHECK-NEXT: [[T_2:%.*]] = icmp slt i8 [[START_2]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_3:%.*]] = mul nsw i8 [[START]], -3
; CHECK-NEXT: [[T_3:%.*]] = icmp slt i8 [[START_3]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_3]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_4:%.*]] = mul nsw i8 [[START]], -4
; CHECK-NEXT: [[C_3:%.*]] = icmp slt i8 [[START_4]], [[HIGH]]
; CHECK-NEXT: call void @use(i1 [[C_3]])
@@ -455,10 +455,10 @@ define void @slt_mul_nsw_3_known_negative_1(i8 %start, i8 %high) {
; CHECK-NEXT: call void @use(i1 [[C_5]])
; CHECK-NEXT: [[START_3:%.*]] = mul nsw i8 [[START]], 3
; CHECK-NEXT: [[T_0:%.*]] = icmp slt i8 [[START_3]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_0]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_4:%.*]] = mul nsw i8 [[START]], 4
; CHECK-NEXT: [[T_1:%.*]] = icmp slt i8 [[START_4]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: ret void
; CHECK: else:
; CHECK-NEXT: ret void
@@ -565,10 +565,10 @@ define void @slt_mul_nsw_neg_3_known_positive_1(i8 %start, i8 %high) {
; CHECK-NEXT: call void @use(i1 [[C_5]])
; CHECK-NEXT: [[START_3:%.*]] = mul nsw i8 [[START]], -3
; CHECK-NEXT: [[T_0:%.*]] = icmp slt i8 [[START_3]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_0]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_4:%.*]] = mul nsw i8 [[START]], -4
; CHECK-NEXT: [[T_1:%.*]] = icmp slt i8 [[START_4]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: ret void
; CHECK: else:
; CHECK-NEXT: ret void
@@ -666,16 +666,16 @@ define void @slt_mul_nsw_3_known_nonnegative_1(i8 %start, i8 %high) {
; CHECK-NEXT: br i1 [[AND]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[T_0:%.*]] = icmp slt i8 [[START]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_0]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_1:%.*]] = mul nsw i8 [[START]], 1
; CHECK-NEXT: [[T_1:%.*]] = icmp slt i8 [[START_1]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_2:%.*]] = mul nsw i8 [[START]], 2
; CHECK-NEXT: [[T_2:%.*]] = icmp slt i8 [[START_2]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_3:%.*]] = mul nsw i8 [[START]], 3
; CHECK-NEXT: [[T_3:%.*]] = icmp slt i8 [[START_3]], [[HIGH]]
-; CHECK-NEXT: call void @use(i1 [[T_3]])
+; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[START_4:%.*]] = mul nsw i8 [[START]], 4
; CHECK-NEXT: [[C_3:%.*]] = icmp slt i8 [[START_4]], [[HIGH]]
; CHECK-NEXT: call void @use(i1 [[C_3]])
More information about the llvm-commits
mailing list