On Thu, 2025-09-25 at 20:18 +0530, Avinash Jayakar wrote:
> On Wed, 2025-09-24 at 08:30 +0200, Richard Biener via Gcc wrote:
> >
> >
> > > 2. Implement mulv2di3 for this specific target (which does
> > > exactly
> > > what
> > > scalar code would do), and let expand pass (expand_mult) take
> > > care
> > > of
> > > converting mult to shift/add/sub.
> >
> > The expand pass wouldn't do this when the target implements
> > mulv2di3.
> >
>
> Actually I tried it by adding define_expand "mulv2di3", and the
> following code in expmed.cc from the function expand_mult
> 3550 {
> 3551 int shift = wi::exact_log2 (rtx_mode_t (scalar_op1,
> mode));
> 3552 /* Perfect power of 2 (other than 1, which is handled
> above). */
> 3553 if (shift > 0)
> 3554 return expand_shift (LSHIFT_EXPR, mode, op0,
> 3555 shift, target, unsignedp);
> 3556 else
> 3557 goto skip_synth;
>
> Does a shift if operand 1 is power of 2. Also there are other
> optimizations applied similar to vector pattern recog.
> So instead of adding similar thing in expand_vector_operations, I was
> thinking of adding mulv2di3 define_expand so that it goes through the
> vectorizer and let expand pass handle the optimization of
> multiplication.
>
The second approach is a bit hacky, there is a particular test that
fails pr25413a.c, which checks that if target does not support
vector_double multiply then loop must not be vectorized. But by adding
mulv2di3 the vector reports the loop has been vectorized. So I think
this approach would be incorrect from the perspective of vectorizer's
output.
Because in case of checking support if op is supported for target, the
predicate can_implement_p is used. It only checks if the code for the
target is available or not and not how it is implemented, so the target
has to implement the code in vectorized way and not emulate it using
scalar instructions to guarantee the vectorizer's output is correct.
Would it be ok to reuse some of the code already written in expand_mult
for constant multiplications? As you mentioned if we have sth like
can_vec_mult_p, then we can use the same in define_expand as well.
Thanks and regards,
Avinash Jayakar