From patchwork Mon Dec 19 15:36:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 34614 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2457989wrn; Mon, 19 Dec 2022 07:36:56 -0800 (PST) X-Google-Smtp-Source: AMrXdXs5eM3BO9u2r+GCvPWzOwEOKAMoqZq7uQoVIVtdP06nuyFre18Sixim6aP1AGMZpi66x3CF X-Received: by 2002:a17:906:274b:b0:7c1:7183:2d32 with SMTP id a11-20020a170906274b00b007c171832d32mr9072229ejd.56.1671464216574; Mon, 19 Dec 2022 07:36:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671464216; cv=none; d=google.com; s=arc-20160816; b=t6H9EyMTYxf8jvW4deQyVmuaLXzc29CP7A6+2CqG6f1dJVZgcoBmBIFd+y4a/lqEvL FrcSoVRyRb/F0G1lqzZFlg90VLqurHRr6+GIml8aTTkBdx90XUPS1clSPJ1EokNlBSIe HcESbAxgLxXgwUDbqS2dgFrkdcZPYyChRSLAxIfPGRoSyR0EHjUU8Q1M+zWqRP66ex4V XHTk+9/IPWY5sfxoxJhXM2TsJ915YcqRE3OkfhbiWc2/3B9eQMynZ1qigRhAoSFInBTg xSfqE09LCvfexkRia4oh5Aan3teCI57401dBshzIGiTYapmq8nh5xUiozInzeCbChmdZ ok7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=50ebXAOf+cLomP3NYrySEfNgRjJfCyittGoE+pbXsbg=; b=Edtz978DXAWAilJp4fNoPn1ZYg9TonrPz4PojZ1qQOOXPt4OIcaK+Q0qVeqDecJbku 2XHjezKZ2nAvORss7/1jGbv+HLp7vltar6wYdLOXTPhp4/j6nIHZtZECzmtqqNT281xn aoqZC/XEnTBXf6hk2AVpPf/8SvX8y493fowJO6ATb4ObmIkkdTxNF65aCsLYUyI7Z6HA tpMsc5jg38RBSiyojEpy5b9YE07CJenY6N2u4dtAWlpra6z4mc/+f3QkaC3iuusY0yGK CEHctG6GRhY2FOs751R5Y2diW3KinO9H6EMtaEOOc3HuN+P4fT/bST5JsON8MQkT/Aj1 ra4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=oWuuYTMV; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u18-20020aa7d992000000b004619acbc70fsi7711367eds.505.2022.12.19.07.36.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 07:36:56 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=oWuuYTMV; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 25281385843A for ; Mon, 19 Dec 2022 15:36:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25281385843A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1671464213; bh=50ebXAOf+cLomP3NYrySEfNgRjJfCyittGoE+pbXsbg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oWuuYTMVnc+BPsEGEVG3BZhuAMbDHGSqLqb+irv6uv+9sbI3CwKRsCcAsdZQkcl/0 5Wc3I/fCsyY+TW8GX5GnrUHNg/0gJACqO8eYoR3jsGaVbT2J4a1tRrbtUMj9Jkan35 GUBI8Iaqsoe/j+dZW1Pj1VWeW5nC126DpyU4if1Q= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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) 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) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/108164 - undefined overflow with IV vectorization MIME-Version: 1.0 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 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-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752657262268092707?= X-GMAIL-MSGID: =?utf-8?q?1752657262268092707?= 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)