From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 2A5133951E5E; Thu, 1 Jul 2021 07:26:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2A5133951E5E Received: by mail-ej1-x632.google.com with SMTP id hc16so8744653ejc.12; Thu, 01 Jul 2021 00:26:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fYBDKmgaXPgUB2gVVs0D9wp4nKXa/BuZLmhebcoQTs8=; b=b3TqZYQXAKEV9USckRTwhOJlcE4aHdIcPuQETXD/LXkugEdigyK0EIt+T6uFQpyKFz O0HcYbDU6qZBDDdIOh9S/QFUkY5dw0mCtZt88iyxPkzWPM5VCP8T60soAOnasoDV5Wuu v0Y9PLKlFs8pnz5H7BLFY7NiUkB7mj/BoxKdKWVScR8KJ+8CNcYZizbiMK1+XZHCuO84 7bEEnOcmtsfxZP+IBI4TeRZZX8y2z6tua8Wh3CYc1aSRlTmmlCYTeKFGwrSHX4OBU1cP QBkc8BB1qNfIYMwzAwiujudqOj03ZUJbqtY3YsqEZnEgGUBjjg16Gbu3FixifZovM+1P W72A== X-Gm-Message-State: AOAM533F0hGOtEH+G9QEr8w86Zj2Dobm+kykc/+st0TqMqwDNVM1f2B3 /aULpRJtm/mMaAfvLouV1jcB0cuz8Zcum+pkIDw= X-Google-Smtp-Source: ABdhPJz7yGzSNTQ6mrA/rgOmu4sQHlNUpsoITfBPKiEVyPDBEE8CNbiAFcQU7v+E3EO++lt6R3Q5Hc31ssXfTqV5A90= X-Received: by 2002:a17:907:9620:: with SMTP id gb32mr12911298ejc.119.1625124376062; Thu, 01 Jul 2021 00:26:16 -0700 (PDT) MIME-Version: 1.0 References: <20210701020503.17845-1-guojiufu@linux.ibm.com> In-Reply-To: <20210701020503.17845-1-guojiufu@linux.ibm.com> From: "Bin.Cheng" Date: Thu, 1 Jul 2021 15:22:16 +0800 Message-ID: Subject: Re: [PATCH] Analyze niter for until-wrap condition [PR101145] To: Jiufu Guo Cc: gcc-patches List , Richard Biener , amker@gcc.gnu.org, Segher Boessenkool , jlaw@tachyum.com, Bill Schmidt , dje.gcc@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.9 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 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Jul 2021 07:26:20 -0000 On Thu, Jul 1, 2021 at 10:06 AM Jiufu Guo via Gcc-patches wrote: > > For code like: > unsigned foo(unsigned val, unsigned start) > { > unsigned cnt = 0; > for (unsigned i = start; i > val; ++i) > cnt++; > return cnt; > } > > The number of iterations should be about UINT_MAX - start. > > There is function adjust_cond_for_loop_until_wrap which > handles similar work for const bases. > Like adjust_cond_for_loop_until_wrap, this patch enhance > function number_of_iterations_cond/number_of_iterations_lt > to analyze number of iterations for this kind of loop. > > Bootstrap and regtest pass on powerpc64le, is this ok for trunk? > > gcc/ChangeLog: > > PR tree-optimization/101145 > * tree-ssa-loop-niter.c > (number_of_iterations_until_wrap): New function. > (number_of_iterations_lt): Invoke above function. > (adjust_cond_for_loop_until_wrap): > Merge to number_of_iterations_until_wrap. > (number_of_iterations_cond): Update invokes for > adjust_cond_for_loop_until_wrap and number_of_iterations_lt. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/101145 > * gcc.dg/vect/pr101145.c: New test. > * gcc.dg/vect/pr101145.inc: New test. > * gcc.dg/vect/pr101145_1.c: New test. > * gcc.dg/vect/pr101145_2.c: New test. > * gcc.dg/vect/pr101145_3.c: New test. > --- > gcc/testsuite/gcc.dg/vect/pr101145.c | 187 +++++++++++++++++++++++++ > gcc/testsuite/gcc.dg/vect/pr101145.inc | 63 +++++++++ > gcc/testsuite/gcc.dg/vect/pr101145_1.c | 15 ++ > gcc/testsuite/gcc.dg/vect/pr101145_2.c | 15 ++ > gcc/testsuite/gcc.dg/vect/pr101145_3.c | 15 ++ > gcc/tree-ssa-loop-niter.c | 150 +++++++++++--------- > 6 files changed, 380 insertions(+), 65 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/vect/pr101145.c > create mode 100644 gcc/testsuite/gcc.dg/vect/pr101145.inc > create mode 100644 gcc/testsuite/gcc.dg/vect/pr101145_1.c > create mode 100644 gcc/testsuite/gcc.dg/vect/pr101145_2.c > create mode 100644 gcc/testsuite/gcc.dg/vect/pr101145_3.c > > diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c > index b5add827018..06db6a36ef8 100644 > --- a/gcc/tree-ssa-loop-niter.c > +++ b/gcc/tree-ssa-loop-niter.c > @@ -1473,6 +1473,86 @@ assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1, > } > } > > +/* Determines number of iterations of loop whose ending condition > + is IV0 < IV1 which likes: {base, -C} < n, or n < {base, C}. > + The number of iterations is stored to NITER. */ > + > +static bool > +number_of_iterations_until_wrap (class loop *, tree type, affine_iv *iv0, > + affine_iv *iv1, class tree_niter_desc *niter) > +{ > + tree niter_type = unsigned_type_for (type); > + tree max, min; > + > + if (POINTER_TYPE_P (type)) > + { > + max = fold_convert (type, TYPE_MAX_VALUE (niter_type)); > + min = fold_convert (type, TYPE_MIN_VALUE (niter_type)); > + } > + else > + { > + max = TYPE_MAX_VALUE (type); > + min = TYPE_MIN_VALUE (type); > + } > + > + tree high = max, low = min, one = build_int_cst (niter_type, 1); > + tree step; > + > + /* n < {base, C}. */ > + if (integer_zerop (iv0->step) && TREE_CODE (iv1->step) == INTEGER_CST > + && !tree_int_cst_sign_bit (iv1->step)) > + { > + step = iv1->step; > + niter->niter = fold_build2 (MINUS_EXPR, niter_type, max, iv1->base); max/iv1->base could be of pointer type, not sure if this is canonical though. > + if (TREE_CODE (iv1->base) == INTEGER_CST) > + low = fold_build2 (MINUS_EXPR, type, iv1->base, one); > + else if (TREE_CODE (iv0->base) == INTEGER_CST) > + low = iv0->base; > + } > + /* {base, -C} < n. */ > + else if (TREE_CODE (iv0->step) == INTEGER_CST > + && tree_int_cst_sign_bit (iv0->step) && integer_zerop (iv1->step)) > + { > + step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv0->step), iv0->step); > + niter->niter = fold_build2 (MINUS_EXPR, niter_type, iv0->base, min); > + if (TREE_CODE (iv0->base) == INTEGER_CST) > + high = fold_build2 (PLUS_EXPR, type, iv0->base, one); > + else if (TREE_CODE (iv1->base) == INTEGER_CST) > + high = iv1->base; > + } > + else > + return false; > + > + /* (delta + step - 1) / step */ > + step = fold_convert (niter_type, step); > + niter->niter = fold_convert (niter_type, niter->niter); > + niter->niter = fold_build2 (PLUS_EXPR, niter_type, niter->niter, step); > + niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, niter->niter, step); > + > + tree m = fold_build2 (MINUS_EXPR, niter_type, high, low); > + m = fold_convert (niter_type, m); > + mpz_t mstep, tmp, mmax; > + mpz_init (mstep); > + mpz_init (tmp); > + mpz_init (mmax); > + wi::to_mpz (wi::to_wide (step), mstep, UNSIGNED); > + wi::to_mpz (wi::to_wide (m), mmax, UNSIGNED); > + mpz_add (tmp, mmax, mstep); > + mpz_sub_ui (tmp, tmp, 1); > + mpz_fdiv_q (tmp, tmp, mstep); > + niter->max = widest_int::from (wi::from_mpz (niter_type, tmp, false), > + TYPE_SIGN (niter_type)); This computation is similar to function number_of_iterations_lt, could we factor it out into an independent function? > + mpz_clear (mstep); > + mpz_clear (tmp); > + > + niter->may_be_zero > + = fold_build2 (LE_EXPR, boolean_type_node, iv1->base, iv0->base); If iv0->base and iv1->base are constant and iv1->base <= iv0->base, the number of iteration is actually zero, but here we rely on may_be_zero (== true), which loses information. Could we specially handle this case and do a fast return? Could you test this on some more targets(x86, aarch64) please? Otherwise LGTM. Thanks, bin > + > + niter->control.no_overflow = false; > + > + return true; > +} > + > /* Determines number of iterations of loop whose ending condition > is IV0 < IV1. TYPE is the type of the iv. The number of > iterations is stored to NITER. BNDS bounds the difference > @@ -1501,6 +1581,11 @@ number_of_iterations_lt (class loop *loop, tree type, affine_iv *iv0, > niter->bound = iv0->base; > } > > + /* {base, -C} < n, or n < {base, C} */ > + if (tree_int_cst_sign_bit (iv0->step) > + || (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))) > + return number_of_iterations_until_wrap (loop, type, iv0, iv1, niter); > + > delta = fold_build2 (MINUS_EXPR, niter_type, > fold_convert (niter_type, iv1->base), > fold_convert (niter_type, iv0->base)); > @@ -1665,62 +1750,6 @@ dump_affine_iv (FILE *file, affine_iv *iv) > } > } > > -/* Given exit condition IV0 CODE IV1 in TYPE, this function adjusts > - the condition for loop-until-wrap cases. For example: > - (unsigned){8, -1}_loop < 10 => {0, 1} != 9 > - 10 < (unsigned){0, max - 7}_loop => {0, 1} != 8 > - Return true if condition is successfully adjusted. */ > - > -static bool > -adjust_cond_for_loop_until_wrap (tree type, affine_iv *iv0, tree_code *code, > - affine_iv *iv1) > -{ > - /* Only support simple cases for the moment. */ > - if (TREE_CODE (iv0->base) != INTEGER_CST > - || TREE_CODE (iv1->base) != INTEGER_CST) > - return false; > - > - tree niter_type = unsigned_type_for (type), high, low; > - /* Case: i-- < 10. */ > - if (integer_zerop (iv1->step)) > - { > - /* TODO: Should handle case in which abs(step) != 1. */ > - if (!integer_minus_onep (iv0->step)) > - return false; > - /* Give up on infinite loop. */ > - if (*code == LE_EXPR > - && tree_int_cst_equal (iv1->base, TYPE_MAX_VALUE (type))) > - return false; > - high = fold_build2 (PLUS_EXPR, niter_type, > - fold_convert (niter_type, iv0->base), > - build_int_cst (niter_type, 1)); > - low = fold_convert (niter_type, TYPE_MIN_VALUE (type)); > - } > - else if (integer_zerop (iv0->step)) > - { > - /* TODO: Should handle case in which abs(step) != 1. */ > - if (!integer_onep (iv1->step)) > - return false; > - /* Give up on infinite loop. */ > - if (*code == LE_EXPR > - && tree_int_cst_equal (iv0->base, TYPE_MIN_VALUE (type))) > - return false; > - high = fold_convert (niter_type, TYPE_MAX_VALUE (type)); > - low = fold_build2 (MINUS_EXPR, niter_type, > - fold_convert (niter_type, iv1->base), > - build_int_cst (niter_type, 1)); > - } > - else > - gcc_unreachable (); > - > - iv0->base = low; > - iv0->step = fold_convert (niter_type, integer_one_node); > - iv1->base = high; > - iv1->step = build_int_cst (niter_type, 0); > - *code = NE_EXPR; > - return true; > -} > - > /* Determine the number of iterations according to condition (for staying > inside loop) which compares two induction variables using comparison > operator CODE. The induction variable on left side of the comparison > @@ -1855,15 +1884,6 @@ number_of_iterations_cond (class loop *loop, > return true; > } > > - /* Handle special case loops: while (i-- < 10) and while (10 < i++) by > - adjusting iv0, iv1 and code. */ > - if (code != NE_EXPR > - && (tree_int_cst_sign_bit (iv0->step) > - || (!integer_zerop (iv1->step) > - && !tree_int_cst_sign_bit (iv1->step))) > - && !adjust_cond_for_loop_until_wrap (type, iv0, &code, iv1)) > - return false; > - > /* OK, now we know we have a senseful loop. Handle several cases, depending > on what comparison operator is used. */ > bound_difference (loop, iv1->base, iv0->base, &bnds); > -- > 2.17.1 >