From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 059443858D1E for ; Mon, 19 Dec 2022 15:36:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 059443858D1E 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 imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id F31B03801C for ; Mon, 19 Dec 2022 15:36:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1671464167; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=50ebXAOf+cLomP3NYrySEfNgRjJfCyittGoE+pbXsbg=; b=pWDiIvYJvfrnc1WR3lYypcGVHFhEnqLQy719o3kCJ+2S1p/YAxRof3p8Nej3uDpuA7xiYY 0OifAbO14d5WEgTdi15rEuy98zY2J6R1904iGVeScLX+ooto4NJGO0cdq1vGFMqKQ7PpdJ W5CvHMimN/prbxWTNAMlciXrI4NAMf0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1671464167; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=50ebXAOf+cLomP3NYrySEfNgRjJfCyittGoE+pbXsbg=; b=6BEWol+tFGRdUPrwej/Q0po4HcFya3+i4FFHpp655tyumVPPccGYeClrG8nYKVgwwwqWxP CD3XRqdDB+oEOqCQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E229F13498 for ; Mon, 19 Dec 2022 15:36:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id +LEjNueEoGMzXQAAMHmgww (envelope-from ) for ; Mon, 19 Dec 2022 15:36:07 +0000 Date: Mon, 19 Dec 2022 16:36:07 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/108164 - undefined overflow with IV vectorization MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Message-Id: <20221219153607.E229F13498@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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: vect_update_ivs_after_vectorizer can end up emitting a signed IV update when the loop body performed an unsigned computation. The following makes sure to perform that update in the type of the loop update type to avoid undefined behavior on overflow. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/108164 * tree-vect-loop-manip.cc (vect_update_ivs_after_vectorizer): Perform vect_step_op_add update in the appropriate type. * gcc.dg/pr108164.c: New testcase. --- gcc/testsuite/gcc.dg/pr108164.c | 19 +++++++++++++++++++ gcc/tree-vect-loop-manip.cc | 12 +++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr108164.c diff --git a/gcc/testsuite/gcc.dg/pr108164.c b/gcc/testsuite/gcc.dg/pr108164.c new file mode 100644 index 00000000000..d76d557876e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108164.c @@ -0,0 +1,19 @@ +/* { dg-do run } */ +/* { dg-options "-O3 -fno-tree-dce" } */ + +int a, b, c; +int main() +{ + int e = -1; + short f = -1; + for (; c < 1; c++) + while (f >= e) + f++; + for (; a < 2; a++) { + short g = ~(~b | ~f); + int h = -g; + int i = (3 / ~h) / ~b; + b = i; + } + return 0; +} diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc index 1d96130c985..5ec739ed218 100644 --- a/gcc/tree-vect-loop-manip.cc +++ b/gcc/tree-vect-loop-manip.cc @@ -1576,14 +1576,16 @@ vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo, if (induction_type == vect_step_op_add) { - off = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr), - fold_convert (TREE_TYPE (step_expr), niters), - step_expr); + tree stype = TREE_TYPE (step_expr); + off = fold_build2 (MULT_EXPR, stype, + fold_convert (stype, niters), step_expr); if (POINTER_TYPE_P (type)) ni = fold_build_pointer_plus (init_expr, off); else - ni = fold_build2 (PLUS_EXPR, type, - init_expr, fold_convert (type, off)); + ni = fold_convert (type, + fold_build2 (PLUS_EXPR, stype, + fold_convert (stype, init_expr), + off)); } /* Don't bother call vect_peel_nonlinear_iv_init. */ else if (induction_type == vect_step_op_neg) -- 2.35.3