public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [lto/pph] Do not pack more bits than requested (issue4415044)
@ 2011-04-14 19:01 Diego Novillo
  2011-04-14 19:40 ` Jakub Jelinek
  0 siblings, 1 reply; 7+ messages in thread
From: Diego Novillo @ 2011-04-14 19:01 UTC (permalink / raw)
  To: reply, rguenther, gcc-patches


This is a corner case that happens not to affect LTO because the "bad"
value is the last one packed.

In pack_ts_type_value_fields, the last thing we pack is
TYPE_ALIAS_SET, which in many cases ends up being -1.  However, we
request bp_pack_value to pack HOST_BITS_PER_INT (32), whereas -1 is 64
bits long on x86_64.  bp_pack_value stores all 64 bits in the word,
but only counts 32.  Any further bit packed will simply be ignored
(since they're OR'd in).

I found this while packing more bits from C++ types, no matter what I
packed, I would get 1s on the reading side.  I fixed it by making
bp_pack_value chop VAL so that only the first NBITS are stored.
Alternately, we could just assert that the caller didn't send a value
that overflows NBITS.

Richi, do you prefer one over the other?


Diego.


	* lto-streamer.h (bitpack_create): Only use the lower NBITS
	from VAL.

diff --git a/gcc/lto-streamer.h b/gcc/lto-streamer.h
index 5f56fc6..0d49430 100644
--- a/gcc/lto-streamer.h
+++ b/gcc/lto-streamer.h
@@ -1190,8 +1190,19 @@ bitpack_create (struct lto_output_stream *s)
 static inline void
 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
 {
-  bitpack_word_t word = bp->word;
+  bitpack_word_t mask, word;
   int pos = bp->pos;
+
+  word = bp->word;
+
+  gcc_assert (nbits > 0 && nbits <= BITS_PER_BITPACK_WORD);
+
+  /* Make sure that VAL only has the lower NBITS set.  Generate a
+     mask with the lower NBITS set and use it to filter the upper
+     bits from VAL.  */
+  mask = ((bitpack_word_t) 1 << nbits) - 1;
+  val = val & mask;
+
   /* If val does not fit into the current bitpack word switch to the
      next one.  */
   if (pos + nbits > BITS_PER_BITPACK_WORD)

--
This patch is available for review at http://codereview.appspot.com/4415044

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-04-15 13:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-14 19:01 [lto/pph] Do not pack more bits than requested (issue4415044) Diego Novillo
2011-04-14 19:40 ` Jakub Jelinek
2011-04-14 21:04   ` Diego Novillo
2011-04-15  9:13     ` Richard Guenther
2011-04-15 12:50       ` Diego Novillo
2011-04-15 13:00         ` Richard Guenther
2011-04-15 13:19           ` Diego Novillo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).