From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id D08A6384DD3B for ; Mon, 20 Feb 2023 13:08:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D08A6384DD3B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 9C8BF223CA for ; Mon, 20 Feb 2023 13:08:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1676898489; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=6zc3lZ5gk+VmoEqnL0KMOwJ1iaZvkd5OH+KrmS/YKn4=; b=pORK3I9c9ADdGp1vxdjC+6eiSM8K2C2msJmRDaEmxmFqNNAvzPcMnIccPTelRXg7xcvZkg H1u0mGB/4dMxRB69vvymFV02AW5nF9Sf5hodiE/htEibX0vdCZY+Cg6i5H00rGyjyDh1ln eRgWYPgQ+V0wHq5t2tGptFRrDwy7ZzM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1676898489; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=6zc3lZ5gk+VmoEqnL0KMOwJ1iaZvkd5OH+KrmS/YKn4=; b=qLo44N0ZQxOWzgXKAIcM2LgWwGbkMfljnaP91mT0R7S8DnAZiZ4c8COcnsS39PCgUNReeL eJVLlXDAxda3WoAA== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 938762C142 for ; Mon, 20 Feb 2023 13:08:09 +0000 (UTC) Date: Mon, 20 Feb 2023 13:08:09 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/108793 - niter compute type mismatch User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,MISSING_MID,SPF_HELO_NONE,SPF_PASS,TXREP 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: Message-ID: <20230220130809.8uleMijSqK2qPnZJS4uQvZd1pmf7A6oUyAEabTu0euQ@z> When computing the number of iterations until wrap types are mixed up, eventually leading to checking ICEs with a pointer bitwise inversion. The following uses niter_type for the calculation. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/108793 * tree-ssa-loop-niter.cc (number_of_iterations_until_wrap): Use convert operands to niter_type when computing num. * gcc.dg/torture/pr108793.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr108793.c | 10 ++++++++++ gcc/tree-ssa-loop-niter.cc | 11 ++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr108793.c diff --git a/gcc/testsuite/gcc.dg/torture/pr108793.c b/gcc/testsuite/gcc.dg/torture/pr108793.c new file mode 100644 index 00000000000..83973eb05d9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr108793.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ + +typedef int *p; +extern p a[], b[]; +int f () { + int n = 0; + for (p* i = &a[0]; i > &b[0]; i++) + n++; + return n; +} diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc index 1ce5e736ce3..dc4c7a418f6 100644 --- a/gcc/tree-ssa-loop-niter.cc +++ b/gcc/tree-ssa-loop-niter.cc @@ -1494,8 +1494,9 @@ number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0, if (integer_zerop (assumptions)) return false; - num = fold_build2 (MINUS_EXPR, niter_type, wide_int_to_tree (type, max), - iv1->base); + num = fold_build2 (MINUS_EXPR, niter_type, + wide_int_to_tree (niter_type, max), + fold_convert (niter_type, iv1->base)); /* When base has the form iv + 1, if we know iv >= n, then iv + 1 < n only when iv + 1 overflows, i.e. when iv == TYPE_VALUE_MAX. */ @@ -1531,8 +1532,9 @@ number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0, if (integer_zerop (assumptions)) return false; - num = fold_build2 (MINUS_EXPR, niter_type, iv0->base, - wide_int_to_tree (type, min)); + num = fold_build2 (MINUS_EXPR, niter_type, + fold_convert (niter_type, iv0->base), + wide_int_to_tree (niter_type, min)); low = min; if (TREE_CODE (iv0->base) == INTEGER_CST) high = wi::to_wide (iv0->base) + 1; @@ -1546,7 +1548,6 @@ number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0, /* (delta + step - 1) / step */ step = fold_convert (niter_type, step); - num = fold_convert (niter_type, num); num = fold_build2 (PLUS_EXPR, niter_type, num, step); niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, num, step); -- 2.35.3