From patchwork Wed May 24 09:54:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 98423 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2722249vqo; Wed, 24 May 2023 02:56:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7bCpHnOB4pSSj81R8UKcvrHRHlFxvnGkl9zVU4mH6OGbpJIZmWXPMGOhEr+FVw7VKLKX+6 X-Received: by 2002:a17:907:1609:b0:94f:1c90:cb71 with SMTP id hb9-20020a170907160900b0094f1c90cb71mr19894327ejc.65.1684922198500; Wed, 24 May 2023 02:56:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684922198; cv=none; d=google.com; s=arc-20160816; b=ZUJTO0+TcHebwd1vt/vSnlM0LK+efOQMt+Ay/tZfZp+RexJx91xok8yPVXj0lhBvFA 7se7sxwzP9Jcp1XNCocTVzyQOK/TtkyBi9gF0yhk16owleCTl7qT8zQVAPGU8XEV/sfD K566KRrVl40NW3p+8k2tN5lprqbPLEfiWvr0CabCmeBtO7umkfXLJ4IhzRzaGVyh+dtO tX+a138ORdpBXQjXcsjnr6wz1Wx32TQF1zXIMneinrKRg6xIM7SL91faA8ztG2oqDQig eKNR38QarC7OZY+9rQk6ca1JqMcs3AfWxOaB/XLeGTJ32RY6RU5D45M9oeVW0Q2Nqnaa pZXg== 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 :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=QU6N2oZoEmMt6GJrTL1ULkyMlbdzUMLqX6vV35X6KWM=; b=vfcvnOwotBmCUsnnxyPmBaH/YSEzTfm8DLmHaGJiCV3AE1RPwhOdpCjEfgZT5DpcBD bDGJDIm+eFa71og3Bguww/rdfE7Ddgu4EYIOy5l9gZgDTFNAZf3ollYQYx9oHymgZNC5 v5JdMnh4Ia+p3QwWeySGyut8DnkhT320lVEnoTVttJvcfSM/okwH7YETgPCNzNjBmtA5 RfMhGSTXuZ1jFDXEaeTQbCGMMdUHoV7UltAZFLRrfJ1yTZiTZjLKzIpzGvF7OZX5515/ iOC2wrp4XSbSLaEqN32GDbg/121HWt5YWh5MzE50owcwTmqrGcq8KroHL0bk+OMGzlgr kvpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=T6exWhND; 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 d15-20020a1709067f0f00b0095ece4be6aasi2872361ejr.972.2023.05.24.02.56.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 02:56:38 -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=@gcc.gnu.org header.s=default header.b=T6exWhND; 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 B8E983858431 for ; Wed, 24 May 2023 09:56:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8E983858431 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684922196; bh=QU6N2oZoEmMt6GJrTL1ULkyMlbdzUMLqX6vV35X6KWM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=T6exWhNDxSUfrlap3/qkBMp7jb3NvHeTjpteWtP43HhqZlrsI/F83VaQkh9n1qcou 0uISsh/BjnDUqBr7xFG55LbvmXjB6X8TLiQ38Py8DO3yHb788iAwVbrtrt02pccDkg KuHoV3nYLDwN33INHdN/GYo9yhjeKdDOJECnGmbc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 4520D3858CDB for ; Wed, 24 May 2023 09:55:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4520D3858CDB Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3078a3f3b5fso577852f8f.0 for ; Wed, 24 May 2023 02:55:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684922149; x=1687514149; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QU6N2oZoEmMt6GJrTL1ULkyMlbdzUMLqX6vV35X6KWM=; b=N453AvB7WV6VaC+g+5bcjJvuH0j7gNgsOGAMEBgO/7rciIHa3Q91DHYTJ4n7AdA7Di 7Q+s1ZbQQe5XTtWKGNt/T4gwRsCPSGnWW+WWI+KYuP+p04++W/tLKEKjCfUvOvuOFDxT uJtsiOgNOFxRgQ53Qb1kSEc7g6M8OpKQHBgstyzzJcsJg1U4rMotbevzC71QIZwIEvpI nI9cgZlQ61omSw1KvDDZ8O5FvMnDFwlefTp1TULJz6pWnCDjl8o8yZ+jXSbRX0v9P6mS eueHixj5pgToqIl6CQJelQn6tB0G4Khsb5H1NLwyDHoohE7HQuafbsCAA8cP/wj3QV7F RPuA== X-Gm-Message-State: AC+VfDxUTX4fzOVr0Fc9AKVDZEgKjzCAiWB14C5wV9RM4TjtWtErByCr dajn62jRgFjSxiI+JD8MIQbOo9EbSUedpIQrjSYVjQ== X-Received: by 2002:a5d:63c6:0:b0:306:3381:67fe with SMTP id c6-20020a5d63c6000000b00306338167femr11454460wrw.27.1684922148935; Wed, 24 May 2023 02:55:48 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id l17-20020adfe591000000b003079c402762sm13786592wrm.19.2023.05.24.02.55.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 02:55:48 -0700 (PDT) X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix artificial overflow during GENERIC folding Date: Wed, 24 May 2023 11:54:41 +0200 Message-ID: <2883909.e9J7NaK4W3@fomalhaut> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Eric Botcazou via Gcc-patches From: Eric Botcazou Reply-To: Eric Botcazou 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?1766768979034473007?= X-GMAIL-MSGID: =?utf-8?q?1766768979034473007?= Hi, on the attached testcase, the Ada compiler gives a bogus warning: storage_offset1.ads:16:52: warning: Constraint_Error will be raised at run time [enabled by default] This directly comes from the GENERIC folding setting a bogus TREE_OVERFLOW on an INTEGER_CST during the (T)P - (T)(P + A) -> -(T) A transformation: /* (T)P - (T)(P + A) -> -(T) A */ (simplify (minus (convert? @0) (convert (plus:c @@0 @1))) (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type) && element_precision (type) <= element_precision (TREE_TYPE (@1))) (with { tree utype = unsigned_type_for (type); } (convert (negate (convert:utype @1)))) (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) /* For integer types, if A has a smaller type than T the result depends on the possible overflow in P + A. E.g. T=size_t, A=(unsigned)429497295, P>0. However, if an overflow in P + A would cause undefined behavior, we can assume that there is no overflow. */ || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) (negate (convert @1))))) (simplify (minus (convert @0) (convert (pointer_plus @@0 @1))) (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type) && element_precision (type) <= element_precision (TREE_TYPE (@1))) (with { tree utype = unsigned_type_for (type); } (convert (negate (convert:utype @1)))) (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) /* For pointer types, if the conversion of A to the final type requires a sign- or zero-extension, then we have to punt - it is not defined which one is correct. */ || (POINTER_TYPE_P (TREE_TYPE (@0)) && TREE_CODE (@1) == INTEGER_CST && tree_int_cst_sign_bit (@1) == 0)) (negate (convert @1))))) Ironically enough, this occurs because of the intermediate conversion to an unsigned type which is supposed to hide overflows, but is counter-productive for constants because TREE_OVERFLOW is always set for them, so it ends up setting a bogus TREE_OVERFLOW when converting back to the original type. The fix simply redirects INTEGER_CSTs to the other, direct path without the intermediate conversion to the unsigned type. Tested on x86-64/Linux, OK for the mainline? 2023-05-24 Eric Botcazou * match.pd ((T)P - (T)(P + A) -> -(T) A): Avoid artificial overflow on constants. 2023-05-24 Eric Botcazou * gnat.dg/specs/storage_offset1.ads: New test. diff --git a/gcc/match.pd b/gcc/match.pd index 1fe0559acfb..b9d04dd423b 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3194,6 +3194,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (plus:c @@0 @1))) (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type) + && TREE_CODE (@1) != INTEGER_CST && element_precision (type) <= element_precision (TREE_TYPE (@1))) (with { tree utype = unsigned_type_for (type); } (convert (negate (convert:utype @1)))) @@ -3213,6 +3214,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (pointer_plus @@0 @1))) (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type) + && TREE_CODE (@1) != INTEGER_CST && element_precision (type) <= element_precision (TREE_TYPE (@1))) (with { tree utype = unsigned_type_for (type); } (convert (negate (convert:utype @1))))