[llvm] [Reassociate] Preserve `nuw` and `nsw` on `mul` chains (PR #97040)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 28 06:56:14 PDT 2024
================
@@ -32,3 +32,157 @@ define i4 @nuw_preserve_non_zero(i4 %a, i4 %b, i4 %c) {
%v1 = mul nuw i4 %v0, %b0
ret i4 %v1
}
+
+define i4 @re_order_mul_nuw(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
+; CHECK-LABEL: define i4 @re_order_mul_nuw(
+; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
+; CHECK-NEXT: [[X0:%.*]] = add nuw i4 [[XX0]], 1
+; CHECK-NEXT: [[X1:%.*]] = add nuw i4 [[XX1]], 1
+; CHECK-NEXT: [[X2:%.*]] = add nuw i4 [[XX2]], 1
+; CHECK-NEXT: [[X3:%.*]] = add nuw i4 [[XX3]], 1
+; CHECK-NEXT: [[MUL_B:%.*]] = mul nuw i4 [[X1]], [[X0]]
+; CHECK-NEXT: [[MUL_A:%.*]] = mul nuw i4 [[MUL_B]], [[X2]]
+; CHECK-NEXT: [[MUL_C:%.*]] = mul nuw i4 [[MUL_A]], [[X3]]
+; CHECK-NEXT: ret i4 [[MUL_C]]
+;
+ %x0 = add nuw i4 %xx0, 1
+ %x1 = add nuw i4 %xx1, 1
+ %x2 = add nuw i4 %xx2, 1
+ %x3 = add nuw i4 %xx3, 1
+ %mul_a = mul nuw i4 %x0, %x1
+ %mul_b = mul nuw i4 %x2, %x3
+ %mul_c = mul nuw i4 %mul_a, %mul_b
+ ret i4 %mul_c
+}
+
+define i4 @re_order_mul_nuw_fail_maybe_zero(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
+; CHECK-LABEL: define i4 @re_order_mul_nuw_fail_maybe_zero(
+; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
+; CHECK-NEXT: [[X0:%.*]] = add nsw i4 [[XX0]], 1
+; CHECK-NEXT: [[X1:%.*]] = add nuw i4 [[XX1]], 1
+; CHECK-NEXT: [[X2:%.*]] = add nuw i4 [[XX2]], 1
+; CHECK-NEXT: [[X3:%.*]] = add nuw i4 [[XX3]], 1
+; CHECK-NEXT: [[MUL_B:%.*]] = mul i4 [[X1]], [[X0]]
+; CHECK-NEXT: [[MUL_A:%.*]] = mul i4 [[MUL_B]], [[X2]]
+; CHECK-NEXT: [[MUL_C:%.*]] = mul i4 [[MUL_A]], [[X3]]
+; CHECK-NEXT: ret i4 [[MUL_C]]
+;
+ %x0 = add nsw i4 %xx0, 1
+ %x1 = add nuw i4 %xx1, 1
+ %x2 = add nuw i4 %xx2, 1
+ %x3 = add nuw i4 %xx3, 1
+ %mul_a = mul nuw i4 %x0, %x1
+ %mul_b = mul nuw i4 %x2, %x3
+ %mul_c = mul nuw i4 %mul_a, %mul_b
+ ret i4 %mul_c
+}
+
+define i4 @re_order_mul_nsw(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
+; CHECK-LABEL: define i4 @re_order_mul_nsw(
+; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
+; CHECK-NEXT: [[X0_NZ:%.*]] = add nuw i4 [[XX0]], 1
+; CHECK-NEXT: [[X1_NZ:%.*]] = add nuw i4 [[XX1]], 1
+; CHECK-NEXT: [[X2_NZ:%.*]] = add nuw i4 [[XX2]], 1
+; CHECK-NEXT: [[X3_NZ:%.*]] = add nuw i4 [[XX3]], 1
+; CHECK-NEXT: [[X0:%.*]] = call i4 @llvm.umin.i4(i4 [[X0_NZ]], i4 7)
+; CHECK-NEXT: [[X1:%.*]] = call i4 @llvm.umin.i4(i4 [[X1_NZ]], i4 7)
+; CHECK-NEXT: [[X2:%.*]] = call i4 @llvm.umin.i4(i4 [[X2_NZ]], i4 7)
+; CHECK-NEXT: [[X3:%.*]] = call i4 @llvm.umin.i4(i4 [[X3_NZ]], i4 7)
+; CHECK-NEXT: [[MUL_B:%.*]] = mul nsw i4 [[X1]], [[X0]]
+; CHECK-NEXT: [[MUL_A:%.*]] = mul nsw i4 [[MUL_B]], [[X2]]
+; CHECK-NEXT: [[MUL_C:%.*]] = mul nsw i4 [[MUL_A]], [[X3]]
+; CHECK-NEXT: ret i4 [[MUL_C]]
+;
+ %x0_nz = add nuw i4 %xx0, 1
+ %x1_nz = add nuw i4 %xx1, 1
+ %x2_nz = add nuw i4 %xx2, 1
+ %x3_nz = add nuw i4 %xx3, 1
+ %x0 = call i4 @llvm.umin.i4(i4 %x0_nz, i4 7)
+ %x1 = call i4 @llvm.umin.i4(i4 %x1_nz, i4 7)
+ %x2 = call i4 @llvm.umin.i4(i4 %x2_nz, i4 7)
+ %x3 = call i4 @llvm.umin.i4(i4 %x3_nz, i4 7)
----------------
nikic wrote:
I think it would be more obvious to write this as smax 0.
https://github.com/llvm/llvm-project/pull/97040
More information about the llvm-commits
mailing list