switch expansion: limit JT growth param values

Message ID a0e291c8-e9e0-eb33-5fa7-8815217e238b@suse.cz
State Accepted
Headers
Series switch expansion: limit JT growth param values |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Martin Liška Jan. 11, 2023, 10:30 a.m. UTC
  Currently, one can request a huge jump table creation which
leads to a non-sensual huge output. Moreover, use auto_vec rather
than a stack-allocated array.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

	PR middle-end/107976

gcc/ChangeLog:

	* params.opt: Limit JT params.
	* stmt.cc (emit_case_dispatch_table): Use auto_vec.
---
 gcc/params.opt | 4 ++--
 gcc/stmt.cc    | 9 ++++-----
 2 files changed, 6 insertions(+), 7 deletions(-)
  

Comments

Richard Biener Jan. 11, 2023, 12:05 p.m. UTC | #1
On Wed, Jan 11, 2023 at 11:31 AM Martin Liška <mliska@suse.cz> wrote:
>
> Currently, one can request a huge jump table creation which
> leads to a non-sensual huge output. Moreover, use auto_vec rather
> than a stack-allocated array.
>
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>
> Ready to be installed?

OK.

Thanks,
Richard.

> Thanks,
> Martin
>
>         PR middle-end/107976
>
> gcc/ChangeLog:
>
>         * params.opt: Limit JT params.
>         * stmt.cc (emit_case_dispatch_table): Use auto_vec.
> ---
>  gcc/params.opt | 4 ++--
>  gcc/stmt.cc    | 9 ++++-----
>  2 files changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/gcc/params.opt b/gcc/params.opt
> index e178dec1600..3454700eb91 100644
> --- a/gcc/params.opt
> +++ b/gcc/params.opt
> @@ -327,11 +327,11 @@ Common Joined UInteger Var(param_iv_max_considered_uses) Init(250) Param Optimiz
>  Bound on number of iv uses in loop optimized in iv optimizations.
>
>  -param=jump-table-max-growth-ratio-for-size=
> -Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_size) Init(300) Param Optimization
> +Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_size) Init(300) IntegerRange(0, 10000) Param Optimization
>  The maximum code size growth ratio when expanding into a jump table (in percent).  The parameter is used when optimizing for size.
>
>  -param=jump-table-max-growth-ratio-for-speed=
> -Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_speed) Init(800) Param Optimization
> +Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_speed) Init(800) IntegerRange(0, 10000) Param Optimization
>  The maximum code size growth ratio when expanding into a jump table (in percent).  The parameter is used when optimizing for speed.
>
>  -param=l1-cache-line-size=
> diff --git a/gcc/stmt.cc b/gcc/stmt.cc
> index 82a3e1035ec..b239c02018a 100644
> --- a/gcc/stmt.cc
> +++ b/gcc/stmt.cc
> @@ -746,7 +746,7 @@ emit_case_dispatch_table (tree index_expr, tree index_type,
>                           tree range, basic_block stmt_bb)
>  {
>    int i, ncases;
> -  rtx *labelvec;
> +  auto_vec<rtx> labelvec;
>    rtx_insn *fallback_label = label_rtx (case_list[0].m_code_label);
>    rtx_code_label *table_label = gen_label_rtx ();
>    bool has_gaps = false;
> @@ -779,8 +779,7 @@ emit_case_dispatch_table (tree index_expr, tree index_type,
>    /* Get table of labels to jump to, in order of case index.  */
>
>    ncases = tree_to_shwi (range) + 1;
> -  labelvec = XALLOCAVEC (rtx, ncases);
> -  memset (labelvec, 0, ncases * sizeof (rtx));
> +  labelvec.safe_grow_cleared (ncases);
>
>    for (unsigned j = 0; j < case_list.length (); j++)
>      {
> @@ -860,11 +859,11 @@ emit_case_dispatch_table (tree index_expr, tree index_type,
>      emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
>                                                  gen_rtx_LABEL_REF (Pmode,
>                                                                     table_label),
> -                                                gen_rtvec_v (ncases, labelvec),
> +                                                gen_rtvec_v (ncases, labelvec.address ()),
>                                                  const0_rtx, const0_rtx));
>    else
>      emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
> -                                           gen_rtvec_v (ncases, labelvec)));
> +                                           gen_rtvec_v (ncases, labelvec.address ())));
>
>    /* Record no drop-through after the table.  */
>    emit_barrier ();
> --
> 2.39.0
>
  

Patch

diff --git a/gcc/params.opt b/gcc/params.opt
index e178dec1600..3454700eb91 100644
--- a/gcc/params.opt
+++ b/gcc/params.opt
@@ -327,11 +327,11 @@  Common Joined UInteger Var(param_iv_max_considered_uses) Init(250) Param Optimiz
 Bound on number of iv uses in loop optimized in iv optimizations.
 
 -param=jump-table-max-growth-ratio-for-size=
-Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_size) Init(300) Param Optimization
+Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_size) Init(300) IntegerRange(0, 10000) Param Optimization
 The maximum code size growth ratio when expanding into a jump table (in percent).  The parameter is used when optimizing for size.
 
 -param=jump-table-max-growth-ratio-for-speed=
-Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_speed) Init(800) Param Optimization
+Common Joined UInteger Var(param_jump_table_max_growth_ratio_for_speed) Init(800) IntegerRange(0, 10000) Param Optimization
 The maximum code size growth ratio when expanding into a jump table (in percent).  The parameter is used when optimizing for speed.
 
 -param=l1-cache-line-size=
diff --git a/gcc/stmt.cc b/gcc/stmt.cc
index 82a3e1035ec..b239c02018a 100644
--- a/gcc/stmt.cc
+++ b/gcc/stmt.cc
@@ -746,7 +746,7 @@  emit_case_dispatch_table (tree index_expr, tree index_type,
 			  tree range, basic_block stmt_bb)
 {
   int i, ncases;
-  rtx *labelvec;
+  auto_vec<rtx> labelvec;
   rtx_insn *fallback_label = label_rtx (case_list[0].m_code_label);
   rtx_code_label *table_label = gen_label_rtx ();
   bool has_gaps = false;
@@ -779,8 +779,7 @@  emit_case_dispatch_table (tree index_expr, tree index_type,
   /* Get table of labels to jump to, in order of case index.  */
 
   ncases = tree_to_shwi (range) + 1;
-  labelvec = XALLOCAVEC (rtx, ncases);
-  memset (labelvec, 0, ncases * sizeof (rtx));
+  labelvec.safe_grow_cleared (ncases);
 
   for (unsigned j = 0; j < case_list.length (); j++)
     {
@@ -860,11 +859,11 @@  emit_case_dispatch_table (tree index_expr, tree index_type,
     emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
 						 gen_rtx_LABEL_REF (Pmode,
 								    table_label),
-						 gen_rtvec_v (ncases, labelvec),
+						 gen_rtvec_v (ncases, labelvec.address ()),
 						 const0_rtx, const0_rtx));
   else
     emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
-					    gen_rtvec_v (ncases, labelvec)));
+					    gen_rtvec_v (ncases, labelvec.address ())));
 
   /* Record no drop-through after the table.  */
   emit_barrier ();