On Wed, 25 May 2022, David Malcolm wrote: > On Wed, 2022-05-25 at 10:36 +0200, Richard Biener via Gcc-patches > wrote: > > This patch adds support to unswitch loops with switch statements > > based on invariant index. It furthermore reworks the cost model > > to allow an overall budget of statements to be created per original > > loop by all unswitching opportunities in the loop. Compared to > > the original all unswitching opportunities in a loop are > > pre-evaluated before the first transform which will allow future > > changes to select the most profitable candidates first. > > > > To efficiently support switch statements the pass now uses > > ranger to simplify switch statements and conditions in loop > > copies based on ranges extracted from the recorded set of > > predicates unswitched. > > > > Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk. > > > > Richard. > > > > [...snip...] > > > diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-10.c > > b/gcc/testsuite/gcc.dg/loop-unswitch-10.c > > new file mode 100644 > > index 00000000000..5e4f16e2935 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/loop-unswitch-10.c > > @@ -0,0 +1,56 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch- > > optimized" } */ > > + > > +int > > +__attribute__((noipa)) > > +foo(double *a, double *b, double *c, double *d, double *r, int size, > > int order) > > +{ > > + for (int i = 0; i < size; i++) > > + { > > + double tmp, tmp2; > > + > > + switch(order) > > + { > > + case 0: > > + tmp = -8 * a[i]; > > + tmp2 = 2 * b[i]; > > + break; > > + case 1: > > + tmp = 3 * a[i] - 2 * b[i]; > > + tmp2 = 5 * b[i] - 2 * c[i]; > > + break; > > + case 2: > > + tmp = 9 * a[i] + 2 * b[i] + c[i]; > > + tmp2 = 4 * b[i] + 2 * c[i] + 8 * d[i]; > > + break; > > + case 3: > > + tmp = 3 * a[i] + 2 * b[i] - c[i]; > > + tmp2 = b[i] - 2 * c[i] + 8 * d[i]; > > + break; > > + defaut: > > + __builtin_unreachable (); > > I'm guessing "defaut:" here is a typo for "default:" i.e. it's an > unused label named "defaut", when presumably you meant to have an > unreachable default case. Does this affect the loop unswitching logic? > > I wonder if we warn for this (or if we can/should?)
I guess we might want to (warn about labels in the "toplevel" scope in switch statements). So warn about switch (x) { case 1: bar: }; the 'case' might be missing here (if 'bar' is a valid enumeration constant for example) but not about switch (x) { case 1: { bar: } }; Maybe we just want to warn when the label is otherwise unused? > Looking at the patch, it seems to also be present in: > gcc/testsuite/gcc.dg/loop-unswitch-11.c > gcc/testsuite/gcc.dg/loop-unswitch-14.c > but I might have missed some. Heh - how did you spot these but me not looking over the patch multiple times ... And no, it doesn't change behavior so I just pushed a fix. Richard. > > > Dave > > -- Richard Biener <rguent...@suse.de> SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)