TailRecursionElimination.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- TailRecursionElimination.h ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file transforms calls of the current function (self recursion) followed
  15. // by a return instruction with a branch to the entry of the function, creating
  16. // a loop. This pass also implements the following extensions to the basic
  17. // algorithm:
  18. //
  19. // 1. Trivial instructions between the call and return do not prevent the
  20. // transformation from taking place, though currently the analysis cannot
  21. // support moving any really useful instructions (only dead ones).
  22. // 2. This pass transforms functions that are prevented from being tail
  23. // recursive by an associative and commutative expression to use an
  24. // accumulator variable, thus compiling the typical naive factorial or
  25. // 'fib' implementation into efficient code.
  26. // 3. TRE is performed if the function returns void, if the return
  27. // returns the result returned by the call, or if the function returns a
  28. // run-time constant on all exits from the function. It is possible, though
  29. // unlikely, that the return returns something else (like constant 0), and
  30. // can still be TRE'd. It can be TRE'd if ALL OTHER return instructions in
  31. // the function return the exact same value.
  32. // 4. If it can prove that callees do not access their caller stack frame,
  33. // they are marked as eligible for tail call elimination (by the code
  34. // generator).
  35. //
  36. // There are several improvements that could be made:
  37. //
  38. // 1. If the function has any alloca instructions, these instructions will be
  39. // moved out of the entry block of the function, causing them to be
  40. // evaluated each time through the tail recursion. Safely keeping allocas
  41. // in the entry block requires analysis to proves that the tail-called
  42. // function does not read or write the stack object.
  43. // 2. Tail recursion is only performed if the call immediately precedes the
  44. // return instruction. It's possible that there could be a jump between
  45. // the call and the return.
  46. // 3. There can be intervening operations between the call and the return that
  47. // prevent the TRE from occurring. For example, there could be GEP's and
  48. // stores to memory that will not be read or written by the call. This
  49. // requires some substantial analysis (such as with DSA) to prove safe to
  50. // move ahead of the call, but doing so could allow many more TREs to be
  51. // performed, for example in TreeAdd/TreeAlloc from the treeadd benchmark.
  52. // 4. The algorithm we use to detect if callees access their caller stack
  53. // frames is very primitive.
  54. //
  55. //===----------------------------------------------------------------------===//
  56. #ifndef LLVM_TRANSFORMS_SCALAR_TAILRECURSIONELIMINATION_H
  57. #define LLVM_TRANSFORMS_SCALAR_TAILRECURSIONELIMINATION_H
  58. #include "llvm/IR/PassManager.h"
  59. namespace llvm {
  60. class Function;
  61. struct TailCallElimPass : PassInfoMixin<TailCallElimPass> {
  62. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  63. };
  64. }
  65. #endif // LLVM_TRANSFORMS_SCALAR_TAILRECURSIONELIMINATION_H
  66. #ifdef __GNUC__
  67. #pragma GCC diagnostic pop
  68. #endif