[LoopUnroll] Fold add chains during unrolling
authorNikita Popov <npopov@redhat.com>
Wed, 14 Jun 2023 08:34:14 +0000 (10:34 +0200)
committerNikita Popov <npopov@redhat.com>
Wed, 5 Jul 2023 07:54:28 +0000 (09:54 +0200)
commitb9808e5660f5fe9e7414c0c0b93acd899235471c
tree20a623893f7eae77f865f024d99c99664ffeffdf
parent2049b2adfe573853c48e797927485c4c6ea97203
[LoopUnroll] Fold add chains during unrolling

Loop unrolling tends to produce chains of
`%x1 = add %x0, 1; %x2 = add %x1, 1; ...` with one add per unrolled
iteration. This patch simplifies these adds to `%xN = add %x0, N`
directly during unrolling, rather than waiting for InstCombine to do so.

The motivation for this is that having a single add (rather than
an add chain) on the induction variable makes it a simple recurrence,
which we specially recognize in a number of places. This allows
InstCombine to directly perform folds with that knowledge, instead
of first folding the add chains, and then doing other folds in another
InstCombine iteration.

Due to the reduced number of InstCombine iterations, this also
results in a small compile-time improvement.

Differential Revision: https://reviews.llvm.org/D153540
33 files changed:
llvm/lib/Transforms/Utils/LoopUnroll.cpp
llvm/test/Analysis/ScalarEvolution/2012-05-29-MulAddRec.ll
llvm/test/DebugInfo/unrolled-loop-remainder.ll
llvm/test/Transforms/LoopUnroll/2011-08-08-PhiUpdate.ll
llvm/test/Transforms/LoopUnroll/AArch64/partial.ll
llvm/test/Transforms/LoopUnroll/ARM/instr-size-costs.ll
llvm/test/Transforms/LoopUnroll/ARM/loop-unrolling.ll
llvm/test/Transforms/LoopUnroll/ARM/multi-blocks.ll
llvm/test/Transforms/LoopUnroll/PowerPC/p7-unrolling.ll
llvm/test/Transforms/LoopUnroll/PowerPC/p8-unrolling-legalize-vectors-inseltpoison.ll
llvm/test/Transforms/LoopUnroll/PowerPC/p8-unrolling-legalize-vectors.ll
llvm/test/Transforms/LoopUnroll/RISCV/unroll.ll
llvm/test/Transforms/LoopUnroll/X86/mmx.ll
llvm/test/Transforms/LoopUnroll/add-folding-overflow.ll
llvm/test/Transforms/LoopUnroll/full-unroll-keep-first-exit.ll
llvm/test/Transforms/LoopUnroll/nonlatchcondbr.ll
llvm/test/Transforms/LoopUnroll/partial-unroll-non-latch-exit.ll
llvm/test/Transforms/LoopUnroll/partially-unroll-unconditional-latch.ll
llvm/test/Transforms/LoopUnroll/runtime-exit-phi-scev-invalidation.ll
llvm/test/Transforms/LoopUnroll/runtime-loop-at-most-two-exits.ll
llvm/test/Transforms/LoopUnroll/runtime-loop-multiexit-dom-verify.ll
llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll
llvm/test/Transforms/LoopUnroll/runtime-loop5.ll
llvm/test/Transforms/LoopUnroll/runtime-multiexit-heuristic.ll
llvm/test/Transforms/LoopUnroll/runtime-small-upperbound.ll
llvm/test/Transforms/LoopUnroll/runtime-unroll-assume-no-remainder.ll
llvm/test/Transforms/LoopUnroll/scev-invalidation-lcssa.ll
llvm/test/Transforms/LoopUnroll/shifted-tripcount.ll
llvm/test/Transforms/LoopUnroll/tripcount-overflow.ll
llvm/test/Transforms/LoopUnroll/unroll-header-exiting-with-phis.ll
llvm/test/Transforms/LoopUnroll/unroll-opt-attribute.ll
llvm/test/Transforms/LoopUnrollAndJam/dependencies_visit_order.ll
llvm/test/Transforms/LoopUnrollAndJam/unroll-and-jam.ll