From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22561 invoked by alias); 29 Aug 2014 06:52:08 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 22552 invoked by uid 89); 29 Aug 2014 06:52:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,SPF_PASS autolearn=ham version=3.3.2 X-HELO: service87.mimecast.com Received: from service87.mimecast.com (HELO service87.mimecast.com) (91.220.42.44) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 29 Aug 2014 06:52:06 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) by service87.mimecast.com; Fri, 29 Aug 2014 07:52:03 +0100 Received: from SHAWIN202 ([10.1.255.212]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Fri, 29 Aug 2014 07:52:01 +0100 From: "Thomas Preud'homme" To: , "Jakub Jelinek" Subject: [PATCH] Fix byte size confusion in bswap pass Date: Fri, 29 Aug 2014 06:52:00 -0000 Message-ID: <000301cfc355$bbe9c550$33bd4ff0$@arm.com> MIME-Version: 1.0 X-MC-Unique: 114082907520300401 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2014-08/txt/msg02626.txt.bz2 [CCing you Jakub as you are the one who raised this issue to me] The bswap pass deals with 3 possibly different byte size: host, target and = the size a byte marker in the symbolic_number structure [1]. However, right= now the code mixes the three sizes. This works in practice as the pass is = only enabled for target with BITS_PER_UNIT =3D=3D 8 and nobody runs GCC on = a host with CHAR_BIT !=3D 8. As prompted by Jakub Jelinek, this patch fixes= this mess. Byte marker are 8-bit quantities (they could be made 4-bit quan= tities but I preferred to keep the code working the same as before) for whi= ch a new macro is introduced (BITS_PER_MARKERS), anything related to storin= g the value or a byte marker in a variable should check for the host byte s= ize or wide integer size and anything aimed at manipulating the target valu= e should check for BITS_PER_UNIT. [1] Although the comment for this structure implies that a byte marker as t= he same size as the host byte, the way it is used in the code (even before = any of my patch) shows that it uses a fixed size of 8 [2]. [2] Note that since the pass is only active for targets with BITS_PER_UNIT = =3D=3D 8, it might be using the target byte size. gcc/ChangeLog: 2014-08-29 Thomas Preud'homme * tree-ssa-math-opts.c (struct symbolic_number): Clarify comment about the size of byte markers. (do_shift_rotate): Fix confusion between host, target and marker byte size. (verify_symbolic_number_p): Likewise. (find_bswap_or_nop_1): Likewise. (find_bswap_or_nop): Likewise. diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c index ca2b30d..55c5df7 100644 --- a/gcc/tree-ssa-math-opts.c +++ b/gcc/tree-ssa-math-opts.c @@ -1600,11 +1600,10 @@ make_pass_cse_sincos (gcc::context *ctxt) =20 /* A symbolic number is used to detect byte permutation and selection patterns. Therefore the field N contains an artificial number - consisting of byte size markers: + consisting of octet sized markers: =20 - 0 - byte has the value 0 - 1..size - byte contains the content of the byte - number indexed with that value minus one. + 0 - target byte has the value 0 + 1..size - marker value is the target byte index minus one. =20 To detect permutations on memory sources (arrays and structures), a sym= bolic number is also associated a base address (the array or structure the lo= ad is @@ -1629,6 +1628,8 @@ struct symbolic_number { unsigned HOST_WIDE_INT range; }; =20 +#define BITS_PER_MARKER 8 + /* The number which the find_bswap_or_nop_1 result should match in order to have a nop. The number is masked according to the size of the symbolic number before using it. */ @@ -1650,15 +1651,16 @@ do_shift_rotate (enum tree_code code, struct symbolic_number *n, int count) { - int bitsize =3D TYPE_PRECISION (n->type); + int size =3D TYPE_PRECISION (n->type) / BITS_PER_UNIT; =20 - if (count % 8 !=3D 0) + if (count % BITS_PER_UNIT !=3D 0) return false; + count =3D (count / BITS_PER_UNIT) * BITS_PER_MARKER; =20 /* Zero out the extra bits of N in order to avoid them being shifted into the significant bits. */ - if (bitsize < 8 * (int)sizeof (int64_t)) - n->n &=3D ((uint64_t)1 << bitsize) - 1; + if (size < 64 / BITS_PER_MARKER) + n->n &=3D ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; =20 switch (code) { @@ -1668,22 +1670,22 @@ do_shift_rotate (enum tree_code code, case RSHIFT_EXPR: /* Arithmetic shift of signed type: result is dependent on the value= . */ if (!TYPE_UNSIGNED (n->type) - && (n->n & ((uint64_t) 0xff << (bitsize - 8)))) + && (n->n & ((uint64_t) 0xff << ((size - 1) * BITS_PER_MARKER)))) return false; n->n >>=3D count; break; case LROTATE_EXPR: - n->n =3D (n->n << count) | (n->n >> (bitsize - count)); + n->n =3D (n->n << count) | (n->n >> ((size * BITS_PER_MARKER) - coun= t)); break; case RROTATE_EXPR: - n->n =3D (n->n >> count) | (n->n << (bitsize - count)); + n->n =3D (n->n >> count) | (n->n << ((size * BITS_PER_MARKER) - coun= t)); break; default: return false; } /* Zero unused bits for size. */ - if (bitsize < 8 * (int)sizeof (int64_t)) - n->n &=3D ((uint64_t)1 << bitsize) - 1; + if (size < 64 / BITS_PER_MARKER) + n->n &=3D ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; return true; } =20 @@ -1724,13 +1726,13 @@ init_symbolic_number (struct symbolic_number *n, tr= ee src) if (size % BITS_PER_UNIT !=3D 0) return false; size /=3D BITS_PER_UNIT; - if (size > (int)sizeof (uint64_t)) + if (size > 64 / BITS_PER_MARKER) return false; n->range =3D size; n->n =3D CMPNOP; =20 - if (size < (int)sizeof (int64_t)) - n->n &=3D ((uint64_t)1 << (size * BITS_PER_UNIT)) - 1; + if (size < 64 / BITS_PER_MARKER) + n->n &=3D ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; =20 return true; } @@ -1868,15 +1870,17 @@ find_bswap_or_nop_1 (gimple stmt, struct symbolic_n= umber *n, int limit) case BIT_AND_EXPR: { int i, size =3D TYPE_PRECISION (n->type) / BITS_PER_UNIT; - uint64_t val =3D int_cst_value (rhs2); - uint64_t tmp =3D val; + uint64_t val =3D int_cst_value (rhs2), mask =3D 0; + uint64_t tmp =3D (1 << BITS_PER_UNIT) - 1; =20 /* Only constants masking full bytes are allowed. */ - for (i =3D 0; i < size; i++, tmp >>=3D BITS_PER_UNIT) - if ((tmp & 0xff) !=3D 0 && (tmp & 0xff) !=3D 0xff) + for (i =3D 0; i < size; i++, tmp <<=3D BITS_PER_UNIT) + if ((val & tmp) !=3D 0 && (val & tmp) !=3D tmp) return NULL; + else if (val & tmp) + mask |=3D (uint64_t) 0xff << (i * BITS_PER_MARKER); =20 - n->n &=3D val; + n->n &=3D mask; } break; case LSHIFT_EXPR: @@ -1895,25 +1899,27 @@ find_bswap_or_nop_1 (gimple stmt, struct symbolic_n= umber *n, int limit) type_size =3D TYPE_PRECISION (type); if (type_size % BITS_PER_UNIT !=3D 0) return NULL; - if (type_size > (int)sizeof (uint64_t) * 8) + type_size /=3D BITS_PER_UNIT; + if (type_size > 64 / BITS_PER_MARKER) return NULL; =20 /* Sign extension: result is dependent on the value. */ - old_type_size =3D TYPE_PRECISION (n->type); + old_type_size =3D TYPE_PRECISION (n->type) / BITS_PER_UNIT; if (!TYPE_UNSIGNED (n->type) && type_size > old_type_size - && n->n & ((uint64_t) 0xff << (old_type_size - 8))) + && n->n & ((uint64_t) 0xff << ((old_type_size - 1) + * BITS_PER_MARKER))) return NULL; =20 - if (type_size / BITS_PER_UNIT < (int)(sizeof (int64_t))) + if (type_size < 64 / BITS_PER_MARKER) { /* If STMT casts to a smaller type mask out the bits not belonging to the target type. */ - n->n &=3D ((uint64_t)1 << type_size) - 1; + n->n &=3D ((uint64_t) 1 << (type_size * BITS_PER_MARKER)) - 1; } n->type =3D type; if (!n->base_addr) - n->range =3D type_size / BITS_PER_UNIT; + n->range =3D type_size; } break; default: @@ -1963,7 +1969,6 @@ find_bswap_or_nop_1 (gimple stmt, struct symbolic_num= ber *n, int limit) !=3D gimple_assign_rhs1 (source_stmt2)) { int64_t inc, mask; - unsigned i; HOST_WIDE_INT off_sub; struct symbolic_number *n_ptr; =20 @@ -1987,21 +1992,23 @@ find_bswap_or_nop_1 (gimple stmt, struct symbolic_n= umber *n, int limit) =20 off_sub =3D n2.bytepos - n1.bytepos; =20 - /* Check that the range of memory covered < biggest int size. */ - if (off_sub + n2.range > (int) sizeof (int64_t)) + /* Check that the range of memory covered can be represented by + a symbolic number. */ + if (off_sub + n2.range > 64 / BITS_PER_MARKER) return NULL; n->range =3D n2.range + off_sub; =20 /* Reinterpret byte marks in symbolic number holding the value of bigger weight according to target endianness. */ inc =3D BYTES_BIG_ENDIAN ? off_sub + n2.range - n1.range : off_sub; - mask =3D 0xFF; + size =3D TYPE_PRECISION (n1.type) / BITS_PER_UNIT; + mask =3D 0xff; if (BYTES_BIG_ENDIAN) n_ptr =3D &n1; else n_ptr =3D &n2; - for (i =3D 0; i < sizeof (int64_t); i++, inc <<=3D 8, - mask <<=3D 8) + for (i =3D 0; i < size; i++, inc <<=3D BITS_PER_MARKER, + mask <<=3D BITS_PER_MARKER) { if (n_ptr->n & mask) n_ptr->n +=3D inc; @@ -2021,7 +2028,7 @@ find_bswap_or_nop_1 (gimple stmt, struct symbolic_num= ber *n, int limit) n->bytepos =3D n1.bytepos; n->type =3D n1.type; size =3D TYPE_PRECISION (n->type) / BITS_PER_UNIT; - for (i =3D 0, mask =3D 0xff; i < size; i++, mask <<=3D BITS_PER_UNIT) + for (i =3D 0, mask =3D 0xff; i < size; i++, mask <<=3D BITS_PER_MARKER) { uint64_t masked1, masked2; =20 @@ -2082,17 +2089,17 @@ find_bswap_or_nop (gimple stmt, struct symbolic_num= ber *n, bool *bswap) int rsize; uint64_t tmpn; =20 - for (tmpn =3D n->n, rsize =3D 0; tmpn; tmpn >>=3D BITS_PER_UNIT, rsi= ze++); + for (tmpn =3D n->n, rsize =3D 0; tmpn; tmpn >>=3D BITS_PER_MARKER, r= size++); n->range =3D rsize; } =20 /* Zero out the extra bits of N and CMP*. */ - if (n->range < (int)sizeof (int64_t)) + if (n->range < (int) sizeof (int64_t)) { uint64_t mask; =20 - mask =3D ((uint64_t)1 << (n->range * BITS_PER_UNIT)) - 1; - cmpxchg >>=3D (sizeof (int64_t) - n->range) * BITS_PER_UNIT; + mask =3D ((uint64_t) 1 << (n->range * BITS_PER_MARKER)) - 1; + cmpxchg >>=3D (64 / BITS_PER_MARKER - n->range) * BITS_PER_MARKER; cmpnop &=3D mask; } Tested via boostrap on x86_64-linux-gnu without regressions. Ok for trunk? Best regards, Thomas