From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id 3FD6E38FD998 for ; Thu, 6 Jun 2024 14:04:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3FD6E38FD998 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3FD6E38FD998 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717682674; cv=none; b=RdJT2dabBKcaAagrXco+N25h8u9H+NP+SKwa7HmC5jqUEAGNY4CDkGJTpMph9CdRzW2FsZ7mniB78WcB7b8GvnD1GfamJ8UML6zl3AFBg0+h8amV7u0+HwgibFM7Oj2SbYalFM0fLqOImVFfd0z4xlLXxcIIWz/cfOHxLQj48nA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717682674; c=relaxed/simple; bh=OArC7E4YbAF+7hgDUNmSoD4gfgik+qx9LJExN5mCYZI=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=fA1ILsltvjUe9/CcZjYrFRiFEcHvVABjEaJgB9qNfgDakBk18VjVRZgRnG8DpY4lJYMc5qbTmEDg2BYFHVjcS82ZMdwVGH7cK/AWBGm73xeNJdcxDernLbWao7YZk29bp4W93lJ6Cm2WVLCN84Uy6/C0pzchICh2PTXuuy3LHVw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-52b950aa47bso1490241e87.1 for ; Thu, 06 Jun 2024 07:04:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717682669; x=1718287469; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=VH/n3yxu/BB7HtnfWV8PeZoIdgSaKnxWRcfsC43AqG8=; b=DDNXfiK3NgYpEOJ8W1wBVFR9UtB1dliePmtVCEwMOgDR1Jnhn94XqmhOYtCK5KOJF/ CK9QIw9EHg86ttsy2ySdT+0KoJCof9b+28od2qst4baPdRO3fTNMjKXRa13f3IgaxNoF RV9hQGtO6Zk2md3+LqqNq12yZR7t0I5AxLy6sa9sD87pXSu0WPVFz8JjvgECzDObpU8G ZYwx591I061NUthlNE+TJysPTkMRsU9lhtjszhBzeDsXj1EVYOQ/sCK0v6BlDT3L117z a3DhJi2jYb3Ndrf3S4C1EiiVj0tGav6AFv1hSHWyFMFnxjOISuBNjp1iVhzAcF5Vv3Sz tzVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717682669; x=1718287469; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VH/n3yxu/BB7HtnfWV8PeZoIdgSaKnxWRcfsC43AqG8=; b=KU+1SUoJClxGZ8T5UZR72IR+hdQTyODHP2/oOCGdf0uB8JVTbEKP/YftQjlBj+jMc6 Tx7k1fYUlpfiLZYuC1yRiOtB4nuwouOwJE56dzA8St5VbgCM5dueaNIj93405MrTG6ke VRAWUDxIgII6qB+0ekiAsAC5kzRT7YhcvhdST/d+IMp+Gan7CXHwJj/tA6gVzaSlyMIV pPOH4UzLqQp9dbf75P8hqMlsn5yboKbz+/1jS2uYT9hBE0/0E+j6awYBK72BYq4/OD1D yOS6vs+EMpQMywATBIkahVofUzOrSM8Ry6h+EQVVzKgK8sYzZCYTavrfwgdDuDKJ2v5b +iNg== X-Gm-Message-State: AOJu0YzyPkS+lI1xVYeKpyNg7lNdZPD/LyMaGQT5UgxhozHsSM+iXfDt 9DTqVx4ViNr1koBSfNDLGioozcGlMnEoSBPkSZcpGyQXKP8sQSpsu0xIU6TK4krEQBfsVQirTS3 8SNGJcFvCzAacd4Pf10R0NEoYCpM= X-Google-Smtp-Source: AGHT+IGgfkR6f1R3Kv+OpmEilYFtVowvCTnLxYA+XgOheMGAl8NaKYMtC8yDFShNCdzhLVUC8+YhBqfWOtHlnb+Fq6c= X-Received: by 2002:a05:6512:12d1:b0:52b:87e6:ec9 with SMTP id 2adb3069b0e04-52bab502133mr4310317e87.53.1717682669033; Thu, 06 Jun 2024 07:04:29 -0700 (PDT) MIME-Version: 1.0 References: <20240606133718.2343188-1-pan2.li@intel.com> In-Reply-To: <20240606133718.2343188-1-pan2.li@intel.com> From: Richard Biener Date: Thu, 6 Jun 2024 16:04:17 +0200 Message-ID: Subject: Re: [PATCH v7] Match: Support more form for scalar unsigned SAT_ADD To: pan2.li@intel.com Cc: gcc-patches@gcc.gnu.org, juzhe.zhong@rivai.ai, kito.cheng@gmail.com, tamar.christina@arm.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, Jun 6, 2024 at 3:37=E2=80=AFPM wrote: > > From: Pan Li > > After we support one gassign form of the unsigned .SAT_ADD, we > would like to support more forms including both the branch and > branchless. There are 5 other forms of .SAT_ADD, list as below: > > Form 1: > #define SAT_ADD_U_1(T) \ > T sat_add_u_1_##T(T x, T y) \ > { \ > return (T)(x + y) >=3D x ? (x + y) : -1; \ > } > > Form 2: > #define SAT_ADD_U_2(T) \ > T sat_add_u_2_##T(T x, T y) \ > { \ > T ret; \ > T overflow =3D __builtin_add_overflow (x, y, &ret); \ > return (T)(-overflow) | ret; \ > } > > Form 3: > #define SAT_ADD_U_3(T) \ > T sat_add_u_3_##T (T x, T y) \ > { \ > T ret; \ > return __builtin_add_overflow (x, y, &ret) ? -1 : ret; \ > } > > Form 4: > #define SAT_ADD_U_4(T) \ > T sat_add_u_4_##T (T x, T y) \ > { \ > T ret; \ > return __builtin_add_overflow (x, y, &ret) =3D=3D 0 ? ret : -1; \ > } > > Form 5: > #define SAT_ADD_U_5(T) \ > T sat_add_u_5_##T(T x, T y) \ > { \ > return (T)(x + y) < x ? -1 : (x + y); \ > } > > Take the forms 3 of above as example: > > uint64_t > sat_add (uint64_t x, uint64_t y) > { > uint64_t ret; > return __builtin_add_overflow (x, y, &ret) ? -1 : ret; > } > > Before this patch: > uint64_t sat_add (uint64_t x, uint64_t y) > { > long unsigned int _1; > long unsigned int _2; > uint64_t _3; > __complex__ long unsigned int _6; > > ;; basic block 2, loop depth 0 > ;; pred: ENTRY > _6 =3D .ADD_OVERFLOW (x_4(D), y_5(D)); > _2 =3D IMAGPART_EXPR <_6>; > if (_2 !=3D 0) > goto ; [35.00%] > else > goto ; [65.00%] > ;; succ: 4 > ;; 3 > > ;; basic block 3, loop depth 0 > ;; pred: 2 > _1 =3D REALPART_EXPR <_6>; > ;; succ: 4 > > ;; basic block 4, loop depth 0 > ;; pred: 3 > ;; 2 > # _3 =3D PHI <_1(3), 18446744073709551615(2)> > return _3; > ;; succ: EXIT > } > > After this patch: > uint64_t sat_add (uint64_t x, uint64_t y) > { > long unsigned int _12; > > ;; basic block 2, loop depth 0 > ;; pred: ENTRY > _12 =3D .SAT_ADD (x_4(D), y_5(D)); [tail call] > return _12; > ;; succ: EXIT > } > > The flag '^' acts on cond_expr will generate matching code similar as bel= ow: > > else if (gphi *_a1 =3D dyn_cast (_d1)) > { > basic_block _b1 =3D gimple_bb (_a1); > if (gimple_phi_num_args (_a1) =3D=3D 2) > { > basic_block _pb_0_1 =3D EDGE_PRED (_b1, 0)->src; > basic_block _pb_1_1 =3D EDGE_PRED (_b1, 1)->src; > basic_block _db_1 =3D safe_dyn_cast (*gsi_last_bb (_pb_= 0_1)) > ? _pb_0_1 : _pb_1_1; > basic_block _other_db_1 =3D safe_dyn_cast (*gsi_last_bb= (_pb_0_1)) > ? _pb_1_1 : _pb_0_1; > gcond *_ct_1 =3D safe_dyn_cast (*gsi_last_bb (_db_1)); > if (_ct_1 && EDGE_COUNT (_other_db_1->preds) =3D=3D 1 > && EDGE_COUNT (_other_db_1->succs) =3D=3D 1 > && EDGE_PRED (_other_db_1, 0)->src =3D=3D _db_1) > { > tree _cond_lhs_1 =3D gimple_cond_lhs (_ct_1); > tree _cond_rhs_1 =3D gimple_cond_rhs (_ct_1); > tree _p0 =3D build2 (gimple_cond_code (_ct_1), boolean_type_n= ode, > _cond_lhs_1, _cond_rhs_1); > bool _arg_0_is_true_1 =3D gimple_phi_arg_edge (_a1, 0)->flags= & EDGE_TRUE_VALUE; > tree _p1 =3D gimple_phi_arg_def (_a1, _arg_0_is_true_1 ? 0 : = 1); > tree _p2 =3D gimple_phi_arg_def (_a1, _arg_0_is_true_1 ? 1 : = 0); > .... > > The below test suites are passed for this patch. > * The x86 bootstrap test. > * The x86 fully regression test. > * The riscv fully regression test. OK. Thanks, Richard. > gcc/ChangeLog: > > * doc/match-and-simplify.texi: Add doc for the matching flag '^'. > * genmatch.cc (cmp_operand): Add match_phi comparation. > (dt_node::gen_kids_1): Add cond_expr bool flag for phi match. > (dt_operand::gen_phi_on_cond): Add new func to gen phi matching > on cond_expr. > (parser::parse_expr): Add handling for the expr flag '^'. > * match.pd: Add more form for unsigned .SAT_ADD. > * tree-ssa-math-opts.cc (build_saturation_binary_arith_call): Add > new func impl to build call for phi gimple. > (match_unsigned_saturation_add): Add new func impl to match the > .SAT_ADD for phi gimple. > (math_opts_dom_walker::after_dom_children): Add phi matching > try for all gimple phi stmt. > > Signed-off-by: Pan Li > --- > gcc/doc/match-and-simplify.texi | 16 ++++ > gcc/genmatch.cc | 126 +++++++++++++++++++++++++++++++- > gcc/match.pd | 43 ++++++++++- > gcc/tree-ssa-math-opts.cc | 56 +++++++++++++- > 4 files changed, 236 insertions(+), 5 deletions(-) > > diff --git a/gcc/doc/match-and-simplify.texi b/gcc/doc/match-and-simplify= .texi > index 01f19e2f62c..63d5af159f5 100644 > --- a/gcc/doc/match-and-simplify.texi > +++ b/gcc/doc/match-and-simplify.texi > @@ -361,6 +361,22 @@ Usually the types of the generated result expression= s are > determined from the context, but sometimes like in the above case > it is required that you specify them explicitly. > > +Another modifier for generated expressions is @code{^} which > +tells the machinery to try more matches for some special cases. > +For example, normally the @code{cond} only allows the gimple > +assign when matching. It will also try to match the gimple @code{PHI} > +besides gimple assign if appending the @code{^} to the @code{cond}. > +Aka @code{cond^}. Consider below example > + > +@smallexample > +(match (unsigned_sat_add @@0 @@1) > + (cond^ (ge (plus:c@@2 @@0 @@1) @@0) @@2 integer_minus_onep)) > +@end smallexample > + > +The above matching will generate the predicate function named > +@code{gimple_unsigned_sat_add} that accepts both the gimple > +assign and gimple @code{PHI}. > + > Another modifier for generated expressions is @code{!} which > tells the machinery to only consider the simplification in case > the marked expression simplified to a simple operand. Consider > diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc > index f1e0e7abe0c..a56bd90cb2c 100644 > --- a/gcc/genmatch.cc > +++ b/gcc/genmatch.cc > @@ -848,12 +848,13 @@ public: > : operand (OP_EXPR, loc), operation (operation_), > ops (vNULL), expr_type (NULL), is_commutative (is_commutative_), > is_generic (false), force_single_use (false), force_leaf (false), > - opt_grp (0) {} > + match_phi (false), opt_grp (0) {} > expr (expr *e) > : operand (OP_EXPR, e->location), operation (e->operation), > ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commu= tative), > is_generic (e->is_generic), force_single_use (e->force_single_use)= , > - force_leaf (e->force_leaf), opt_grp (e->opt_grp) {} > + force_leaf (e->force_leaf), match_phi (e->match_phi), > + opt_grp (e->opt_grp) {} > void append_op (operand *op) { ops.safe_push (op); } > /* The operator and its operands. */ > id_base *operation; > @@ -871,6 +872,8 @@ public: > /* Whether in the result expression this should be a leaf node > with any children simplified down to simple operands. */ > bool force_leaf; > + /* Whether the COND_EXPR is matching PHI gimple, default false. */ > + bool match_phi; > /* If non-zero, the group for optional handling. */ > unsigned char opt_grp; > void gen_transform (FILE *f, int, const char *, bool, int, > @@ -1819,6 +1822,7 @@ public: > > char *get_name (char *); > void gen_opname (char *, unsigned); > + void gen_phi_on_cond (FILE *, int, int); > }; > > /* Leaf node of the decision tree, used for DT_SIMPLIFY. */ > @@ -1898,7 +1902,8 @@ cmp_operand (operand *o1, operand *o2) > expr *e1 =3D static_cast(o1); > expr *e2 =3D static_cast(o2); > if (e1->operation !=3D e2->operation > - || e1->is_generic !=3D e2->is_generic) > + || e1->is_generic !=3D e2->is_generic > + || e1->match_phi !=3D e2->match_phi) > return false; > if (e1->operation->kind =3D=3D id_base::FN > /* For function calls also compare number of arguments. */ > @@ -3251,6 +3256,7 @@ dt_node::gen_kids_1 (FILE *f, int indent, bool gimp= le, int depth, > depth); > indent +=3D 4; > fprintf_indent (f, indent, "{\n"); > + bool cond_expr_p =3D false; > for (unsigned i =3D 0; i < exprs_len; ++i) > { > expr *e =3D as_a (gimple_exprs[i]->op); > @@ -3262,6 +3268,7 @@ dt_node::gen_kids_1 (FILE *f, int indent, bool gimp= le, int depth, > else > { > id_base *op =3D e->operation; > + cond_expr_p |=3D (*op =3D=3D COND_EXPR && e->match_phi)= ; > if (*op =3D=3D CONVERT_EXPR || *op =3D=3D NOP_EXPR) > fprintf_indent (f, indent, "CASE_CONVERT:\n"); > else > @@ -3275,6 +3282,27 @@ dt_node::gen_kids_1 (FILE *f, int indent, bool gim= ple, int depth, > fprintf_indent (f, indent, "default:;\n"); > fprintf_indent (f, indent, "}\n"); > indent -=3D 4; > + > + if (cond_expr_p) > + { > + fprintf_indent (f, indent, > + "else if (gphi *_a%d =3D dyn_cast (_d%d))\n", > + depth, depth); > + indent +=3D 2; > + fprintf_indent (f, indent, "{\n"); > + indent +=3D 2; > + > + for (unsigned i =3D 0; i < exprs_len; i++) > + { > + expr *e =3D as_a (gimple_exprs[i]->op); > + if (*e->operation =3D=3D COND_EXPR && e->match_phi) > + gimple_exprs[i]->gen_phi_on_cond (f, indent, depth); > + } > + > + indent -=3D 2; > + fprintf_indent (f, indent, "}\n"); > + indent -=3D 2; > + } > } > > if (fns_len) > @@ -3483,6 +3511,86 @@ dt_operand::gen (FILE *f, int indent, bool gimple,= int depth) > } > } > > +/* Generate matching code for the phi when meet COND_EXPR. */ > + > +void > +dt_operand::gen_phi_on_cond (FILE *f, int indent, int depth) > +{ > + fprintf_indent (f, indent, > + "basic_block _b%d =3D gimple_bb (_a%d);\n", depth, depth); > + > + fprintf_indent (f, indent, "if (gimple_phi_num_args (_a%d) =3D=3D 2)\n= ", depth); > + > + indent +=3D 2; > + fprintf_indent (f, indent, "{\n"); > + indent +=3D 2; > + > + fprintf_indent (f, indent, > + "basic_block _pb_0_%d =3D EDGE_PRED (_b%d, 0)->src;\n", depth, depth= ); > + fprintf_indent (f, indent, > + "basic_block _pb_1_%d =3D EDGE_PRED (_b%d, 1)->src;\n", depth, depth= ); > + fprintf_indent (f, indent, > + "basic_block _db_%d =3D safe_dyn_cast (*gsi_last_bb (_pb_0= _%d)) ? " > + "_pb_0_%d : _pb_1_%d;\n", depth, depth, depth, depth); > + fprintf_indent (f, indent, > + "basic_block _other_db_%d =3D safe_dyn_cast " > + "(*gsi_last_bb (_pb_0_%d)) ? _pb_1_%d : _pb_0_%d;\n", > + depth, depth, depth, depth); > + > + fprintf_indent (f, indent, > + "gcond *_ct_%d =3D safe_dyn_cast (*gsi_last_bb (_db_%d));\= n", > + depth, depth); > + fprintf_indent (f, indent, "if (_ct_%d" > + " && EDGE_COUNT (_other_db_%d->preds) =3D=3D 1\n", depth, depth); > + fprintf_indent (f, indent, > + " && EDGE_COUNT (_other_db_%d->succs) =3D=3D 1\n", depth); > + fprintf_indent (f, indent, > + " && EDGE_PRED (_other_db_%d, 0)->src =3D=3D _db_%d)\n", depth, dep= th); > + > + indent +=3D 2; > + fprintf_indent (f, indent, "{\n"); > + indent +=3D 2; > + > + fprintf_indent (f, indent, > + "tree _cond_lhs_%d =3D gimple_cond_lhs (_ct_%d);\n", depth, depth); > + fprintf_indent (f, indent, > + "tree _cond_rhs_%d =3D gimple_cond_rhs (_ct_%d);\n", depth, depth); > + > + char opname_0[20]; > + char opname_1[20]; > + char opname_2[20]; > + gen_opname (opname_0, 0); > + > + fprintf_indent (f, indent, > + "tree %s =3D build2 (gimple_cond_code (_ct_%d), " > + "boolean_type_node, _cond_lhs_%d, _cond_rhs_%d);\n", > + opname_0, depth, depth, depth); > + > + fprintf_indent (f, indent, > + "bool _arg_0_is_true_%d =3D gimple_phi_arg_edge (_a%d, 0)->flags" > + " & EDGE_TRUE_VALUE;\n", depth, depth); > + > + gen_opname (opname_1, 1); > + fprintf_indent (f, indent, > + "tree %s =3D gimple_phi_arg_def (_a%d, _arg_0_is_true_%d ? 0 : 1);\n= ", > + opname_1, depth, depth); > + > + gen_opname (opname_2, 2); > + fprintf_indent (f, indent, > + "tree %s =3D gimple_phi_arg_def (_a%d, _arg_0_is_true_%d ? 1 : 0);\n= ", > + opname_2, depth, depth); > + > + gen_kids (f, indent, true, depth); > + > + indent -=3D 2; > + fprintf_indent (f, indent, "}\n"); > + indent -=3D 2; > + > + indent -=3D 2; > + fprintf_indent (f, indent, "}\n"); > + indent -=3D 2; > +} > + > /* Emit a logging call to the debug file to the file F, with the INDENT = from > either the RESULT location or the S's match location if RESULT is nul= l. */ > static void > @@ -4600,6 +4708,18 @@ parser::parse_expr () > e->force_leaf =3D true; > } > > + if (token->type =3D=3D CPP_XOR && !(token->flags & PREV_WHITE)) > + { > + if (!parsing_match_operand) > + fatal_at (token, "modifier '^' is only valid in a match expressio= n"); > + > + if (!(*e->operation =3D=3D COND_EXPR)) > + fatal_at (token, "modifier '^' can only act on operation COND_EXP= R"); > + > + eat_token (CPP_XOR); > + e->match_phi =3D true; > + } > + > if (token->type =3D=3D CPP_COLON > && !(token->flags & PREV_WHITE)) > { > diff --git a/gcc/match.pd b/gcc/match.pd > index 7c1ad428a3c..5f36c1fac82 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -3056,31 +3056,48 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype)))))) > > /* Unsigned Saturation Add */ > +/* SAT_ADD =3D usadd_left_part_1 | usadd_right_part_1, aka: > + SAT_ADD =3D (X + Y) | -((X + Y) < X) */ > (match (usadd_left_part_1 @0 @1) > (plus:c @0 @1) > (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) > && types_match (type, @0, @1)))) > > +/* SAT_ADD =3D usadd_left_part_2 | usadd_right_part_2, aka: > + SAT_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVER= FLOW> !=3D 0) */ > (match (usadd_left_part_2 @0 @1) > (realpart (IFN_ADD_OVERFLOW:c @0 @1)) > (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) > && types_match (type, @0, @1)))) > > +/* SAT_ADD =3D usadd_left_part_1 | usadd_right_part_1, aka: > + SAT_ADD =3D (X + Y) | -((type)(X + Y) < X) */ > (match (usadd_right_part_1 @0 @1) > (negate (convert (lt (plus:c @0 @1) @0))) > (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) > && types_match (type, @0, @1)))) > > +/* SAT_ADD =3D usadd_left_part_1 | usadd_right_part_1, aka: > + SAT_ADD =3D (X + Y) | -(X > (X + Y)) */ > (match (usadd_right_part_1 @0 @1) > (negate (convert (gt @0 (plus:c @0 @1)))) > (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) > && types_match (type, @0, @1)))) > > +/* SAT_ADD =3D usadd_left_part_2 | usadd_right_part_2, aka: > + SAT_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVER= FLOW> !=3D 0) */ > (match (usadd_right_part_2 @0 @1) > (negate (convert (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zero= p))) > (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) > && types_match (type, @0, @1)))) > > +/* SAT_ADD =3D usadd_left_part_2 | usadd_right_part_2, aka: > + SAT_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> | -IMAGPART_EXPR <.ADD_OVER= FLOW> */ > +(match (usadd_right_part_2 @0 @1) > + (negate (imagpart (IFN_ADD_OVERFLOW:c @0 @1))) > + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) > + && types_match (type, @0, @1)))) > + > /* We cannot merge or overload usadd_left_part_1 and usadd_left_part_2 > because the sub part of left_part_2 cannot work with right_part_1. > For example, left_part_2 pattern focus one .ADD_OVERFLOW but the > @@ -3092,10 +3109,34 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (match (unsigned_integer_sat_add @0 @1) > (bit_ior:c (usadd_left_part_1 @0 @1) (usadd_right_part_1 @0 @1))) > > -/* Unsigned saturation add, case 2 (branchless with .ADD_OVERFLOW). */ > +/* Unsigned saturation add, case 2 (branchless with .ADD_OVERFLOW): > + SAT_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> | -IMAGPART_EXPR <.ADD_OVER= FLOW> or > + SAT_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVER= FLOW> !=3D 0) */ > (match (unsigned_integer_sat_add @0 @1) > (bit_ior:c (usadd_left_part_2 @0 @1) (usadd_right_part_2 @0 @1))) > > +/* Unsigned saturation add, case 3 (branch with ge): > + SAT_U_ADD =3D (X + Y) >=3D x ? (X + Y) : -1. */ > +(match (unsigned_integer_sat_add @0 @1) > + (cond^ (ge (usadd_left_part_1@2 @0 @1) @0) @2 integer_minus_onep)) > + > +/* Unsigned saturation add, case 4 (branch with lt): > + SAT_U_ADD =3D (X + Y) < x ? -1 : (X + Y). */ > +(match (unsigned_integer_sat_add @0 @1) > + (cond^ (lt (usadd_left_part_1@2 @0 @1) @0) integer_minus_onep @2)) > + > +/* Unsigned saturation add, case 5 (branch with eq .ADD_OVERFLOW): > + SAT_U_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> =3D=3D 0 ? .ADD_OVERFLOW = : -1. */ > +(match (unsigned_integer_sat_add @0 @1) > + (cond^ (eq (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop) > + (usadd_left_part_2 @0 @1) integer_minus_onep)) > + > +/* Unsigned saturation add, case 6 (branch with ne .ADD_OVERFLOW): > + SAT_U_ADD =3D REALPART_EXPR <.ADD_OVERFLOW> !=3D 0 ? -1 : .ADD_OVERFL= OW. */ > +(match (unsigned_integer_sat_add @0 @1) > + (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop) > + integer_minus_onep (usadd_left_part_2 @0 @1))) > + > /* Unsigned saturation sub, case 1 (branch with gt): > SAT_U_SUB =3D X > Y ? X - Y : 0 */ > (match (unsigned_integer_sat_sub @0 @1) > diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc > index 469e34d828d..173b0366f5e 100644 > --- a/gcc/tree-ssa-math-opts.cc > +++ b/gcc/tree-ssa-math-opts.cc > @@ -4101,8 +4101,24 @@ build_saturation_binary_arith_call (gimple_stmt_it= erator *gsi, internal_fn fn, > } > } > > +static void > +build_saturation_binary_arith_call (gimple_stmt_iterator *gsi, gphi *phi= , > + internal_fn fn, tree lhs, tree op_0, > + tree op_1) > +{ > + if (direct_internal_fn_supported_p (fn, TREE_TYPE (lhs), OPTIMIZE_FOR_= BOTH)) > + { > + gcall *call =3D gimple_build_call_internal (fn, 2, op_0, op_1); > + gimple_call_set_lhs (call, lhs); > + gsi_insert_before (gsi, call, GSI_SAME_STMT); > + > + gimple_stmt_iterator psi =3D gsi_for_stmt (phi); > + remove_phi_node (&psi, /* release_lhs_p */ false); > + } > +} > + > /* > - * Try to match saturation unsigned add. > + * Try to match saturation unsigned add with assign. > * _7 =3D _4 + _6; > * _8 =3D _4 > _7; > * _9 =3D (long unsigned int) _8; > @@ -4121,6 +4137,37 @@ match_unsigned_saturation_add (gimple_stmt_iterato= r *gsi, gassign *stmt) > build_saturation_binary_arith_call (gsi, IFN_SAT_ADD, lhs, ops[0], o= ps[1]); > } > > +/* > + * Try to match saturation unsigned add with PHI. > + * : > + * _1 =3D x_3(D) + y_4(D); > + * if (_1 >=3D x_3(D)) > + * goto ; [INV] > + * else > + * goto ; [INV] > + * > + * : > + * > + * : > + * # _2 =3D PHI <255(2), _1(3)> > + * =3D> > + * [local count: 1073741824]: > + * _2 =3D .SAT_ADD (x_4(D), y_5(D)); */ > + > +static void > +match_unsigned_saturation_add (gimple_stmt_iterator *gsi, gphi *phi) > +{ > + if (gimple_phi_num_args (phi) !=3D 2) > + return; > + > + tree ops[2]; > + tree phi_result =3D gimple_phi_result (phi); > + > + if (gimple_unsigned_integer_sat_add (phi_result, ops, NULL)) > + build_saturation_binary_arith_call (gsi, phi, IFN_SAT_ADD, phi_resul= t, > + ops[0], ops[1]); > +} > + > /* > * Try to match saturation unsigned sub. > * _1 =3D _4 >=3D _5; > @@ -6052,6 +6099,13 @@ math_opts_dom_walker::after_dom_children (basic_bl= ock bb) > > fma_deferring_state fma_state (param_avoid_fma_max_bits > 0); > > + for (gphi_iterator psi =3D gsi_start_phis (bb); !gsi_end_p (psi); > + gsi_next (&psi)) > + { > + gimple_stmt_iterator gsi =3D gsi_last_bb (bb); > + match_unsigned_saturation_add (&gsi, psi.phi ()); > + } > + > for (gsi =3D gsi_after_labels (bb); !gsi_end_p (gsi);) > { > gimple *stmt =3D gsi_stmt (gsi); > -- > 2.34.1 >