Loop Strength Reduction Pass Does Not Work for Some Varialbles Related to Induction Variables

Dear all,

Hi! Recently, I try to combine the passes SeparateConstOffsetFromGEP and LoopStrengthReduction to transform the multiplication in the lowered GEP IRs into addition.
However, it seems LoopStrengthReduction is unable to remove all the multiplications for the element offset calculation.

My test code is shown below and thanks a lot in advance for your time and suggestion!

From a quick glance at the code, it looks like both inner loops were unrolled at least once (vary by architecture)

and have the expected number of multiplications, four and two (if unrolled once), respectively.

https://gcc.godbolt.org/z/1ks4Pt

Dear Momchil,

Thanks a lot for your prompt reply and kindly suggestion.

The code will truly lead to some automatic unrolling with GCC and increase the number of operations in the assembly code. However, I am focus on the the IR optimization and the calculation of the array element offest, which may need multiplication if the IR instruction GEP is lowered to arithmetic operations. The multiplication operations should be able to be removed by using the LLVM Pass LoopStrengthReduction. Interestingly, it does not work for the test code.

I wamt to post the IR code but it might be too long to be in the mail.

Thanks again for your time and precious suggestions!

Best regards,

Some multiplications, used in address calculation, may stay after loop strength reduction, if the target machine has scaled index addressing mode.

Dear Momchil,

Thanks a lot for your detailed information and yes, it seems that loop strength reduction is limited by scaled index addressing mode. Now, I implement a simple LSR pass to do LSR aggressively, ignoring the scaled addressing mode.
Thanks again for you time and suggestions!