[PATCH] D42365: [LoopFlatten] Add a loop-flattening pass

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 22 03:45:07 PST 2018


dmgreen created this revision.
Herald added subscribers: eraman, mgorny, mehdi_amini.

This is a simple pass that flattens nested loops.

      

The intention is to optimise loop nests like this, which together
access an array linearly:

  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j)
      f(A[i*M+j]);

into one loop:

  for (int i = 0; i < (N*M); ++i)
    f(A[i]);

It can also flatten loops where the induction variables are not used
in the loop. This can help with codesize and runtime, especially on
simple cpus without advanced branch prediction.

This is only worth flattening if the induction variables are only used
in an expression like i*M+j. If they had any other uses, we would have
to insert a div/mod to reconstruct the original values, so this
wouldn't be profitable.

This pass was originally written by Oliver Stannard, but he had to
go do other super important things. This have been changed a little
since then. There is a patch to enable loop versioning that will
follow.


https://reviews.llvm.org/D42365

Files:
  include/llvm/InitializePasses.h
  include/llvm/LinkAllPasses.h
  include/llvm/Transforms/Scalar.h
  include/llvm/Transforms/Scalar/LoopFlatten.h
  lib/Passes/PassBuilder.cpp
  lib/Passes/PassRegistry.def
  lib/Transforms/IPO/PassManagerBuilder.cpp
  lib/Transforms/Scalar/CMakeLists.txt
  lib/Transforms/Scalar/LoopFlatten.cpp
  lib/Transforms/Scalar/Scalar.cpp
  test/Other/new-pm-defaults.ll
  test/Other/new-pm-thinlto-defaults.ll
  test/Transforms/LoopFlatten/loop-flatten-negative.ll
  test/Transforms/LoopFlatten/loop-flatten.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42365.130861.patch
Type: text/x-patch
Size: 75245 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180122/3364b192/attachment.bin>


More information about the llvm-commits mailing list