multiplies by powers of two

Matthew Fluet mfluet@intertrust.com
Wed, 11 Jul 2001 16:29:17 -0700 (PDT)


> > Note  imul  will set the overflow flag correctly.  If it weren't for that, we
> > should definitely handle multiply-by-constants using all the  tricks.   Note,
> > shift  is  NOT  always  the best way.  You can do a multiply by lots of small
> > numbers using the lea instruction.  Gcc goes insane doing this, but it really
> > can make a large difference.

If there are some tricks for getting overflow correctly with lea's, I'd be
happy to add them.  Like I said before, multiplications hurt the register
allocator, because you need to get the dst into %eax and clear %edx.


> I believe I saw this even with code compiled -detect-overflow false.  In any
> case, I am in the process of rewriting the basis library code to use integer
> arithmetic without overflow detection when possible, so that we will be able to
> use these tricks even when compiling -detect-overflow true.  Given that shift is
> not always the best way, it seems that this optimization belongs in the codegen.

Note, the peephole optimization for turning mults by pow2's into shifts is
explicitly checking to see if the next transfer is a conditional branch on
the overflow flag; if not, then it goes ahead and does the rewrite.  So,
hopefully Steve's new basis will expose some more optimizations.