From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 1CB653858C31 for ; Sat, 14 Oct 2023 10:49:15 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1CB653858C31 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697280558; cv=none; b=ScrgZJuxPav5J/NczRl2xeGJisUbCsi6mZHneg75a+LvzRnqJXv6gvTZ+nyG1oN1Ic5sjnYff6zmxKThspnTzCtE4rJsC8TYQw5lHyZi4HulQW/8zH0qyEVXXITHsMUSQPm5Ygy3F0vagSZwNSr4wue1slMC8ioAI4hVgiIqBR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697280558; c=relaxed/simple; bh=hDtGEuYc/PU5HtM1roRBDhUICf546VUD+qoX3yuEwRY=; h=DKIM-Signature:DKIM-Signature:From:Mime-Version:Subject:Date: Message-Id:To; b=JI3dSLSaFLbk2ilW6oHEb+YL3tA5A+Cx7NxD1wVPDWZnmxECGxDavB3QV10VDXkR/8enqLiOwHHEfqaklThhJlo5YmpRS86SnJR9ODk8lPyKd6QOgC8f2iMV3+4yl1eScgV+h2V2cD6KhSAJhKCcjdCooBxCXoeVuGBiGu0d6lE= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1CB653858C31 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 1F1701F747; Sat, 14 Oct 2023 10:49:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697280553; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MiovwckD3cOZ4Ac9bS3+dnwn8XYKO7tTWE8y6eCDa88=; b=AvyvZQGuhZci7o54Uk28sMpX5Cfv/tn97IskJ52+6/2xLh4SBDkSGEHX2isd6vcjrlGHjc 2ocDMrnfq6qhaDViJfQhHDjj+ExF0cfGNfIfTNHqTHf7BHM+8lVLoFBOxEfIzFrVV5WXqD 2HlmDp8vEFdn7ANecQtFd8sJ9Yxl8jk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697280553; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MiovwckD3cOZ4Ac9bS3+dnwn8XYKO7tTWE8y6eCDa88=; b=XnuT2envspVij+7VhkFRBXzNmd5X/rPpKOqEfm7NOH+e3aKnMqWAKLY+ob7t7NgMgVlk+q 9ld0o2okA7AZ89Cg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 10FA01390A; Sat, 14 Oct 2023 10:49:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id NDcXBClyKmXEIAAAMHmgww (envelope-from ); Sat, 14 Oct 2023 10:49:13 +0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] wide-int, v2: Fix estimation of buffer sizes for wide_int printing [PR111800] Date: Sat, 14 Oct 2023 12:49:02 +0200 Message-Id: <8EFBCB35-4A82-4294-A180-6D3520F090A1@suse.de> References: Cc: Richard Sandiford , gcc-patches@gcc.gnu.org In-Reply-To: To: Jakub Jelinek X-Mailer: iPhone Mail (20H30) Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -6.60 X-Spamd-Result: default: False [-6.60 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_HAM(-3.00)[100.00%]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; MV_CASE(0.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-3.00)[-1.000]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > Am 14.10.2023 um 11:50 schrieb Jakub Jelinek : >=20 > =EF=BB=BFHi! >=20 >> On Sat, Oct 14, 2023 at 10:41:28AM +0200, Richard Biener wrote: >> Can we somehow abstract this common pattern? >=20 > So like this? With room for the future tweaks like printing decimal > instead of hex numbers by print_dec*, where we'd only need to adjust > the inlines. The XALLOCAVEC call is left for the callers, those would > make the inlines uninlinable and not doing what they should. LGTM. Richard=20 > 2023-10-14 Jakub Jelinek >=20 > PR tree-optimization/111800 > gcc/ > * wide-int-print.h (print_dec_buf_size, print_decs_buf_size, > print_decu_buf_size, print_hex_buf_size): New inline functions. > * wide-int.cc (assert_deceq): Use print_dec_buf_size. > (assert_hexeq): Use print_hex_buf_size. > * wide-int-print.cc (print_decs): Use print_decs_buf_size. > (print_decu): Use print_decu_buf_size. > (print_hex): Use print_hex_buf_size. > (pp_wide_int_large): Use print_dec_buf_size. > * value-range.cc (irange_bitmask::dump): Use print_hex_buf_size. > * value-range-pretty-print.cc (vrange_printer::print_irange_bitmasks): > Likewise. > * tree-ssa-loop-niter.cc (do_warn_aggressive_loop_optimizations): Use > print_dec_buf_size. Use TYPE_SIGN macro in print_dec call argument. > gcc/c-family/ > * c-warn.cc (match_case_to_enum_1): Assert w.get_precision () > is smaller or equal to WIDE_INT_MAX_INL_PRECISION rather than > w.get_len () is smaller or equal to WIDE_INT_MAX_INL_ELTS. >=20 > --- gcc/wide-int-print.h.jj 2023-10-13 19:34:44.283830089 +0200 > +++ gcc/wide-int-print.h 2023-10-14 11:21:44.190603091 +0200 > @@ -36,4 +36,40 @@ extern void print_hex (const wide_int_re > extern void print_hex (const wide_int_ref &wi, FILE *file); > extern void pp_wide_int_large (pretty_printer *, const wide_int_ref &, sig= nop); >=20 > +inline bool > +print_dec_buf_size (const wide_int_ref &wi, signop sgn, unsigned int *len= ) > +{ > + unsigned int l =3D wi.get_len (); > + if ((l !=3D 1 || sgn =3D=3D UNSIGNED) && wi::neg_p (wi)) > + l =3D WIDE_INT_MAX_HWIS (wi.get_precision ()); > + l =3D l * HOST_BITS_PER_WIDE_INT / 4 + 4; > + *len =3D l; > + return UNLIKELY (l > WIDE_INT_PRINT_BUFFER_SIZE); > +} > + > +inline bool > +print_decs_buf_size (const wide_int_ref &wi, unsigned int *len) > +{ > + return print_dec_buf_size (wi, SIGNED, len); > +} > + > +inline bool > +print_decu_buf_size (const wide_int_ref &wi, unsigned int *len) > +{ > + return print_dec_buf_size (wi, UNSIGNED, len); > +} > + > +inline bool > +print_hex_buf_size (const wide_int_ref &wi, unsigned int *len) > +{ > + unsigned int l; > + if (wi::neg_p (wi)) > + l =3D WIDE_INT_MAX_HWIS (wi.get_precision ()); > + else > + l =3D wi.get_len (); > + l =3D l * HOST_BITS_PER_WIDE_INT / 4 + 4; > + *len =3D l; > + return UNLIKELY (l > WIDE_INT_PRINT_BUFFER_SIZE); > +} > + > #endif /* WIDE_INT_PRINT_H */ > --- gcc/wide-int.cc.jj 2023-10-14 11:07:52.738850767 +0200 > +++ gcc/wide-int.cc 2023-10-14 11:22:03.100347386 +0200 > @@ -2450,9 +2450,9 @@ static void > assert_deceq (const char *expected, const wide_int_ref &wi, signop sgn) > { > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p =3D buf; > - unsigned len =3D wi.get_len (); > - if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len; > + if (print_dec_buf_size (wi, sgn, &len)) > + p =3D XALLOCAVEC (char, len); > print_dec (wi, p, sgn); > ASSERT_STREQ (expected, p); > } > @@ -2463,9 +2463,9 @@ static void > assert_hexeq (const char *expected, const wide_int_ref &wi) > { > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p =3D buf; > - unsigned len =3D wi.get_len (); > - if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len; > + if (print_hex_buf_size (wi, &len)) > + p =3D XALLOCAVEC (char, len); > print_hex (wi, p); > ASSERT_STREQ (expected, p); > } > --- gcc/wide-int-print.cc.jj 2023-10-14 11:07:52.737850781 +0200 > +++ gcc/wide-int-print.cc 2023-10-14 11:37:43.994623668 +0200 > @@ -75,9 +75,9 @@ void > print_decs (const wide_int_ref &wi, FILE *file) > { > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p =3D buf; > - unsigned len =3D wi.get_len (); > - if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len; > + if (print_decs_buf_size (wi, &len)) > + p =3D XALLOCAVEC (char, len); > print_decs (wi, p); > fputs (p, file); > } > @@ -102,9 +102,9 @@ void > print_decu (const wide_int_ref &wi, FILE *file) > { > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p =3D buf; > - unsigned len =3D wi.get_len (); > - if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len; > + if (print_decu_buf_size (wi, &len)) > + p =3D XALLOCAVEC (char, len); > print_decu (wi, p); > fputs (p, file); > } > @@ -141,9 +141,9 @@ void > print_hex (const wide_int_ref &wi, FILE *file) > { > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p =3D buf; > - unsigned len =3D wi.get_len (); > - if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len; > + if (print_hex_buf_size (wi, &len)) > + p =3D XALLOCAVEC (char, len); > print_hex (wi, p); > fputs (p, file); > } > @@ -154,8 +154,10 @@ print_hex (const wide_int_ref &wi, FILE > void > pp_wide_int_large (pretty_printer *pp, const wide_int_ref &w, signop sgn) > { > - unsigned int prec =3D w.get_precision (); > - char *buf =3D XALLOCAVEC (char, (prec + 3) / 4 + 3); > + unsigned int len; > + if (!print_dec_buf_size (w, sgn, &len)) > + len =3D WIDE_INT_PRINT_BUFFER_SIZE; > + char *buf =3D XALLOCAVEC (char, len); > print_dec (w, buf, sgn); > pp_string (pp, buf); > } > --- gcc/value-range.cc.jj 2023-10-14 11:07:52.737850781 +0200 > +++ gcc/value-range.cc 2023-10-14 11:26:02.656108017 +0200 > @@ -251,11 +251,10 @@ irange_bitmask::dump (FILE *file) const > pp_needs_newline (&buffer) =3D true; > buffer.buffer->stream =3D file; > pp_string (&buffer, "MASK "); > - unsigned len_mask =3D m_mask.get_len (); > - unsigned len_val =3D m_value.get_len (); > - unsigned len =3D MAX (len_mask, len_val); > - if (len > WIDE_INT_MAX_INL_ELTS) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len_mask, len_val; > + if (print_hex_buf_size (m_mask, &len_mask) > + || print_hex_buf_size (m_value, &len_val)) > + p =3D XALLOCAVEC (char, MAX (len_mask, len_val)); > else > p =3D buf; > print_hex (m_mask, p); > --- gcc/value-range-pretty-print.cc.jj 2023-10-14 11:07:52.735850808 +0= 200 > +++ gcc/value-range-pretty-print.cc 2023-10-14 11:26:46.084520752 +0200= > @@ -100,11 +100,10 @@ vrange_printer::print_irange_bitmasks (c >=20 > pp_string (pp, " MASK "); > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p; > - unsigned len_mask =3D bm.mask ().get_len (); > - unsigned len_val =3D bm.value ().get_len (); > - unsigned len =3D MAX (len_mask, len_val); > - if (len > WIDE_INT_MAX_INL_ELTS) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len_mask, len_val; > + if (print_hex_buf_size (bm.mask (), &len_mask) > + || print_hex_buf_size (bm.value (), &len_val)) > + p =3D XALLOCAVEC (char, MAX (len_mask, len_val)); > else > p =3D buf; > print_hex (bm.mask (), p); > --- gcc/tree-ssa-loop-niter.cc.jj 2023-10-14 11:07:52.732850849 +0200 > +++ gcc/tree-ssa-loop-niter.cc 2023-10-14 11:27:58.299544234 +0200 > @@ -3874,13 +3874,13 @@ do_warn_aggressive_loop_optimizations (c >=20 > gimple *estmt =3D last_nondebug_stmt (e->src); > char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p; > - unsigned len =3D i_bound.get_len (); > - if (len > WIDE_INT_MAX_INL_ELTS) > - p =3D XALLOCAVEC (char, len * HOST_BITS_PER_WIDE_INT / 4 + 4); > + unsigned len; > + if (print_dec_buf_size (i_bound, TYPE_SIGN (TREE_TYPE (loop->nb_iterati= ons)), > + &len)) > + p =3D XALLOCAVEC (char, len); > else > p =3D buf; > - print_dec (i_bound, p, TYPE_UNSIGNED (TREE_TYPE (loop->nb_iterations)) > - ? UNSIGNED : SIGNED); > + print_dec (i_bound, p, TYPE_SIGN (TREE_TYPE (loop->nb_iterations))); > auto_diagnostic_group d; > if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizatio= ns, > "iteration %s invokes undefined behavior", p)) > --- gcc/c-family/c-warn.cc.jj 2023-10-14 11:07:52.720851011 +0200 > +++ gcc/c-family/c-warn.cc 2023-10-14 11:08:38.493231623 +0200 > @@ -1519,7 +1519,7 @@ match_case_to_enum_1 (tree key, tree typ > char buf[WIDE_INT_PRINT_BUFFER_SIZE]; > wide_int w =3D wi::to_wide (key); >=20 > - gcc_assert (w.get_len () <=3D WIDE_INT_MAX_INL_ELTS); > + gcc_assert (w.get_precision () <=3D WIDE_INT_MAX_INL_PRECISION); > if (tree_fits_uhwi_p (key)) > print_dec (w, buf, UNSIGNED); > else if (tree_fits_shwi_p (key)) >=20 >=20 > Jakub >=20