[llvm] [InstCombine] Factorise add/sub and max/min using distributivity (PR #101507)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 1 10:08:52 PDT 2024


================
@@ -0,0 +1,210 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s 2>&1 | FileCheck %s
+
+define i32 @umin_of_umax(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umin_of_umax(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %max1 = call i32 @llvm.umax.i32(i32 %x, i32 %z)
+  %max2 = call i32 @llvm.umax.i32(i32 %y, i32 %z)
+  %min = call i32 @llvm.umin.i32(i32 %max1, i32 %max2)
+  ret i32 %min
+}
+
+define i32 @umin_of_umax_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umin_of_umax_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %max1 = call i32 @llvm.umax.i32(i32 %z, i32 %x)
+  %max2 = call i32 @llvm.umax.i32(i32 %z, i32 %y)
+  %min = call i32 @llvm.umin.i32(i32 %max1, i32 %max2)
+  ret i32 %min
+}
+
+define i32 @smin_of_smax(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smin_of_smax(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %max1 = call i32 @llvm.smax.i32(i32 %x, i32 %z)
+  %max2 = call i32 @llvm.smax.i32(i32 %y, i32 %z)
+  %min = call i32 @llvm.smin.i32(i32 %max1, i32 %max2)
+  ret i32 %min
+}
+
+define i32 @smin_of_smax_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smin_of_smax_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %max1 = call i32 @llvm.smax.i32(i32 %z, i32 %x)
+  %max2 = call i32 @llvm.smax.i32(i32 %z, i32 %y)
+  %min = call i32 @llvm.smin.i32(i32 %max1, i32 %max2)
+  ret i32 %min
+}
+
+define i32 @umax_of_umin(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umax_of_umin(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.umin.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %min1 = call i32 @llvm.umin.i32(i32 %x, i32 %z)
+  %min2 = call i32 @llvm.umin.i32(i32 %y, i32 %z)
+  %max = call i32 @llvm.umax.i32(i32 %min1, i32 %min2)
+  ret i32 %max
+}
+
+define i32 @umax_of_umin_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umax_of_umin_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.umin.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %min1 = call i32 @llvm.umin.i32(i32 %z, i32 %x)
+  %min2 = call i32 @llvm.umin.i32(i32 %z, i32 %y)
+  %max = call i32 @llvm.umax.i32(i32 %min1, i32 %min2)
+  ret i32 %max
+}
+
+define i32 @smax_of_smin(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smax_of_smin(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %min1 = call i32 @llvm.smin.i32(i32 %x, i32 %z)
+  %min2 = call i32 @llvm.smin.i32(i32 %y, i32 %z)
+  %max = call i32 @llvm.smax.i32(i32 %min1, i32 %min2)
+  ret i32 %max
+}
+
+define i32 @smax_of_smin_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smax_of_smin_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %min1 = call i32 @llvm.smin.i32(i32 %z, i32 %x)
+  %min2 = call i32 @llvm.smin.i32(i32 %z, i32 %y)
+  %max = call i32 @llvm.smax.i32(i32 %min1, i32 %min2)
+  ret i32 %max
+}
+
+define i32 @umax_of_uadd_sat(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umax_of_uadd_sat(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %add1 = call i32 @llvm.uadd.sat.i32(i32 %x, i32 %z)
+  %add2 = call i32 @llvm.uadd.sat.i32(i32 %y, i32 %z)
+  %max = call i32 @llvm.umax.i32(i32 %add1, i32 %add2)
+  ret i32 %max
+}
+
+define i32 @umax_of_uadd_sat_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umax_of_uadd_sat_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %add1 = call i32 @llvm.uadd.sat.i32(i32 %z, i32 %x)
+  %add2 = call i32 @llvm.uadd.sat.i32(i32 %z, i32 %y)
+  %max = call i32 @llvm.umax.i32(i32 %add1, i32 %add2)
+  ret i32 %max
+}
+
+define i32 @umin_of_uadd_sat(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umin_of_uadd_sat(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %add1 = call i32 @llvm.uadd.sat.i32(i32 %x, i32 %z)
+  %add2 = call i32 @llvm.uadd.sat.i32(i32 %y, i32 %z)
+  %min = call i32 @llvm.umin.i32(i32 %add1, i32 %add2)
+  ret i32 %min
+}
+
+define i32 @umin_of_uadd_sat_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @umin_of_uadd_sat_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %add1 = call i32 @llvm.uadd.sat.i32(i32 %z, i32 %x)
+  %add2 = call i32 @llvm.uadd.sat.i32(i32 %z, i32 %y)
+  %min = call i32 @llvm.umin.i32(i32 %add1, i32 %add2)
+  ret i32 %min
+}
+
+define i32 @smax_of_sadd_sat(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smax_of_sadd_sat(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.sadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %add1 = call i32 @llvm.sadd.sat.i32(i32 %x, i32 %z)
+  %add2 = call i32 @llvm.sadd.sat.i32(i32 %y, i32 %z)
+  %max = call i32 @llvm.smax.i32(i32 %add1, i32 %add2)
+  ret i32 %max
+}
+
+define i32 @smax_of_sadd_sat_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smax_of_sadd_sat_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.sadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MAX]]
+;
+  %add1 = call i32 @llvm.sadd.sat.i32(i32 %z, i32 %x)
+  %add2 = call i32 @llvm.sadd.sat.i32(i32 %z, i32 %y)
+  %max = call i32 @llvm.smax.i32(i32 %add1, i32 %add2)
+  ret i32 %max
+}
+
+define i32 @smin_of_sadd_sat(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smin_of_sadd_sat(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.sadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %add1 = call i32 @llvm.sadd.sat.i32(i32 %x, i32 %z)
+  %add2 = call i32 @llvm.sadd.sat.i32(i32 %y, i32 %z)
+  %min = call i32 @llvm.smin.i32(i32 %add1, i32 %add2)
+  ret i32 %min
+}
+
+define i32 @smin_of_sadd_sat_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smin_of_sadd_sat_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.sadd.sat.i32(i32 [[TMP1]], i32 [[Z]])
+; CHECK-NEXT:    ret i32 [[MIN]]
+;
+  %add1 = call i32 @llvm.sadd.sat.i32(i32 %z, i32 %x)
+  %add2 = call i32 @llvm.sadd.sat.i32(i32 %z, i32 %y)
+  %min = call i32 @llvm.smin.i32(i32 %add1, i32 %add2)
----------------
goldsteinn wrote:

Can you change these tests to `i8` so its easier to verify them?

https://github.com/llvm/llvm-project/pull/101507


More information about the llvm-commits mailing list