From 38a4afeecdf392267ff73a27a69f458b5d8b424c Mon Sep 17 00:00:00 2001 From: Mark Mitchell Date: Sun, 23 Apr 2006 18:04:33 +0000 Subject: re PR c++/26534 ([4.1] bitfield wrong optimize) 2006-04-23 Mark Mitchell PR c++/26534 * c-common.h (c_build_bitfield_integer_type): Declare. * c-decl.c (c_build_bitfield_integer_type): Move to ... * c-common.c (c_build_bitfield_integer_type): ... here. 2006-04-23 Mark Mitchell PR c++/26534 * cp-tree.h (is_bitfield_expr_with_lowered_type): New function. * typeck.c (is_bitfield_expr_with_lowered_type): New function. (decay_conversion): Convert bitfield expressions to the correct type. (build_modify_expr): Remove spurious conversions. * class.c (layout_class_type): Modify the type of bitfields to indicate a limited range. * call.c (standard_conversion): Adjust the type of bitfield expressions used in an rvalue context. (build_conditional_expr): Likewise. 2006-04-23 Mark Mitchell PR c++/26534 * g++.dg/opt/bitfield1.C: New test. * g++.dg/compat/abi/bitfield1_main.C: Add -w. * g++.dg/compat/abi/bitfield1_x.C: Likewise. * g++.dg/compat/abi/bitfield1_y.C: Likewise. * g++.dg/compat/abi/bitfield2_main.C: Likewise. * g++.dg/compat/abi/bitfield2_x.C: Likewise. * g++.dg/compat/abi/bitfield2_y.C: Likewise. * g++.dg/abi/bitfield1.C: Add dg-warning markers. * g++.dg/abi/bitfield2.C: Likewise. * g++.dg/init/bitfield1.C: Likewise. From-SVN: r113199 --- gcc/cp/class.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'gcc/cp/class.c') diff --git a/gcc/cp/class.c b/gcc/cp/class.c index cc26cb8..1cf87dc 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -4727,6 +4727,29 @@ layout_class_type (tree t, tree *virtuals_p) "classes to be placed at different locations in a " "future version of GCC", field); + /* The middle end uses the type of expressions to determine the + possible range of expression values. In order to optimize + "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end + must be made aware of the width of "i", via its type. + + Because C++ does not have integer types of arbitrary width, + we must (for the purposes of the front end) convert from the + type assigned here to the declared type of the bitfield + whenever a bitfield expression is used as an rvalue. + Similarly, when assigning a value to a bitfield, the value + must be converted to the type given the bitfield here. */ + if (DECL_C_BIT_FIELD (field)) + { + tree ftype; + unsigned HOST_WIDE_INT width; + ftype = TREE_TYPE (field); + width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1); + if (width != TYPE_PRECISION (ftype)) + TREE_TYPE (field) + = c_build_bitfield_integer_type (width, + TYPE_UNSIGNED (ftype)); + } + /* If we needed additional padding after this field, add it now. */ if (padding) -- cgit v1.1