From patchwork Thu Feb 15 18:37:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 201702 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp43780dyb; Thu, 15 Feb 2024 10:38:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV0eOx4Fd50d49+bh4OYr7nA1Gzl3aM+VqMK/08MlHtmTH0KXBhe/UlQzV8WBv9bIDCVGxYRYIEzm4kol/y5YziotQ4Fw== X-Google-Smtp-Source: AGHT+IHuqqfF3nlP4UmQ7fHqbEtmuUayFw6N4T/1u335gIupLEBagu6D01sZVfXdSVKAMbm9mgzJ X-Received: by 2002:a05:620a:2902:b0:787:19fd:5b45 with SMTP id m2-20020a05620a290200b0078719fd5b45mr3427847qkp.66.1708022318038; Thu, 15 Feb 2024 10:38:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708022318; cv=pass; d=google.com; s=arc-20160816; b=wptze6ic6E2y9CAKmZjzTmdWoxyvXaoPZqtgtRS1Pf5aChdD6S6SOktGcqt+hGdbup q/Syz5V2csMvn0tYsHZSiv87w8YgC8MZiFzXt9okGjzUMZuNDU88McU2r4KnqDCHjpZR gAcG4bQAVPcTkNvhN8D9Faw1jJtYa0uXK0Ex8sEXOAeazcn3XW9C4nFzNQc/MP+xHtJt 82xvB7pLlYGxcliH7vLvUD3kXjdSGtNHSDiB6vc1b1KkrNkNi4aZHZrYMzcb1WG4s24a K1xFQuSwnLyzj9lM8dLdiycr/OpPXb4wyOr4j1MnkQVzPHIzjNbdnqUKBY/yshD8XX9U VxWA== 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:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=Wuic2/wGcY2pXYSJ222JyZ/1/byoKdyPNotGgpHxswM=; fh=2rASkADSwGVpvDy7jg5DSLYvTiE4uxP+mXuVMyfs8f4=; b=bkGW1E4UenT9IOmzgTyVBLPLToEw2eEw5h6DhKs9lRvfUbNw101Ucx/QJKOmKsLwmZ u7ymGhczQqLiurEs1SHuTgfNHb6Say2FwrpuxC7oCJ7Cs71VjCK1GS44zf2XqfFXjjuA A6xR+E/Xsw0n+KEyHCQuYmZn7aexUfKsG0DZnjRmhjLXWIVbnjAGPrwQ4yQKkw3Ob7oH TgpYm7TbaeXFBiSM3iXEZUkBuQedZ7voiu7eVp9bKyr/jhwm3DaGutGeZ1SeFiljp/+u MXaSVkuVx3/O7imWnEtN7YfeWwT+oz61XFnp07GSBE5V+L13qyK+NM32cyThv8o7jXQA SEqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=drRdQG6E; 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j26-20020ae9c21a000000b007873d2a0ee6si700364qkg.700.2024.02.15.10.38.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 10:38:38 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=drRdQG6E; 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B628B386C5A0 for ; Thu, 15 Feb 2024 18:38:37 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 5F7FA386C5A2 for ; Thu, 15 Feb 2024 18:37:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5F7FA386C5A2 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5F7FA386C5A2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708022265; cv=none; b=fAlxEjiFpJESGsVM1Qbkg0NBO/illlp4pPx/+xy03hWSOzwYzHW+GAWZlntuFlis9TamLJlPNHK0ceVxIUYZk2Zf/RjKQHit3IshPKQ+kCgPlK+G+K6MYAd6t/+u/LDL76EJr3eWalq1Bt4r8NHAGlUjzN75iRQEa11rrMHDTA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708022265; c=relaxed/simple; bh=tBXYfXc29jZdA+DM0TMmKxX0JrXIe5oApA+AUjxqedk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GBBfJ/IbgY3tclG/9P5ATfgMTdqYIjlY1YqMp2PIcormvEcUDoRwIFhGZYEbB27LNFK6W4zcQ2YBifKUjfYMz2ppFExStmSS8ZqzvqjroTyy1XWiZtYrZdoHH6X/0P9sD8l0q4a5HbNJ4AD5bu5+AjHUd/LPR9C8YY9fJi3kVWc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708022262; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Wuic2/wGcY2pXYSJ222JyZ/1/byoKdyPNotGgpHxswM=; b=drRdQG6Ecw0eYd02rts7XHa+rb+meWDs8EtSRJtNcQp8ihfuiFyfAatO+BxjjixJjJ3HI2 BL0kIoOvSUKFHvb5L41qXVQD8O5d362i9igadKr0fr8B5g+lpF+xbRf8p+wtxSIB7uzV9x GtqIjE3wObm44pXn/Zk0029xzDhoW+4= Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-26-erLtzXLcOlO8ydoCxF-2Pw-1; Thu, 15 Feb 2024 13:37:41 -0500 X-MC-Unique: erLtzXLcOlO8ydoCxF-2Pw-1 Received: by mail-oi1-f197.google.com with SMTP id 5614622812f47-3c13db33d54so21084b6e.0 for ; Thu, 15 Feb 2024 10:37:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708022259; x=1708627059; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Wuic2/wGcY2pXYSJ222JyZ/1/byoKdyPNotGgpHxswM=; b=GkQCq66OdOp0g4YIZ5KeMVwvcMkUVNK/a2Jc7HqmPfIqsletluPuBATF/3hg5pw5UY 8Y6P+l3y/SkhPHi/XlEnukMYE5R6X9tK3QoW2tet+GITG1+Y7MAjnVFlIwZ8CsHxDZFH kMdj0Cy3iGMl/MWICEEvzIgxPoWw/0gDfOmDLwdzUQhEiAmuX6yp6Nkmt0Kr/Z8odTqF Wdjqk27AAtviJMuFdmCc83WFZWnYIOzYKV/nEKzyjp/vT0zPPRcDdMGZqcQ5U5DMc+cv uP5IqKJYHVr1G9SXxsqH/UFLi/ie9vQCSJwyIB+FvPKsvyivvg/Yakqiq9P4h7PtBVZW bThw== X-Gm-Message-State: AOJu0YyD4NV78zxbpkayRMfxVJIHGrHsyC0dzPi7S9mVathQlGa5znWP DahbIJnvXz1eXBg4w5dNpPXNj5RcsoDZsb0Px1wg83hidaJ9gEOa0ib6ZuEINPqtPkuSB4a7Oq3 Q9/CsbH4QtkW+oz/zS2tHKv+KHGRyWYEFM9BXWEuHfJ2T757TG9B8awRBoxyr1TzUSbck3l7VKn +Xu7fFxsCeTKAc1nXR4GLjmL/kR2bWAEJB6njT X-Received: by 2002:a05:6808:1688:b0:3c0:4838:a490 with SMTP id bb8-20020a056808168800b003c04838a490mr3271999oib.30.1708022258561; Thu, 15 Feb 2024 10:37:38 -0800 (PST) X-Received: by 2002:a05:6808:1688:b0:3c0:4838:a490 with SMTP id bb8-20020a056808168800b003c04838a490mr3271954oib.30.1708022257734; Thu, 15 Feb 2024 10:37:37 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id qq2-20020a0562142c0200b0068cd1e72c44sm906837qvb.56.2024.02.15.10.37.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 10:37:37 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, nathan@acm.org, Patrick Palka Subject: [PATCH] c++/modules: optimize tree flag streaming Date: Thu, 15 Feb 2024 13:37:34 -0500 Message-ID: <20240215183735.244505-1-ppalka@redhat.com> X-Mailer: git-send-email 2.44.0.rc1.15.g4fc51f00ef MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP, T_FILL_THIS_FORM_SHORT, T_SCC_BODY_TEXT_LINE, URIBL_BLACK 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: 1790991209676689404 X-GMAIL-MSGID: 1790991209676689404 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- One would expect consecutive calls to bytes_in/out::b for streaming adjacent bits, as we do for tree flag streaming, to at least be optimized by the compiler into individual bit operations using statically known bit positions (and ideally merged into larger sized reads/writes). Unfortunately this doesn't happen because the compiler has trouble tracking the values of this->bit_pos and this->bit_val across such calls, likely because the compiler doesn't know 'this' and so it's treated as global memory. This means for each consecutive bit stream operation, bit_pos and bit_val are loaded from memory, checked if buffering is needed, and finally the bit is extracted from bit_val according to the (unknown) bit_pos, even though relative to the previous operation (if we didn't need to buffer) bit_val is unchanged and bit_pos is just 1 larger. This ends up being quite slow, with tree_node_bools taking 10% of time when streaming in parts of the std module. This patch optimizes this by making tracking of bit_pos and bit_val easier for the compiler. Rather than bit_pos and bit_val being members of the (effectively global) bytes_in/out objects, this patch factors out the bit streaming code/state into separate classes bits_in/out that get constructed locally as needed for bit streaming. Since these objects are now clearly local, the compiler can more easily track their values. And since bit streaming is intended to be batched it's natural for these new classes to be RAII-enabled such that the bit stream is flushed upon destruction. In order to make the most of this improved tracking of bit position, this patch changes parts where we conditionally stream a tree flag to unconditionally stream (the flag or a dummy value). That way the number of bits streamed and the respective bit positions are as statically known as reasonably possible. In lang_decl_bools and lang_type_bools we flush the current bit buffer at the start so that subsequent bit positions are statically known. And in core bools, we can add explicit early exits utilizing invariants that the compiler can't figure out itself (e.g. a tree code can't have both TS_TYPE_COMMON and TS_DECL_COMMON, and if a tree code doesn't have TS_DECL_COMMON then it doesn't have TS_DECL_WITH_VIS). Finally if we're streaming fewer than 4 bits, it's more space efficient to stream them as individual bytes rather than as packed bits (due to the 32-bit buffer). This patch also moves the definitions of the relevant streaming classes into anonymous namespaces so that the compiler can make more informed decisions about inlining their member functions. After this patch, compile time for a simple Hello World using the std module is reduced by 7% with a release compiler. The on-disk size of the std module increases by 0.7% (presumably due to the extra flushing done in lang_decl_bools and lang_type_bools). The bit stream out performance isn't improved as much as the stream in due to the spans/lengths instrumentation performed on stream out (which probably should be e.g. removed for release builds?) gcc/cp/ChangeLog: * module.cc (class data): Enclose in an anonymous namespace. (data::calc_crc): Moved from bytes::calc_crc. (class bytes): Remove. Move bit_flush to namespace scope. (class bytes_in): Enclose in an anonymous namespace. Inherit directly from data and adjust accordingly. Move b and bflush members to bits_in. (class bytes_out): As above. Remove is_set static data member. (bit_flush): Moved from class bytes. (struct bits_in): Define. (struct bits_out): Define. (bytes_out::bflush): Moved to bits_out/in. (bytes_in::bflush): Likewise (bytes_in::bfill): Removed. (bytes_out::b): Moved to bits_out/in. (bytes_in::b): Likewise. (class trees_in): Enclose in an anonymous namespace. (class trees_out): Enclose in an anonymous namespace. (trees_out::core_bools): Add bits_out/in parameter and use it. Unconditionally stream a bit for public_flag. Add early exits as appropriate. (trees_out::core_bools): Likewise. (trees_out::lang_decl_bools): Add bits_out/in parameter and use it. Flush the current bit buffer at the start. Unconditionally stream a bit for module_keyed_decls_p. (trees_in::lang_decl_bools): Likewise. (trees_out::lang_type_bools): Add bits_out/in parameter and use it. Flush the current bit buffer at the start. (trees_in::lang_type_bools): Likewise. (trees_out::tree_node_bools): Construct a bits_out object and use/pass it. (trees_in::tree_node_bools): Likewise. (trees_out::decl_value): Stream stray bit values as bytes. (trees_in::decl_value): Likewise. (module_state::write_define): Likewise. (module_state::read_define): Likewise. --- gcc/cp/module.cc | 401 ++++++++++++++++++++++++++--------------------- 1 file changed, 219 insertions(+), 182 deletions(-) diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 0291d456ff5..9477e4f2e35 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -153,9 +153,11 @@ Classes used: data - buffer - bytes - data streamer - bytes_in : bytes - scalar reader - bytes_out : bytes - scalar writer + bytes_in - scalar reader + bytes_out - scalar writer + + bits_in - bit stream reader + bits_out - bit stream writer elf - ELROND format elf_in : elf - ELROND reader @@ -346,6 +348,7 @@ typedef hash_map ptr_int_hash_map; /* Variable length buffer. */ +namespace { class data { public: class allocator { @@ -393,6 +396,8 @@ protected: return res; } + unsigned calc_crc (unsigned) const; + public: void unuse (unsigned count) { @@ -402,6 +407,7 @@ public: public: static allocator simple_memory; }; +} // anon namespace /* The simple data allocator. */ data::allocator data::simple_memory; @@ -447,46 +453,11 @@ data::allocator::shrink (char *ptr) XDELETEVEC (ptr); } -/* Byte streamer base. Buffer with read/write position and smarts - for single bits. */ - -class bytes : public data { -public: - typedef data parent; - -protected: - uint32_t bit_val; /* Bit buffer. */ - unsigned bit_pos; /* Next bit in bit buffer. */ - -public: - bytes () - :parent (), bit_val (0), bit_pos (0) - {} - ~bytes () - { - } - -protected: - unsigned calc_crc (unsigned) const; - -protected: - /* Finish bit packet. Rewind the bytes not used. */ - unsigned bit_flush () - { - gcc_assert (bit_pos); - unsigned bytes = (bit_pos + 7) / 8; - unuse (4 - bytes); - bit_pos = 0; - bit_val = 0; - return bytes; - } -}; - /* Calculate the crc32 of the buffer. Note the CRC is stored in the first 4 bytes, so don't include them. */ unsigned -bytes::calc_crc (unsigned l) const +data::calc_crc (unsigned l) const { return crc32 (0, (unsigned char *)buffer + 4, l - 4); } @@ -495,8 +466,9 @@ class elf_in; /* Byte stream reader. */ -class bytes_in : public bytes { - typedef bytes parent; +namespace { +class bytes_in : public data { + typedef data parent; protected: bool overrun; /* Sticky read-too-much flag. */ @@ -531,7 +503,6 @@ public: if (offset > size) set_overrun (); pos = offset; - bit_pos = bit_val = 0; } public: @@ -572,13 +543,6 @@ public: public: unsigned u32 (); /* Read uncompressed integer. */ -public: - bool b (); /* Read a bool. */ - void bflush (); /* Completed a block of bools. */ - -private: - void bfill (); /* Get the next block of bools. */ - public: int c (); /* Read a char. */ int i (); /* Read a signed int. */ @@ -590,6 +554,7 @@ public: const void *buf (size_t); /* Read a fixed-length buffer. */ cpp_hashnode *cpp_node (); /* Read a cpp node. */ }; +} // anon namespace /* Verify the buffer's CRC is correct. */ @@ -610,8 +575,9 @@ class elf_out; /* Byte stream writer. */ -class bytes_out : public bytes { - typedef bytes parent; +namespace { +class bytes_out : public data { + typedef data parent; public: allocator *memory; /* Obtainer of memory. */ @@ -658,10 +624,6 @@ public: public: void u32 (unsigned); /* Write uncompressed integer. */ -public: - void b (bool); /* Write bool. */ - void bflush (); /* Finish block of bools. */ - public: void c (unsigned char); /* Write unsigned char. */ void i (int); /* Write signed int. */ @@ -694,13 +656,127 @@ protected: /* Instrumentation. */ static unsigned spans[4]; static unsigned lengths[4]; - static int is_set; + friend struct bits_out; +}; +} // anon namespace + +/* Finish bit packet. Rewind the bytes not used. */ +static unsigned +bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos) +{ + gcc_assert (bit_pos); + unsigned bytes = (bit_pos + 7) / 8; + bits.unuse (4 - bytes); + bit_pos = 0; + bit_val = 0; + return bytes; +} + +/* RAII-enabled bit stream reader. Bools are packed into bytes. You + cannot mix bools and non-bools. Use bflush to flush the current stream + of bools on demand. Upon destruction bflush is called. + + When reading, we don't know how many bools we'll read in. So read + 4 bytes-worth, and then rewind when flushing if we didn't need them + all. You can't have a block of bools closer than 4 bytes to the + end of the buffer. */ + +namespace { +struct bits_in { + bytes_in& in; + uint32_t bit_val = 0; + unsigned bit_pos = 0; + + bits_in (bytes_in& in) + : in (in) + { + } + + ~bits_in () + { + bflush (); + } + + bits_in(const bits_in&) = delete; + bits_in& operator=(const bits_in&) = delete; + + /* Completed a block of bools. */ + void bflush () + { + if (bit_pos) + bit_flush (in, bit_val, bit_pos); + } + + /* Read one bit. */ + bool b () + { + if (!bit_pos) + bit_val = in.u32 (); + bool x = (bit_val >> bit_pos) & 1; + bit_pos = (bit_pos + 1) % 32; + return x; + } }; +} // anon namespace + +/* RAII-enabled bit stream writer, counterpart to bits_in. */ + +namespace { +struct bits_out { + bytes_out& out; + uint32_t bit_val = 0; + unsigned bit_pos = 0; + char is_set = -1; + + bits_out (bytes_out& out) + : out (out) + { } + + ~bits_out () + { + bflush (); + } + + bits_out(const bits_out&) = delete; + bits_out& operator=(const bits_out&) = delete; + + /* Completed a block of bools. */ + void bflush () + { + if (bit_pos) + { + out.u32 (bit_val); + out.lengths[2] += bit_flush (out, bit_val, bit_pos); + } + out.spans[2]++; + is_set = -1; + } + + /* Write one bit. + + It may be worth optimizing for most bools being zero. Some kind of + run-length encoding? */ + void b (bool x) + { + if (is_set != x) + { + is_set = x; + out.spans[x]++; + } + out.lengths[x]++; + bit_val |= unsigned (x) << bit_pos++; + if (bit_pos == 32) + { + out.u32 (bit_val); + out.lengths[2] += bit_flush (out, bit_val, bit_pos); + } + } +}; +} // anon namespace /* Instrumentation. */ unsigned bytes_out::spans[4]; unsigned bytes_out::lengths[4]; -int bytes_out::is_set = -1; /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into that pointed to by CRC_PTR. */ @@ -723,73 +799,6 @@ bytes_out::set_crc (unsigned *crc_ptr) } } -/* Finish a set of bools. */ - -void -bytes_out::bflush () -{ - if (bit_pos) - { - u32 (bit_val); - lengths[2] += bit_flush (); - } - spans[2]++; - is_set = -1; -} - -void -bytes_in::bflush () -{ - if (bit_pos) - bit_flush (); -} - -/* When reading, we don't know how many bools we'll read in. So read - 4 bytes-worth, and then rewind when flushing if we didn't need them - all. You can't have a block of bools closer than 4 bytes to the - end of the buffer. */ - -void -bytes_in::bfill () -{ - bit_val = u32 (); -} - -/* Bools are packed into bytes. You cannot mix bools and non-bools. - You must call bflush before emitting another type. So batch your - bools. - - It may be worth optimizing for most bools being zero. Some kind of - run-length encoding? */ - -void -bytes_out::b (bool x) -{ - if (is_set != x) - { - is_set = x; - spans[x]++; - } - lengths[x]++; - bit_val |= unsigned (x) << bit_pos++; - if (bit_pos == 32) - { - u32 (bit_val); - lengths[2] += bit_flush (); - } -} - -bool -bytes_in::b () -{ - if (!bit_pos) - bfill (); - bool v = (bit_val >> bit_pos++) & 1; - if (bit_pos == 32) - bit_flush (); - return v; -} - /* Exactly 4 bytes. Used internally for bool packing and a few other places. We can't simply use uint32_t because (a) alignment and (b) we need little-endian for the bool streaming rewinding to make @@ -2846,6 +2855,7 @@ struct post_process_data { read trees with TREE_VISITED. Thus it's quite safe to have multiple concurrent readers. Which is good, because lazy loading. */ +namespace { class trees_in : public bytes_in { typedef bytes_in parent; @@ -2870,15 +2880,15 @@ private: public: /* Needed for binfo writing */ - bool core_bools (tree); + bool core_bools (tree, bits_in&); private: /* Stream tree_core, lang_decl_specific and lang_type_specific bits. */ bool core_vals (tree); - bool lang_type_bools (tree); + bool lang_type_bools (tree, bits_in&); bool lang_type_vals (tree); - bool lang_decl_bools (tree); + bool lang_decl_bools (tree, bits_in&); bool lang_decl_vals (tree); bool lang_vals (tree); bool tree_node_bools (tree); @@ -2965,6 +2975,7 @@ private: private: void assert_definition (tree, bool installing); }; +} // anon namespace trees_in::trees_in (module_state *state) :parent (), state (state), unused (0) @@ -2982,6 +2993,7 @@ trees_in::~trees_in () } /* Tree stream writer. */ +namespace { class trees_out : public bytes_out { typedef bytes_out parent; @@ -3043,11 +3055,11 @@ public: } private: - void core_bools (tree); + void core_bools (tree, bits_out&); void core_vals (tree); - void lang_type_bools (tree); + void lang_type_bools (tree, bits_out&); void lang_type_vals (tree); - void lang_decl_bools (tree); + void lang_decl_bools (tree, bits_out&); void lang_decl_vals (tree); void lang_vals (tree); void tree_node_bools (tree); @@ -3126,6 +3138,7 @@ private: static unsigned back_ref_count; static unsigned null_count; }; +} // anon namespace /* Instrumentation counters. */ unsigned trees_out::tree_val_count; @@ -5292,9 +5305,9 @@ trees_in::start (unsigned code) /* Read & write the core boolean flags. */ void -trees_out::core_bools (tree t) +trees_out::core_bools (tree t, bits_out& bits) { -#define WB(X) (b (X)) +#define WB(X) (bits.b (X)) tree_code code = TREE_CODE (t); WB (t->base.side_effects_flag); @@ -5314,6 +5327,8 @@ trees_out::core_bools (tree t) if (TREE_CODE_CLASS (code) != tcc_type) /* This is TYPE_CACHED_VALUES_P for types. */ WB (t->base.public_flag); + else + WB (false); WB (t->base.private_flag); WB (t->base.protected_flag); WB (t->base.deprecated_flag); @@ -5327,7 +5342,7 @@ trees_out::core_bools (tree t) case TARGET_MEM_REF: case TREE_VEC: /* These use different base.u fields. */ - break; + return; default: WB (t->base.u.bits.lang_flag_0); @@ -5374,6 +5389,7 @@ trees_out::core_bools (tree t) WB (t->type_common.lang_flag_5); WB (t->type_common.lang_flag_6); WB (t->type_common.typeless_storage); + return; } if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) @@ -5439,6 +5455,8 @@ trees_out::core_bools (tree t) WB (t->decl_common.decl_nonshareable_flag); WB (t->decl_common.decl_not_flexarray); } + else + return; if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) { @@ -5459,6 +5477,8 @@ trees_out::core_bools (tree t) WB (t->decl_with_vis.final); WB (t->decl_with_vis.regdecl_flag); } + else + return; if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL)) { @@ -5485,13 +5505,16 @@ trees_out::core_bools (tree t) WB ((kind >> 0) & 1); WB ((kind >> 1) & 1); } + else + return; #undef WB } bool -trees_in::core_bools (tree t) +trees_in::core_bools (tree t, bits_in& bits) { -#define RB(X) ((X) = b ()) +#define RB(X) ((X) = bits.b ()) + tree_code code = TREE_CODE (t); RB (t->base.side_effects_flag); @@ -5507,6 +5530,8 @@ trees_in::core_bools (tree t) RB (t->base.static_flag); if (TREE_CODE_CLASS (code) != tcc_type) RB (t->base.public_flag); + else + bits.b (); RB (t->base.private_flag); RB (t->base.protected_flag); RB (t->base.deprecated_flag); @@ -5520,7 +5545,7 @@ trees_in::core_bools (tree t) case TARGET_MEM_REF: case TREE_VEC: /* These use different base.u fields. */ - break; + goto done; default: RB (t->base.u.bits.lang_flag_0); @@ -5554,6 +5579,7 @@ trees_in::core_bools (tree t) RB (t->type_common.lang_flag_5); RB (t->type_common.lang_flag_6); RB (t->type_common.typeless_storage); + goto done; } if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) @@ -5584,6 +5610,8 @@ trees_in::core_bools (tree t) RB (t->decl_common.decl_nonshareable_flag); RB (t->decl_common.decl_not_flexarray); } + else + goto done; if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) { @@ -5604,6 +5632,8 @@ trees_in::core_bools (tree t) RB (t->decl_with_vis.final); RB (t->decl_with_vis.regdecl_flag); } + else + goto done; if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL)) { @@ -5627,20 +5657,22 @@ trees_in::core_bools (tree t) /* decl_type is a (misnamed) 2 bit discriminator. */ unsigned kind = 0; - kind |= unsigned (b ()) << 0; - kind |= unsigned (b ()) << 1; + kind |= unsigned (bits.b ()) << 0; + kind |= unsigned (bits.b ()) << 1; t->function_decl.decl_type = function_decl_type (kind); } #undef RB +done: return !get_overrun (); } void -trees_out::lang_decl_bools (tree t) +trees_out::lang_decl_bools (tree t, bits_out& bits) { -#define WB(X) (b (X)) +#define WB(X) (bits.b (X)) const struct lang_decl *lang = DECL_LANG_SPECIFIC (t); + bits.bflush (); WB (lang->u.base.language == lang_cplusplus); WB ((lang->u.base.use_template >> 0) & 1); WB ((lang->u.base.use_template >> 1) & 1); @@ -5664,6 +5696,8 @@ trees_out::lang_decl_bools (tree t) WB (lang->u.base.module_attach_p); if (VAR_OR_FUNCTION_DECL_P (t)) WB (lang->u.base.module_keyed_decls_p); + else + WB (false); switch (lang->u.base.selector) { default: @@ -5714,15 +5748,16 @@ trees_out::lang_decl_bools (tree t) } bool -trees_in::lang_decl_bools (tree t) +trees_in::lang_decl_bools (tree t, bits_in& bits) { -#define RB(X) ((X) = b ()) +#define RB(X) ((X) = bits.b ()) struct lang_decl *lang = DECL_LANG_SPECIFIC (t); - lang->u.base.language = b () ? lang_cplusplus : lang_c; + bits.bflush (); + lang->u.base.language = bits.b () ? lang_cplusplus : lang_c; unsigned v; - v = b () << 0; - v |= b () << 1; + v = bits.b () << 0; + v |= bits.b () << 1; lang->u.base.use_template = v; /* lang->u.base.not_really_extern is not streamed. */ RB (lang->u.base.initialized_in_class); @@ -5738,6 +5773,8 @@ trees_in::lang_decl_bools (tree t) RB (lang->u.base.module_attach_p); if (VAR_OR_FUNCTION_DECL_P (t)) RB (lang->u.base.module_keyed_decls_p); + else + bits.b (); switch (lang->u.base.selector) { default: @@ -5786,11 +5823,12 @@ trees_in::lang_decl_bools (tree t) } void -trees_out::lang_type_bools (tree t) +trees_out::lang_type_bools (tree t, bits_out& bits) { -#define WB(X) (b (X)) +#define WB(X) (bits.b (X)) const struct lang_type *lang = TYPE_LANG_SPECIFIC (t); + bits.bflush (); WB (lang->has_type_conversion); WB (lang->has_copy_ctor); WB (lang->has_default_ctor); @@ -5852,11 +5890,12 @@ trees_out::lang_type_bools (tree t) } bool -trees_in::lang_type_bools (tree t) +trees_in::lang_type_bools (tree t, bits_in& bits) { -#define RB(X) ((X) = b ()) +#define RB(X) ((X) = bits.b ()) struct lang_type *lang = TYPE_LANG_SPECIFIC (t); + bits.bflush (); RB (lang->has_type_conversion); RB (lang->has_copy_ctor); RB (lang->has_default_ctor); @@ -5864,8 +5903,8 @@ trees_in::lang_type_bools (tree t) RB (lang->ref_needs_init); RB (lang->has_const_copy_assign); unsigned v; - v = b () << 0; - v |= b () << 1; + v = bits.b () << 0; + v |= bits.b () << 1; lang->use_template = v; RB (lang->has_mutable); @@ -5877,8 +5916,8 @@ trees_in::lang_type_bools (tree t) RB (lang->has_new); RB (lang->has_array_new); - v = b () << 0; - v |= b () << 1; + v = bits.b () << 0; + v |= bits.b () << 1; lang->gets_delete = v; RB (lang->interface_only); RB (lang->interface_unknown); @@ -7106,18 +7145,19 @@ trees_out::tree_node_bools (tree t) gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL || DECL_NAMESPACE_ALIAS (t)); - core_bools (t); + bits_out bits (*this); + core_bools (t, bits); switch (TREE_CODE_CLASS (TREE_CODE (t))) { case tcc_declaration: { bool specific = DECL_LANG_SPECIFIC (t) != NULL; - b (specific); + bits.b (specific); if (specific && VAR_P (t)) - b (DECL_DECOMPOSITION_P (t)); + bits.b (DECL_DECOMPOSITION_P (t)); if (specific) - lang_decl_bools (t); + lang_decl_bools (t, bits); } break; @@ -7128,9 +7168,9 @@ trees_out::tree_node_bools (tree t) gcc_assert (TYPE_LANG_SPECIFIC (t) == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t))); - b (specific); + bits.b (specific); if (specific) - lang_type_bools (t); + lang_type_bools (t, bits); } break; @@ -7138,34 +7178,35 @@ trees_out::tree_node_bools (tree t) break; } - bflush (); + bits.bflush (); } bool trees_in::tree_node_bools (tree t) { - bool ok = core_bools (t); + bits_in bits (*this); + bool ok = core_bools (t, bits); if (ok) switch (TREE_CODE_CLASS (TREE_CODE (t))) { case tcc_declaration: - if (b ()) + if (bits.b ()) { - bool decomp = VAR_P (t) && b (); + bool decomp = VAR_P (t) && bits.b (); ok = maybe_add_lang_decl_raw (t, decomp); if (ok) - ok = lang_decl_bools (t); + ok = lang_decl_bools (t, bits); } break; case tcc_type: - if (b ()) + if (bits.b ()) { ok = maybe_add_lang_type_raw (t); if (ok) - ok = lang_type_bools (t); + ok = lang_type_bools (t, bits); } break; @@ -7173,7 +7214,7 @@ trees_in::tree_node_bools (tree t) break; } - bflush (); + bits.bflush (); if (!ok || get_overrun ()) return false; @@ -7699,8 +7740,7 @@ trees_out::decl_value (tree decl, depset *dep) if (!(mk & MK_template_mask) && !state->is_header ()) { /* Tell the importer whether this is a global module entity, - or a module entity. This bool merges into the next block - of bools. Sneaky. */ + or a module entity. */ tree o = get_originating_module_decl (decl); bool is_attached = false; @@ -7709,9 +7749,9 @@ trees_out::decl_value (tree decl, depset *dep) && DECL_MODULE_ATTACH_P (not_tmpl)) is_attached = true; - b (is_attached); + c (is_attached); } - b (dep && dep->has_defn ()); + c (dep && dep->has_defn ()); } tree_node_bools (decl); } @@ -7982,10 +8022,9 @@ trees_in::decl_value () if (mk != MK_unique) { if (!(mk & MK_template_mask) && !state->is_header ()) - /* See note in trees_out about where this bool is sequenced. */ - is_attached = b (); + is_attached = c (); - has_defn = b (); + has_defn = c (); } if (!tree_node_bools (decl)) @@ -16682,10 +16721,9 @@ module_state::write_define (bytes_out &sec, const cpp_macro *macro) { sec.u (macro->count); - sec.b (macro->fun_like); - sec.b (macro->variadic); - sec.b (macro->syshdr); - sec.bflush (); + sec.c (macro->fun_like); + sec.c (macro->variadic); + sec.c (macro->syshdr); write_location (sec, macro->line); if (macro->fun_like) @@ -16780,10 +16818,9 @@ module_state::read_define (bytes_in &sec, cpp_reader *reader) const macro->kind = cmk_macro; macro->imported_p = true; - macro->fun_like = sec.b (); - macro->variadic = sec.b (); - macro->syshdr = sec.b (); - sec.bflush (); + macro->fun_like = sec.c (); + macro->variadic = sec.c (); + macro->syshdr = sec.c (); macro->line = read_location (sec);