From patchwork Fri Oct 20 13:13:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 156102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp1047621vqb; Fri, 20 Oct 2023 06:14:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGTjm8aydpBFLjS464sue0SB7Y/VTpOoy1ixJTb5uN1j+rPPDBOBHrFtgMEsddl4axkVgsz X-Received: by 2002:a05:620a:19a8:b0:76d:83ae:fdcd with SMTP id bm40-20020a05620a19a800b0076d83aefdcdmr1877492qkb.57.1697807661135; Fri, 20 Oct 2023 06:14:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697807661; cv=pass; d=google.com; s=arc-20160816; b=USIj2JGBqKjsAjPniaqg3JDR1EdFAevovvBMblSYuW+oLRJII0fl31j9pNfIXb/LX+ cEErRHcVpSYswQb5ZPsgp1TOqACqnpRFor89qURmH18NoigC21TMcwouHGmYFS90o46K 27UxpWYcbL+3ikMm1DNOJaxvrhwo1h1AHf9ZEpKHDL1x0ngvQCqFTdaxttsC70XkK8PW dcFiCojUprGSKy9mo8clUeEZj0+MvvoA50hDs34CvdO+hU6uuHJsLC6B9yIi8lOtdiQJ KEW11Rb/hzNtvEeSx8oljIhKJzCtbNX2GjanKOFL+EIlAVME+6saCiGWOULwlYw9/MN+ sNCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:mime-version:subject :to:from:date:dkim-signature:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=Izra1JJObGXcObLugYhri1/trKFdgsDzHzrapNoWHDI=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=xn4Vr/3J/VtiQfA1aLtNdaQEy9OPsxHDJFfojcG3E9lOyrhassrbON43JebONBKZgD zuvQlIsE8/wrWFbM+ZcSLLL5KxCKdMenTXTjKk0gNQ45Lepdvi27rWMTckz6W6ssE/Bm 0VI34HB6/uMnF4Qfleasg9z+E2dEEn3ByenEy/jcByRXq5oOM04zviOZgBxbyyKb7aVr TMDcrwXPjeDe1nB0f66htuTaiJJRyL4BYiITW5Qm3sJZb9f48rzkkQUaE7BtgsPGZ9lb 3cht7DCc/bTBvrcsRDuxWV7CNbefBJqgm0CNBh6LRnceMQpr3ruL/zrB062OPUsKVnNV o0sw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=VNlF6ZE8; dkim=neutral (no key) header.i=@suse.de header.b=8jrx0z10; arc=pass (i=1); 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=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id az42-20020a05620a172a00b0077424168fa1si1336357qkb.629.2023.10.20.06.14.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 06:14:21 -0700 (PDT) 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=@suse.de header.s=susede2_rsa header.b=VNlF6ZE8; dkim=neutral (no key) header.i=@suse.de header.b=8jrx0z10; arc=pass (i=1); 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=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E3166385843A for ; Fri, 20 Oct 2023 13:14:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id AD5393858D20 for ; Fri, 20 Oct 2023 13:13:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD5393858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AD5393858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697807636; cv=none; b=jb93CSR8+dT9OqJxpHHSp6fd4CC62QSkSMybdR+ULbRIoPhf787WlMxIiQwmFklLi4gv7kIRluZ5JIUfrQ3OWasXuHsaPsMJRQSknWV2/4DYZBjGsNam3rB2g/+4NdTiVN6ovN8rImli8eIhFD8dQmgDrqHr6kimtbsHZT4DOYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697807636; c=relaxed/simple; bh=2HABOVZ83Kr+vzfjqsjYjuPzyRIwYxlo1oJulFnzy0U=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version: Message-Id; b=KN+O3aj9mhWNSZzjyDaCD/j+ZakGkEZUDyjI6GWYi/nHV08LoL4UQn6WzoghlPjQD0lHsekWkUQzD/vphgDt+kvJjf+iQ673olHJJpaAQv0Ltq66cQIGZ5GYuctCHTQG/HRFjsazFSmT2D3MbQzas4QHivJUeFSJPyDiycvLmmA= ARC-Authentication-Results: i=1; server2.sourceware.org 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-out2.suse.de (Postfix) with ESMTPS id 90F3B1F750 for ; Fri, 20 Oct 2023 13:13:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697807633; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=Izra1JJObGXcObLugYhri1/trKFdgsDzHzrapNoWHDI=; b=VNlF6ZE8ldjXnHA/sSDBXTIIVl7XLDLwTq4F+NBKKs0OFwL3yXaUbRyNVyCYxQ22xPhe6c s0UIpByEb8hupJm6lkuisKNPdFKaC1ekgk+JG2NOnOtw1DGsEY3Oa2l+2nXW7wc8cq5LXW HJ1wzP+oSSQZvZsKY8DbuT3wBiugbls= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697807633; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=Izra1JJObGXcObLugYhri1/trKFdgsDzHzrapNoWHDI=; b=8jrx0z10rjZFdHqrbnObhr6Shvb6gbjZbjMW8s2MGwirK7VJCXcb8aoqmYUIj96Oo6vBUE 2oQQdfooGKNzdQCA== 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 7E572138E2 for ; Fri, 20 Oct 2023 13:13:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id snWoHRF9MmW1HQAAMHmgww (envelope-from ) for ; Fri, 20 Oct 2023 13:13:53 +0000 Date: Fri, 20 Oct 2023 15:13:53 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/110243 - IVOPTs introducing undefined overflow MIME-Version: 1.0 Message-Id: <20231020131353.7E572138E2@imap2.suse-dmz.suse.de> Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -7.10 X-Spamd-Result: default: False [-7.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MID_RHS_MATCH_FROMTLD(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-11.2 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780280366023219243 X-GMAIL-MSGID: 1780280366023219243 The following addresses IVOPTs rewriting expressions in its strip_offset without caring for definedness of overflow. Rather than the earlier attempt of just using the proper split_constant_offset from data-ref analysis the following adjusts IVOPTs helper trying to minimize changes from this fix, possibly easing backports. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/110243 PR tree-optimization/111336 * tree-ssa-loop-ivopts.cc (strip_offset_1): Rewrite operations with undefined behavior on overflow to unsigned arithmetic. * gcc.dg/torture/pr110243.c: New testcase. * gcc.dg/torture/pr111336.c: Likewise. --- gcc/testsuite/gcc.dg/torture/pr110243.c | 22 ++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr111336.c | 25 ++++++++++++++++++++ gcc/tree-ssa-loop-ivopts.cc | 31 ++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr110243.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr111336.c diff --git a/gcc/testsuite/gcc.dg/torture/pr110243.c b/gcc/testsuite/gcc.dg/torture/pr110243.c new file mode 100644 index 00000000000..07dffd95d4d --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr110243.c @@ -0,0 +1,22 @@ +/* { dg-do run } */ +/* { dg-require-effective-target lp64 } */ + +#define X 1100000000 +unsigned char a; +long b = X; +int c[9][1]; +unsigned d; +static long *e = &b, *f = &b; +int g() { + if (a && a <= '9') + return '0'; + if (a) + return 10; + return -1; +} +int main() { + d = 0; + for (; (int)*f -(X-1) + d < 9; d++) + c[g() + (int)*f + ((int)*e - X) -(X-1) + d] + [0] = 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr111336.c b/gcc/testsuite/gcc.dg/torture/pr111336.c new file mode 100644 index 00000000000..a2fa12258df --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111336.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-require-effective-target lp64 } */ + +extern void abort (void); +int a, b; +long c = 3521733542; +int d[2]; +int e(int f, int g) { + if (f == 0) + return 0; + if (f > 200) + return 0; + if (g) + return 5 * f; + return 0; +} +int main() +{ + int h = 0; + for (; e((int)c + 773233762, c + 60) + 773163185 + h < 2; h++) + d[h] = b; + if (a != 0) + abort (); + return 0; +} diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc index 2c1f0846c88..98e5b3024db 100644 --- a/gcc/tree-ssa-loop-ivopts.cc +++ b/gcc/tree-ssa-loop-ivopts.cc @@ -2829,12 +2829,29 @@ strip_offset_1 (tree expr, bool inside_addr, bool top_compref, else if (integer_zerop (op0)) { if (code == MINUS_EXPR) - expr = fold_build1 (NEGATE_EXPR, type, op1); + { + if (TYPE_OVERFLOW_UNDEFINED (type)) + { + type = unsigned_type_for (type); + op1 = fold_convert (type, op1); + } + expr = fold_build1 (NEGATE_EXPR, type, op1); + } else expr = op1; } else - expr = fold_build2 (code, type, op0, op1); + { + if (TYPE_OVERFLOW_UNDEFINED (type)) + { + type = unsigned_type_for (type); + if (code == POINTER_PLUS_EXPR) + code = PLUS_EXPR; + op0 = fold_convert (type, op0); + op1 = fold_convert (type, op1); + } + expr = fold_build2 (code, type, op0, op1); + } return fold_convert (orig_type, expr); @@ -2852,7 +2869,15 @@ strip_offset_1 (tree expr, bool inside_addr, bool top_compref, if (integer_zerop (op0)) expr = op0; else - expr = fold_build2 (MULT_EXPR, type, op0, op1); + { + if (TYPE_OVERFLOW_UNDEFINED (type)) + { + type = unsigned_type_for (type); + op0 = fold_convert (type, op0); + op1 = fold_convert (type, op1); + } + expr = fold_build2 (MULT_EXPR, type, op0, op1); + } return fold_convert (orig_type, expr);