From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3078a3f3b5fso577852f8f.0 for ; Wed, 24 May 2023 02:55:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1684922149; x=1687514149; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=QU6N2oZoEmMt6GJrTL1ULkyMlbdzUMLqX6vV35X6KWM=; b=SSls6i1Ei+4DBJYDVW8n+2+hO0GkBjz+XfxVvUSUoTBYHZXZXJvWkcm7myh32fCOZ6 kKocpwEk/2NNiPLP3FqYIhRIuiDuAVrAx22GHYjKEQp1up9PsnLsVsmCs6NrlztYnh66 CbxxT8sLmwEnXQdy10qX944/YeCG4ofKvo1rha8AwZ+QZskIWymIA8YQe3twx+wczOHx lZZwG5+ycUwKCzw7bPwft3JWr1iRWhPXYxZpQSvS5RrtdOG9ty0HMzhxMlRTJgjQjlEO AIDR4DdKos6pixURHNLluKmm2b45l9orVr2aB7HrOX+SDjBJXoVUA+hDQBrFc4gpBU4m 752Q== 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-Google-Smtp-Source: ACHHUZ4eSIf/qnqtEr2SktbXGshGATpIG4Im9THHPDZHkbsc/aDPNNrApgw+0w9dI5ict7VAZl7AqQ== 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) From: Eric Botcazou 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 Content-Type: multipart/mixed; boundary="nextPart3231521.aeNJFYEL58" Content-Transfer-Encoding: 7Bit 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 List-Id: This is a multi-part message in MIME format. --nextPart3231521.aeNJFYEL58 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" 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. -- Eric Botcazou --nextPart3231521.aeNJFYEL58 Content-Disposition: attachment; filename="p.diff" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="UTF-8"; name="p.diff" 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)))) --nextPart3231521.aeNJFYEL58 Content-Disposition: attachment; filename="storage_offset1.ads" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="storage_offset1.ads" -- { dg-do compile } with System.Storage_Elements; use System.Storage_Elements; with System; package Storage_Offset1 is type Rec is record I1, I2 : Integer; end record; type Ptr is access all Rec; R : Ptr := new Rec; Offset : constant Storage_Offset := R.I1'Address - R.I2'Address; end Storage_Offset1; --nextPart3231521.aeNJFYEL58--