From patchwork Wed Jun 7 13:45:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 772432 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wjVGB4zz7z9sDb for ; Wed, 7 Jun 2017 23:46:39 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="spHszlxb"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=hJX5yv6ZOCNcijrUTZvbKcmK7gf5rFdvFm5nXFzxgEgiq3eXb9 jPB3rfiDNnzz5L/ZUV5n+TljjDo3vRDFS7Hik47BZMOrtgxaVkKhZCg/IQlERdh7 lzXugpvzFLtqINWAOVSsbojiUC05PnJM86vn3IIJuXglZlpdy7ZQy7XcE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=4pRytgEHWI6jvisggoaa7FcROKc=; b=spHszlxbaLef4jzxpyox 0ewM0f8SSRbzRbEzPL0EK7vVCRTjUlgzapTrDVb5eag9MBGWuF8JNwTYm1qfSSHR sDScx2tJtZPE45yg1OnXPr71DHhjYJTscUNoH11O/MPAjYMVKYESWZ3l5Zb9OdQy RJIvO0cmdi8o5b+kTf5jHqM= Received: (qmail 3657 invoked by alias); 7 Jun 2017 13:45:37 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 3487 invoked by uid 89); 7 Jun 2017 13:45:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=exciting, Hx-languages-length:3481, tpe, restructure X-HELO: mail-yw0-f179.google.com Received: from mail-yw0-f179.google.com (HELO mail-yw0-f179.google.com) (209.85.161.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 07 Jun 2017 13:45:34 +0000 Received: by mail-yw0-f179.google.com with SMTP id 141so3908980ywe.2 for ; Wed, 07 Jun 2017 06:45:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:to:from:subject:message-id:date :user-agent:mime-version:content-language; bh=A0vGfOF9ksR7Yyb0thKB1amCobC3yrTBsxr/jwhilPQ=; b=paxaQxQJz5FrKj33pmWBWtxmY2jpNDdet20ICXYXle6aaVQ/aO0stuKhbTeXW09Hqn iGk1LDBaMotAoyyHOLR/F7JfOiJFT8yve9h2OOf3zsdJ0z0HUDcT7wJVONWofnL9oSxq nWsng3PXQGNgCGsObtqHDQL4dDDzFSpicVCh5Dj1ds+3gxU9N6X0xKpJZl9FvT1i8Mai Wyr7yOk1ikCGw8JJMvAnWZk6NLvZe+3ab7dT5sCIfYncT9BTbMjv1mvlYPlsCIGcwM+K W+5nTDl0Dr14NYlIK2mRMFFObyfH4RT3NgpM7R+92E/RM7yaoe/T/upwTroQkCa16AUl Q1fw== X-Gm-Message-State: AODbwcDWz8T6HUUgbDayLD7R/OkNo9ijbu0lBHMM1TrcUj0Y+Jmzf6Yi J6DuZKWVlkFfOQ== X-Received: by 10.129.172.102 with SMTP id z38mr7634599ywj.281.1496843136928; Wed, 07 Jun 2017 06:45:36 -0700 (PDT) Received: from ?IPv6:2620:10d:c0a3:20fb:7500:e7fb:4a6f:2254? ([2620:10d:c091:200::7:53e4]) by smtp.googlemail.com with ESMTPSA id i84sm623814ywe.38.2017.06.07.06.45.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Jun 2017 06:45:36 -0700 (PDT) To: GCC Patches From: Nathan Sidwell Subject: [C++ PATCH] Bitfield layout Message-ID: Date: Wed, 7 Jun 2017 09:45:34 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 There are some exciting rules for laying out a bitfield whose type is longer than its underlying type. Essentially you have to go find the longest supported type no longer than the requested size, use that and then insert a bunch on inaccessible padding. The code implementing that rule was more confusing than necessary, scanning the list of provided types for one that is too long, and then rewinding. Then seeing if we found a type shorted than requested and determining the padding. This patch changes the search to simply record the most recent short-enough type and bail out of the loop when we find one too long. Finally, we simply check if the calculated padding is zero bits, and if so, don't put an empty padding field in. Applied to trunk. nathan 2017-06-07 Nathan Sidwell * class.c (layout_class_type): Restructure overlong-bitfield tpe search. Index: class.c =================================================================== --- class.c (revision 248970) +++ class.c (working copy) @@ -6426,41 +6426,39 @@ layout_class_type (tree t, tree *virtual if (DECL_C_BIT_FIELD (field) && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field))) { - unsigned int itk; - tree integer_type; bool was_unnamed_p = false; /* We must allocate the bits as if suitably aligned for the - longest integer type that fits in this many bits. type - of the field. Then, we are supposed to use the left over - bits as additional padding. */ - for (itk = itk_char; itk != itk_none; ++itk) - if (integer_types[itk] != NULL_TREE - && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE), - TYPE_SIZE (integer_types[itk])) - || tree_int_cst_lt (DECL_SIZE (field), - TYPE_SIZE (integer_types[itk])))) - break; + longest integer type that fits in this many bits. Then, + we are supposed to use the left over bits as additional + padding. */ - /* ITK now indicates a type that is too large for the - field. We have to back up by one to find the largest - type that fits. */ - do - { - --itk; - integer_type = integer_types[itk]; - } while (itk > 0 && integer_type == NULL_TREE); + /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */ + tree limit = size_int (MAX_FIXED_MODE_SIZE); + if (tree_int_cst_lt (DECL_SIZE (field), limit)) + limit = DECL_SIZE (field); + + tree integer_type = integer_types[itk_char]; + for (unsigned itk = itk_char; itk != itk_none; itk++) + if (tree next = integer_types[itk]) + { + if (tree_int_cst_lt (limit, TYPE_SIZE (next))) + /* Too big, so our current guess is what we want. */ + break; + /* Not bigger than limit, ok */ + integer_type = next; + } /* Figure out how much additional padding is required. */ - if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field))) - { - if (TREE_CODE (t) == UNION_TYPE) - /* In a union, the padding field must have the full width - of the bit-field; all fields start at offset zero. */ - padding = DECL_SIZE (field); - else - padding = size_binop (MINUS_EXPR, DECL_SIZE (field), - TYPE_SIZE (integer_type)); - } + if (TREE_CODE (t) == UNION_TYPE) + /* In a union, the padding field must have the full width + of the bit-field; all fields start at offset zero. */ + padding = DECL_SIZE (field); + else + padding = size_binop (MINUS_EXPR, DECL_SIZE (field), + TYPE_SIZE (integer_type)); + + if (integer_zerop (padding)) + padding = NULL_TREE; /* An unnamed bitfield does not normally affect the alignment of the containing class on a target where