From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from bird.elm.relay.mailchannels.net (bird.elm.relay.mailchannels.net [23.83.212.17]) by sourceware.org (Postfix) with ESMTPS id 5BA443858404 for ; Tue, 19 Oct 2021 07:38:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5BA443858404 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 3ACF6E025E; Tue, 19 Oct 2021 07:38:42 +0000 (UTC) Received: from pdx1-sub0-mail-a17.g.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 7E92AE02D0; Tue, 19 Oct 2021 07:38:41 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a17.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.112.83.63 (trex/6.4.3); Tue, 19 Oct 2021 07:38:42 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Keen-Lettuce: 4f8151c76e8bebac_1634629121820_395168335 X-MC-Loop-Signature: 1634629121820:2588898857 X-MC-Ingress-Time: 1634629121819 Received: from pdx1-sub0-mail-a17.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a17.g.dreamhost.com (Postfix) with ESMTP id 0BE2783497; Tue, 19 Oct 2021 00:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gotplt.org; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=gotplt.org; bh=l40jc4e86CtEWh03gpv ZQeCxvjM=; b=gAyVRR5uWremHU3+l6nTv8WXpMgZ4Zb64RiQqvWE+PrKXV6MbAY rL6m0DanbSF+HHCAqMM7TMORzCSoIqyyPpbzPuTx817uBUg/OXFX9eWubR1C0szn 9wGjT7VVTYOLk9w/dHml9DX5lCZTehJ5nbJ7qSGa0Whv/tpx1EuUddTE= Received: from rhbox.redhat.com (unknown [1.186.123.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a17.g.dreamhost.com (Postfix) with ESMTPSA id 59BDA8348A; Tue, 19 Oct 2021 00:38:38 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a17 From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com Subject: [PATCH v2] tree-object-size: Make unknown a computation Date: Tue, 19 Oct 2021 13:08:30 +0530 Message-Id: <20211019073830.303140-1-siddhesh@gotplt.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211019065813.GJ304296@tucnak> References: <20211019065813.GJ304296@tucnak> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3037.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 19 Oct 2021 07:38:47 -0000 Compute the unknown size value as a function of the min/max bit of object_size_type. This transforms into a neat little branchless sequence on x86_64: movl %edi, %eax sarl %eax xorl $1, %eax negl %eax cltq which should be faster than loading the value from memory. A quick unscientific test using `time make check-gcc RUNTESTFLAGS=3D"dg.exp=3Dbuiltin*"` shaves about half a second off execution time with this. Also simplify implementation of unknown_object_size. gcc/ChangeLog: * tree-object-size.c (unknown): Make into a function. Adjust all uses. (unknown_object_size): Simplify implementation. Signed-off-by: Siddhesh Poyarekar --- gcc/tree-object-size.c | 100 ++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/gcc/tree-object-size.c b/gcc/tree-object-size.c index 46a976dfe10..4334e05ef70 100644 --- a/gcc/tree-object-size.c +++ b/gcc/tree-object-size.c @@ -45,13 +45,6 @@ struct object_size_info unsigned int *stack, *tos; }; =20 -static const unsigned HOST_WIDE_INT unknown[4] =3D { - HOST_WIDE_INT_M1U, - HOST_WIDE_INT_M1U, - 0, - 0 -}; - static tree compute_object_offset (const_tree, const_tree); static bool addr_object_size (struct object_size_info *, const_tree, int, unsigned HOST_WIDE_INT *); @@ -82,6 +75,11 @@ static bitmap computed[4]; /* Maximum value of offset we consider to be addition. */ static unsigned HOST_WIDE_INT offset_limit; =20 +static inline unsigned HOST_WIDE_INT +unknown (int object_size_type) +{ + return ((unsigned HOST_WIDE_INT) -((object_size_type >> 1) ^ 1)); +} =20 /* Initialize OFFSET_LIMIT variable. */ static void @@ -204,7 +202,7 @@ decl_init_size (tree decl, bool min) =20 /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR. OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. - If unknown, return unknown[object_size_type]. */ + If unknown, return unknown (object_size_type). */ =20 static bool addr_object_size (struct object_size_info *osi, const_tree ptr, @@ -216,7 +214,7 @@ addr_object_size (struct object_size_info *osi, const= _tree ptr, =20 /* Set to unknown and overwrite just before returning if the size could be determined. */ - *psize =3D unknown[object_size_type]; + *psize =3D unknown (object_size_type); =20 pt_var =3D TREE_OPERAND (ptr, 0); while (handled_component_p (pt_var)) @@ -244,9 +242,9 @@ addr_object_size (struct object_size_info *osi, const= _tree ptr, SSA_NAME_VERSION (var))) sz =3D object_sizes[object_size_type][SSA_NAME_VERSION (var)]; else - sz =3D unknown[object_size_type]; + sz =3D unknown (object_size_type); } - if (sz !=3D unknown[object_size_type]) + if (sz !=3D unknown (object_size_type)) { offset_int mem_offset; if (mem_ref_offset (pt_var).is_constant (&mem_offset)) @@ -257,13 +255,13 @@ addr_object_size (struct object_size_info *osi, con= st_tree ptr, else if (wi::fits_uhwi_p (dsz)) sz =3D dsz.to_uhwi (); else - sz =3D unknown[object_size_type]; + sz =3D unknown (object_size_type); } else - sz =3D unknown[object_size_type]; + sz =3D unknown (object_size_type); } =20 - if (sz !=3D unknown[object_size_type] && sz < offset_limit) + if (sz !=3D unknown (object_size_type) && sz < offset_limit) pt_var_size =3D size_int (sz); } else if (DECL_P (pt_var)) @@ -445,7 +443,7 @@ addr_object_size (struct object_size_info *osi, const= _tree ptr, /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL. Handles calls to functions declared with attribute alloc_size. OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. - If unknown, return unknown[object_size_type]. */ + If unknown, return unknown (object_size_type). */ =20 static unsigned HOST_WIDE_INT alloc_object_size (const gcall *call, int object_size_type) @@ -459,7 +457,7 @@ alloc_object_size (const gcall *call, int object_size= _type) calltype =3D gimple_call_fntype (call); =20 if (!calltype) - return unknown[object_size_type]; + return unknown (object_size_type); =20 /* Set to positions of alloc_size arguments. */ int arg1 =3D -1, arg2 =3D -1; @@ -479,7 +477,7 @@ alloc_object_size (const gcall *call, int object_size= _type) || (arg2 >=3D 0 && (arg2 >=3D (int)gimple_call_num_args (call) || TREE_CODE (gimple_call_arg (call, arg2)) !=3D INTEGER_CST))) - return unknown[object_size_type]; + return unknown (object_size_type); =20 tree bytes =3D NULL_TREE; if (arg2 >=3D 0) @@ -492,7 +490,7 @@ alloc_object_size (const gcall *call, int object_size= _type) if (bytes && tree_fits_uhwi_p (bytes)) return tree_to_uhwi (bytes); =20 - return unknown[object_size_type]; + return unknown (object_size_type); } =20 =20 @@ -534,7 +532,7 @@ compute_builtin_object_size (tree ptr, int object_siz= e_type, =20 /* Set to unknown and overwrite just before returning if the size could be determined. */ - *psize =3D unknown[object_size_type]; + *psize =3D unknown (object_size_type); =20 if (! offset_limit) init_offset_limit (); @@ -674,7 +672,7 @@ compute_builtin_object_size (tree ptr, int object_siz= e_type, { EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi) if (object_sizes[object_size_type][i] - !=3D unknown[object_size_type]) + !=3D unknown (object_size_type)) { print_generic_expr (dump_file, ssa_name (i), dump_flags); @@ -692,7 +690,7 @@ compute_builtin_object_size (tree ptr, int object_siz= e_type, } =20 *psize =3D object_sizes[object_size_type][SSA_NAME_VERSION (ptr)]; - return *psize !=3D unknown[object_size_type]; + return *psize !=3D unknown (object_size_type); } =20 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_N= AME. */ @@ -705,7 +703,7 @@ expr_object_size (struct object_size_info *osi, tree = ptr, tree value) unsigned HOST_WIDE_INT bytes; =20 gcc_assert (object_sizes[object_size_type][varno] - !=3D unknown[object_size_type]); + !=3D unknown (object_size_type)); gcc_assert (osi->pass =3D=3D 0); =20 if (TREE_CODE (value) =3D=3D WITH_SIZE_EXPR) @@ -718,7 +716,7 @@ expr_object_size (struct object_size_info *osi, tree = ptr, tree value) if (TREE_CODE (value) =3D=3D ADDR_EXPR) addr_object_size (osi, value, object_size_type, &bytes); else - bytes =3D unknown[object_size_type]; + bytes =3D unknown (object_size_type); =20 if ((object_size_type & 2) =3D=3D 0) { @@ -745,7 +743,7 @@ call_object_size (struct object_size_info *osi, tree = ptr, gcall *call) gcc_assert (is_gimple_call (call)); =20 gcc_assert (object_sizes[object_size_type][varno] - !=3D unknown[object_size_type]); + !=3D unknown (object_size_type)); gcc_assert (osi->pass =3D=3D 0); =20 bytes =3D alloc_object_size (call, object_size_type); @@ -770,24 +768,12 @@ unknown_object_size (struct object_size_info *osi, = tree ptr) { int object_size_type =3D osi->object_size_type; unsigned int varno =3D SSA_NAME_VERSION (ptr); - unsigned HOST_WIDE_INT bytes; + unsigned HOST_WIDE_INT bytes =3D unknown (object_size_type); =20 - gcc_assert (object_sizes[object_size_type][varno] - !=3D unknown[object_size_type]); - gcc_assert (osi->pass =3D=3D 0); + gcc_checking_assert (object_sizes[object_size_type][varno] !=3D bytes)= ; + gcc_checking_assert (osi->pass =3D=3D 0); =20 - bytes =3D unknown[object_size_type]; - - if ((object_size_type & 2) =3D=3D 0) - { - if (object_sizes[object_size_type][varno] < bytes) - object_sizes[object_size_type][varno] =3D bytes; - } - else - { - if (object_sizes[object_size_type][varno] > bytes) - object_sizes[object_size_type][varno] =3D bytes; - } + object_sizes[object_size_type][varno] =3D bytes; } =20 =20 @@ -802,11 +788,11 @@ merge_object_sizes (struct object_size_info *osi, t= ree dest, tree orig, unsigned int varno =3D SSA_NAME_VERSION (dest); unsigned HOST_WIDE_INT orig_bytes; =20 - if (object_sizes[object_size_type][varno] =3D=3D unknown[object_size_t= ype]) + if (object_sizes[object_size_type][varno] =3D=3D unknown (object_size_= type)) return false; if (offset >=3D offset_limit) { - object_sizes[object_size_type][varno] =3D unknown[object_size_type= ]; + object_sizes[object_size_type][varno] =3D unknown (object_size_typ= e); return false; } =20 @@ -814,7 +800,7 @@ merge_object_sizes (struct object_size_info *osi, tre= e dest, tree orig, collect_object_sizes_for (osi, orig); =20 orig_bytes =3D object_sizes[object_size_type][SSA_NAME_VERSION (orig)]= ; - if (orig_bytes !=3D unknown[object_size_type]) + if (orig_bytes !=3D unknown (object_size_type)) orig_bytes =3D (offset > orig_bytes) ? HOST_WIDE_INT_0U : orig_bytes - offset; =20 @@ -865,7 +851,7 @@ plus_stmt_object_size (struct object_size_info *osi, = tree var, gimple *stmt) else gcc_unreachable (); =20 - if (object_sizes[object_size_type][varno] =3D=3D unknown[object_size_t= ype]) + if (object_sizes[object_size_type][varno] =3D=3D unknown (object_size_= type)) return false; =20 /* Handle PTR + OFFSET here. */ @@ -874,7 +860,7 @@ plus_stmt_object_size (struct object_size_info *osi, = tree var, gimple *stmt) || TREE_CODE (op0) =3D=3D ADDR_EXPR)) { if (! tree_fits_uhwi_p (op1)) - bytes =3D unknown[object_size_type]; + bytes =3D unknown (object_size_type); else if (TREE_CODE (op0) =3D=3D SSA_NAME) return merge_object_sizes (osi, var, op0, tree_to_uhwi (op1)); else @@ -883,10 +869,10 @@ plus_stmt_object_size (struct object_size_info *osi= , tree var, gimple *stmt) =20 /* op0 will be ADDR_EXPR here. */ addr_object_size (osi, op0, object_size_type, &bytes); - if (bytes =3D=3D unknown[object_size_type]) + if (bytes =3D=3D unknown (object_size_type)) ; else if (off > offset_limit) - bytes =3D unknown[object_size_type]; + bytes =3D unknown (object_size_type); else if (off > bytes) bytes =3D 0; else @@ -894,7 +880,7 @@ plus_stmt_object_size (struct object_size_info *osi, = tree var, gimple *stmt) } } else - bytes =3D unknown[object_size_type]; + bytes =3D unknown (object_size_type); =20 if ((object_size_type & 2) =3D=3D 0) { @@ -924,7 +910,7 @@ cond_expr_object_size (struct object_size_info *osi, = tree var, gimple *stmt) =20 gcc_assert (gimple_assign_rhs_code (stmt) =3D=3D COND_EXPR); =20 - if (object_sizes[object_size_type][varno] =3D=3D unknown[object_size_t= ype]) + if (object_sizes[object_size_type][varno] =3D=3D unknown (object_size_= type)) return false; =20 then_ =3D gimple_assign_rhs2 (stmt); @@ -935,7 +921,7 @@ cond_expr_object_size (struct object_size_info *osi, = tree var, gimple *stmt) else expr_object_size (osi, var, then_); =20 - if (object_sizes[object_size_type][varno] =3D=3D unknown[object_size_t= ype]) + if (object_sizes[object_size_type][varno] =3D=3D unknown (object_size_= type)) return reexamine; =20 if (TREE_CODE (else_) =3D=3D SSA_NAME) @@ -956,9 +942,9 @@ cond_expr_object_size (struct object_size_info *osi, = tree var, gimple *stmt) object size is object size of the first operand minus the constant. If the constant is bigger than the number of remaining bytes until th= e end of the object, object size is 0, but if it is instead a pointer - subtraction, object size is unknown[object_size_type]. + subtraction, object size is unknown (object_size_type). To differentiate addition from subtraction, ADDR_EXPR returns - unknown[object_size_type] for all objects bigger than half of the add= ress + unknown (object_size_type) for all objects bigger than half of the ad= dress space, and constants less than half of the address space are consider= ed addition, while bigger constants subtraction. For a memcpy like GIMPLE_CALL that always returns one of its argument= s, the @@ -1030,7 +1016,7 @@ collect_object_sizes_for (struct object_size_info *= osi, tree var) expr_object_size (osi, var, rhs); } else - unknown_object_size (osi, var); + unknown_object_size (osi, var); break; } =20 @@ -1053,7 +1039,7 @@ collect_object_sizes_for (struct object_size_info *= osi, tree var) =20 case GIMPLE_ASM: /* Pointers defined by __asm__ statements can point anywhere. */ - object_sizes[object_size_type][varno] =3D unknown[object_size_type= ]; + object_sizes[object_size_type][varno] =3D unknown (object_size_typ= e); break; =20 case GIMPLE_NOP: @@ -1062,7 +1048,7 @@ collect_object_sizes_for (struct object_size_info *= osi, tree var) expr_object_size (osi, var, SSA_NAME_VAR (var)); else /* Uninitialized SSA names point nowhere. */ - object_sizes[object_size_type][varno] =3D unknown[object_size_type]; + object_sizes[object_size_type][varno] =3D unknown (object_size_type); break; =20 case GIMPLE_PHI: @@ -1074,7 +1060,7 @@ collect_object_sizes_for (struct object_size_info *= osi, tree var) tree rhs =3D gimple_phi_arg (stmt, i)->def; =20 if (object_sizes[object_size_type][varno] - =3D=3D unknown[object_size_type]) + =3D=3D unknown (object_size_type)) break; =20 if (TREE_CODE (rhs) =3D=3D SSA_NAME) @@ -1090,7 +1076,7 @@ collect_object_sizes_for (struct object_size_info *= osi, tree var) } =20 if (! reexamine - || object_sizes[object_size_type][varno] =3D=3D unknown[object_siz= e_type]) + || object_sizes[object_size_type][varno] =3D=3D unknown (object_si= ze_type)) { bitmap_set_bit (computed[object_size_type], varno); bitmap_clear_bit (osi->reexamine, varno); --=20 2.31.1