From patchwork Wed Sep 14 07:43:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp2699038wrt; Wed, 14 Sep 2022 00:44:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR5olaaFKM0rSVD0K03JHHZwZhdNQZf9q/hWTwz0iwRp6m69kOrwzAAWnx3XV3gx7eXLlPJc X-Received: by 2002:a05:6402:1a4d:b0:44e:221d:d1dd with SMTP id bf13-20020a0564021a4d00b0044e221dd1ddmr29911545edb.267.1663141446297; Wed, 14 Sep 2022 00:44:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663141446; cv=none; d=google.com; s=arc-20160816; b=rBhFRHgaq7zUJPWikrKNTmG14lvmg+MzGnKnUCoCqnh8e4pwONRiVt5/LTBsSzlljX tUGzUmWH/sHQYuIRYOpwZMZ4Nyw1qmj3O/PP9AbbPEv7D94Fjaxvc6nqpaxfK6lj+s5n euqM06nCzO75oKg14dsiCx3sLBGMK9r5jJUJFrrNWNF2yle0qBLqBIceh3vbCD4gcDtj +HdaYZv49uBa9vdZ78jOer8Vv8s8EDUeOXzTzbWyZdsQlLG0sv1a08RysgG52ZeHaKaG ZHhigBObxSkyhq2P7G2R20NGz+sbLw4+GgvrND0bh31W2SoJZGyzRLmqAXZuzRJOK35o WFvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hd5oVTkJ08Zjv9FQx4epsIiOPfHXtt8z4N8dj+eJsWE=; b=YquongeDnlD5D7ZPzLd5AF+rv53Pw7wbLfQYl95gKUNFNvxMyuSWev3I0hsOstM3ex BGnqtEXHAm8fRNCbKxMM7X5cKpd766f43yoWIc15AJl1YCVTKulFch1pxGmwcez0sKsN 7gnYyDWXZpvuszVsMG+dJWIkSS1y/mPRcJh92ceDUHCq+O2k8Wyd2UA9/yqtg9Dnj/rF tZ8GcexJT6KmYCpoa6KOEAfR2eSZpSViGysYEHHlwsKF97enlrcW4z+S5ocZVvIgHcZj T7WsC1oTJHltK3vpEN1lJS4xAZKgZdjbhy/Ci0uwZu2umxkbN5l0fCKGFevGpYRaf+oB 8NPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ow5CEIvQ; 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 qo20-20020a170907213400b0073105e9fb53si9763130ejb.855.2022.09.14.00.44.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 00:44:06 -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=ow5CEIvQ; 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 E5B053850855 for ; Wed, 14 Sep 2022 07:44:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5B053850855 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663141444; bh=hd5oVTkJ08Zjv9FQx4epsIiOPfHXtt8z4N8dj+eJsWE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=ow5CEIvQNl1+iNQA8H/KO31WfwVEc0Z625vo/JwWcycoZqKpNNO/xPFETOJPykWoC KvTRUElirvm9U9ngkx6qg0LSaNQPt6fWD2sVGoY6ixkmTUQDcZss/MHoYkLJCh4zi4 m2ufpzHKVCMZgsSff+ZRCYVUPiGyO0S5q8P9yPFE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 30C833851408 for ; Wed, 14 Sep 2022 07:43:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 30C833851408 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-611-cxOc5nNkMni6iMMTyvBIVg-1; Wed, 14 Sep 2022 03:43:16 -0400 X-MC-Unique: cxOc5nNkMni6iMMTyvBIVg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B79AD1818802; Wed, 14 Sep 2022 07:43:15 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6F7962083A08; Wed, 14 Sep 2022 07:43:15 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 28E7hCMl1975019 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 14 Sep 2022 09:43:13 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 28E7hBgH1975018; Wed, 14 Sep 2022 09:43:11 +0200 Date: Wed, 14 Sep 2022 09:43:11 +0200 To: Richard Biener Subject: [PATCH] Disallow pointer operands for |, ^ and partly & [PR106878] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org 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?1743930205278726989?= X-GMAIL-MSGID: =?utf-8?q?1743930205278726989?= Hi! My change to match.pd (that added the two simplifications this patch touches) results in more |/^/& assignments with pointer arguments, but since r12-1608 we reject pointer operands for BIT_NOT_EXPR. Disallowing them for BIT_NOT_EXPR and allowing for BIT_{IOR,XOR,AND}_EXPR leads to a match.pd maintainance nightmare (see one of the patches in the PR), so either we want to allow pointer operand on BIT_NOT_EXPR (but then we run into issues e.g. with the ranger which expects it can emulate BIT_NOT_EXPR ~X as - 1 - X which doesn't work for pointers which don't support MINUS_EXPR), or the following patch disallows pointer arguments for all of BIT_{IOR,XOR,AND}_EXPR with the exception of BIT_AND_EXPR with INTEGER_CST last operand (for simpler pointer realignment). I had to tweak one reassoc optimization and the two match.pd simplifications. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? P.S.: I know it would be better for the verifiers to have positive set of types it wants to allow for each operation, but I have no idea what exactly we use there right now. 2022-09-14 Jakub Jelinek PR tree-optimization/106878 * tree-cfg.cc (verify_gimple_assign_binary): Disallow pointer, reference or OFFSET_TYPE BIT_IOR_EXPR, BIT_XOR_EXPR or, unless the second argument is INTEGER_CST, BIT_AND_EXPR. * match.pd ((type) X op CST -> (type) (X op ((type-x) CST)), (type) (((type2) X) op Y) -> (X op (type) Y)): Punt for POINTER_TYPE_P or OFFSET_TYPE. * tree-ssa-reassoc.cc (optimize_range_tests_cmp_bitwise): For pointers cast them to pointer sized integers first. * gcc.c-torture/compile/pr106878.c: New test. Jakub --- gcc/tree-cfg.cc.jj 2022-09-08 20:22:07.788184491 +0200 +++ gcc/tree-cfg.cc 2022-09-13 09:28:53.563243962 +0200 @@ -4167,6 +4167,8 @@ verify_gimple_assign_binary (gassign *st case ROUND_MOD_EXPR: case RDIV_EXPR: case EXACT_DIV_EXPR: + case BIT_IOR_EXPR: + case BIT_XOR_EXPR: /* Disallow pointer and offset types for many of the binary gimple. */ if (POINTER_TYPE_P (lhs_type) || TREE_CODE (lhs_type) == OFFSET_TYPE) @@ -4182,9 +4184,23 @@ verify_gimple_assign_binary (gassign *st case MIN_EXPR: case MAX_EXPR: - case BIT_IOR_EXPR: - case BIT_XOR_EXPR: + /* Continue with generic binary expression handling. */ + break; + case BIT_AND_EXPR: + if (POINTER_TYPE_P (lhs_type) + && TREE_CODE (rhs2) == INTEGER_CST) + break; + /* Disallow pointer and offset types for many of the binary gimple. */ + if (POINTER_TYPE_P (lhs_type) + || TREE_CODE (lhs_type) == OFFSET_TYPE) + { + error ("invalid types for %qs", code_name); + debug_generic_expr (lhs_type); + debug_generic_expr (rhs1_type); + debug_generic_expr (rhs2_type); + return true; + } /* Continue with generic binary expression handling. */ break; --- gcc/match.pd.jj 2022-09-08 20:22:00.836276502 +0200 +++ gcc/match.pd 2022-09-13 10:21:04.567853941 +0200 @@ -1763,6 +1763,8 @@ (define_operator_list SYNC_FETCH_AND_AND && (int_fits_type_p (@1, TREE_TYPE (@0)) || tree_nop_conversion_p (TREE_TYPE (@0), type))) || types_match (@0, @1)) + && !POINTER_TYPE_P (TREE_TYPE (@0)) + && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE /* ??? This transform conflicts with fold-const.cc doing Convert (T)(x & c) into (T)x & (T)c, if c is an integer constants (if x has signed type, the sign bit cannot be set @@ -1799,7 +1801,9 @@ (define_operator_list SYNC_FETCH_AND_AND (if (GIMPLE && TREE_CODE (@1) != INTEGER_CST && tree_nop_conversion_p (type, TREE_TYPE (@2)) - && types_match (type, @0)) + && types_match (type, @0) + && !POINTER_TYPE_P (TREE_TYPE (@0)) + && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE) (bitop @0 (convert @1))))) (for bitop (bit_and bit_ior) --- gcc/tree-ssa-reassoc.cc.jj 2022-06-28 13:03:31.292684917 +0200 +++ gcc/tree-ssa-reassoc.cc 2022-09-13 10:18:27.466085947 +0200 @@ -3608,10 +3608,14 @@ optimize_range_tests_cmp_bitwise (enum t tree type2 = NULL_TREE; bool strict_overflow_p = false; candidates.truncate (0); + if (POINTER_TYPE_P (type1)) + type1 = pointer_sized_int_node; for (j = i; j; j = chains[j - 1]) { tree type = TREE_TYPE (ranges[j - 1].exp); strict_overflow_p |= ranges[j - 1].strict_overflow_p; + if (POINTER_TYPE_P (type)) + type = pointer_sized_int_node; if ((b % 4) == 3) { /* For the signed < 0 cases, the types should be @@ -3642,6 +3646,8 @@ optimize_range_tests_cmp_bitwise (enum t tree type = TREE_TYPE (ranges[j - 1].exp); if (j == k) continue; + if (POINTER_TYPE_P (type)) + type = pointer_sized_int_node; if ((b % 4) == 3) { if (!useless_type_conversion_p (type1, type)) @@ -3671,7 +3677,7 @@ optimize_range_tests_cmp_bitwise (enum t op = r->exp; continue; } - if (id == l) + if (id == l || POINTER_TYPE_P (TREE_TYPE (op))) { code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR; g = gimple_build_assign (make_ssa_name (type1), code, op); @@ -3695,6 +3701,14 @@ optimize_range_tests_cmp_bitwise (enum t gimple_seq_add_stmt_without_update (&seq, g); op = gimple_assign_lhs (g); } + type1 = TREE_TYPE (ranges[k - 1].exp); + if (POINTER_TYPE_P (type1)) + { + gimple *g + = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op); + gimple_seq_add_stmt_without_update (&seq, g); + op = gimple_assign_lhs (g); + } candidates.pop (); if (update_range_test (&ranges[k - 1], NULL, candidates.address (), candidates.length (), opcode, ops, op, --- gcc/testsuite/gcc.c-torture/compile/pr106878.c.jj 2022-09-13 10:21:04.567853941 +0200 +++ gcc/testsuite/gcc.c-torture/compile/pr106878.c 2022-09-13 10:21:04.567853941 +0200 @@ -0,0 +1,15 @@ +/* PR tree-optimization/106878 */ + +typedef __INTPTR_TYPE__ intptr_t; +typedef __UINTPTR_TYPE__ uintptr_t; +int a; + +int +foo (const int *c) +{ + uintptr_t d = ((intptr_t) c | (intptr_t) &a) & 65535 << 16; + intptr_t e = (intptr_t) c; + if (d != (e & 65535 << 16)) + return 1; + return 0; +}