Assembly-level peepholes?

2024-10-04 Thread Sid Maxwell via Gcc
I haven't found anything in my digging, so I'd like to ask if anyone knows
of any mechanism in GCC for conducting peephole optimization on generated
code.  I can imagine doing this as a pass over the generated code from
within the backend, or as a separate process between cc1 and as.  I'm
working with a derivative of 4.3.

-+- Sid


Re: Where does this dead statement generated in switch conversion come from?

2024-10-04 Thread Filip Kastl
On Thu 2024-10-03 14:29:44, Andrew Pinski wrote:
> On Thu, Oct 3, 2024 at 7:51 AM Filip Kastl  wrote:
> >
> > Hi All,
> >
> > While toying with the switch conversion GIMPLE pass I noticed that the pass
> > generates a dead statement.  I wanted to investigate why this happens and
> > potentially fix this.  However after looking into the part of the pass
> > responsible for generating the code in question I still have no idea why the
> > dead statement is generated.  Can someone more experienced look at this and
> > tell me what is going on, please?
> >
> > Let's say I'm compiling this C testcase
> >
> > int main()
> > {
> > switch (a % 4)
> > {
> > case 0: return 10;
> > case 1: return 20;
> > case 2: return 30;
> > default: return 40;
> > }
> > }
> >
> > Since the switch computes a linear function, the linear transformation 
> > feature
> > of switch conversion triggers -- with cofficients A = 10 and B = 10 in this
> > case.  This is the relevant GCC source code.  It is a part of the
> > switch_conversion::build_one_array () function:
> >
> >   if (dump_file && coeff_a.to_uhwi () > 0)
> > fprintf (dump_file, "Linear transformation with A = %" PRId64
> >  " and B = %" PRId64 "\n", coeff_a.to_shwi (),
> >  coeff_b.to_shwi ());
> >
> >   /* We must use type of constructor values.  */
> >   gimple_seq seq = NULL;
> >   tree tmp = gimple_convert (&seq, type, m_index_expr);
> >   tree tmp2 = gimple_build (&seq, MULT_EXPR, type,
> > wide_int_to_tree (type, coeff_a), tmp);
> >   tree tmp3 = gimple_build (&seq, PLUS_EXPR, type, tmp2,
> > wide_int_to_tree (type, coeff_b));
> >   tree tmp4 = gimple_convert (&seq, TREE_TYPE (name), tmp3);
> >   gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
> >   load = gimple_build_assign (name, tmp4);
> 
> Both gimple_build uses gimple_simplify to build the gimple (which does
> simplifications).
> Note gimple_convert just calls gimple_build (unless it is already the
> correct type).
> You can find which simplifications is done by adding the `-folding`
> option to the dump option (note this option is not documented but that
> is PR 114892).

Ah ok, this explains it.  Thanks a lot, Andrew!

Filip Kastl

> 
> Thanks,
> Andrew Pinski
> 
> 
> >
> > Before this code is run, the GIMPLE of the basic block in question looks 
> > like
> > this (output of debug_bb (m_switch_bb)):
> >
> > a.0_1 = a;
> > _2 = a.0_1 % 4;
> > _9 = (unsigned int) _2;
> > switch (_2)  [INV], case 0:  [INV], case 1:  [INV], 
> > case 2:  [INV], case 3:  [INV]>
> >
> > What I would expect to see is something like this (also output of debug_bb 
> > ()
> > but this time after the code I listed was run):
> >
> > a.0_1 = a;
> > _2 = a.0_1 % 4;
> > _9 = (unsigned int) _2;
> > _7 = (unsigned int) _2;
> > _8 = 10 * _7;
> > _10 = _8 + 10;
> > switch (_2)  [INV], case 0:  [INV], case 1:  [INV], 
> > case 2:  [INV], case 3:  [INV]>
> >
> > but what I instead see is this:
> >
> > a.0_1 = a;
> > _2 = a.0_1 % 4;
> > _9 = (unsigned int) _2;
> > _7 = (unsigned int) _2;
> > _6 = 10 * _7;
> > _5 = _7 + 1;
> > _10 = _5 * 10;
> > _11 = (int) _10;
> > switch (_2)  [INV], case 0:  [INV], case 1:  [INV], 
> > case 2:  [INV], case 3:  [INV]>
> >
> > The first thing I noticed is that there are two multiplications instead of 
> > one
> > and that the result of one of them doesn't get used (this redundant
> > multiplication is the original reason I started looking into this).  But 
> > there
> > is also a cast to int that I don't see how the GCC code created and the 
> > order
> > of the non-dead multiplication and addition is switched and the added 
> > constant
> > (coefficient B) is 1 instead of 10 (which is correct but just not what I 
> > expect
> > from reading the code).
> >
> > What am I not seeing here?  Does gsi_insert_seq_before do some 
> > optimizations on
> > the seq it inserts?
> >
> > Thanks,
> > Filip Kastl


Assembly-level peepholes?

2024-10-04 Thread Basile STARYNKEVITCH

hello



I haven't found anything in my digging, so I'd like to ask if anyone knows
of any mechanism in GCC for conducting peephole optimization on generated
code.


GCC has a plugin mechanism. write your plugin doing so.


I can imagine doing this as a pass over the generated code from
within the backend, or as a separate process between cc1 and as.  I'm
working with a derivative of 4.3.

Better share optimization code in the middle end.

You really should upgrade your GCC. The current version is 15. Your GCC 
version is a decade old, and you won't find a lot of experts knowing 
that old version (4.3).


NB. My open source project is an inference engine, see 
https://github.com/RefPerSys/RefPerSys/


--
Basile STARYNKEVITCH   
8 rue de la Faïencerie
92340 Bourg-la-Reine   mobile: +33 6 8501 2359
France http://starynkevitch.net/Basile/



gcc-13-20241004 is now available

2024-10-04 Thread GCC Administrator via Gcc
Snapshot gcc-13-20241004 is now available on
  https://gcc.gnu.org/pub/gcc/snapshots/13-20241004/
and on various mirrors, see https://gcc.gnu.org/mirrors.html for details.

This snapshot has been generated from the GCC 13 git branch
with the following options: git://gcc.gnu.org/git/gcc.git branch 
releases/gcc-13 revision bd9f62324853686c65e1b4331a187102e1e526b0

You'll find:

 gcc-13-20241004.tar.xz   Complete GCC

  SHA256=681c588b9724b55fc402098ea5360c0ee28474c976811bb7f5d3f346cf0f9558
  SHA1=abe7abf75297996fd38954cdf6a3dc32506e7333

Diffs from 13-20240927 are available in the diffs/ subdirectory.

When a particular snapshot is ready for public consumption the LATEST-13
link is updated and a message is sent to the gcc list.  Please do not use
a snapshot before it has been announced that way.