This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnroll] Fold add chains during unrolling
ClosedPublic

Authored by nikic on Jun 22 2023, 5:51 AM.

Details

Summary

Loop unrolling tends to produce chains of %x1 = add %x0, 1; %x2 = add %x1, 1; ... with one add per unrolled iteration. This patch proposed to simplify 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: http://llvm-compile-time-tracker.com/compare.php?from=4c748821cd18a13898db87cbe4925e5f26af1fab&to=e21a18f8e74a7bdb1bf98fbe1ce6accce3a10a0a&stat=instructions:u

Diff Detail

Event Timeline

nikic created this revision.Jun 22 2023, 5:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2023, 5:51 AM
nikic requested review of this revision.Jun 22 2023, 5:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2023, 5:51 AM
nikic edited the summary of this revision. (Show Details)Jun 29 2023, 12:03 AM

Ping

fhahn accepted this revision.Jun 30 2023, 8:57 AM

LGTM, thanks!

llvm/lib/Transforms/Utils/LoopUnroll.cpp
258

Do we have any tests where there's a signed overflow here? Would be good to make sure this is covered in the tests.

This revision is now accepted and ready to land.Jun 30 2023, 8:57 AM
This revision was automatically updated to reflect the committed changes.