LICM.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LICM.h - Loop Invariant Code Motion Pass -------*- 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 pass performs loop invariant code motion, attempting to remove as much
  15. // code from the body of a loop as possible. It does this by either hoisting
  16. // code into the preheader block, or by sinking code to the exit blocks if it is
  17. // safe. This pass also promotes must-aliased memory locations in the loop to
  18. // live in registers, thus hoisting and sinking "invariant" loads and stores.
  19. //
  20. // This pass uses alias analysis for two purposes:
  21. //
  22. // 1. Moving loop invariant loads and calls out of loops. If we can determine
  23. // that a load or call inside of a loop never aliases anything stored to,
  24. // we can hoist it or sink it like any other instruction.
  25. // 2. Scalar Promotion of Memory - If there is a store instruction inside of
  26. // the loop, we try to move the store to happen AFTER the loop instead of
  27. // inside of the loop. This can only happen if a few conditions are true:
  28. // A. The pointer stored through is loop invariant
  29. // B. There are no stores or loads in the loop which _may_ alias the
  30. // pointer. There are no calls in the loop which mod/ref the pointer.
  31. // If these conditions are true, we can promote the loads and stores in the
  32. // loop of the pointer to use a temporary alloca'd variable. We then use
  33. // the SSAUpdater to construct the appropriate SSA form for the value.
  34. //
  35. //===----------------------------------------------------------------------===//
  36. #ifndef LLVM_TRANSFORMS_SCALAR_LICM_H
  37. #define LLVM_TRANSFORMS_SCALAR_LICM_H
  38. #include "llvm/Analysis/LoopAnalysisManager.h"
  39. #include "llvm/IR/PassManager.h"
  40. #include "llvm/Support/CommandLine.h"
  41. namespace llvm {
  42. class LPMUpdater;
  43. class Loop;
  44. class LoopNest;
  45. extern cl::opt<unsigned> SetLicmMssaOptCap;
  46. extern cl::opt<unsigned> SetLicmMssaNoAccForPromotionCap;
  47. struct LICMOptions {
  48. unsigned MssaOptCap;
  49. unsigned MssaNoAccForPromotionCap;
  50. bool AllowSpeculation;
  51. LICMOptions()
  52. : MssaOptCap(SetLicmMssaOptCap),
  53. MssaNoAccForPromotionCap(SetLicmMssaNoAccForPromotionCap),
  54. AllowSpeculation(true) {}
  55. LICMOptions(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
  56. bool AllowSpeculation)
  57. : MssaOptCap(MssaOptCap),
  58. MssaNoAccForPromotionCap(MssaNoAccForPromotionCap),
  59. AllowSpeculation(AllowSpeculation) {}
  60. };
  61. /// Performs Loop Invariant Code Motion Pass.
  62. class LICMPass : public PassInfoMixin<LICMPass> {
  63. LICMOptions Opts;
  64. public:
  65. LICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
  66. bool AllowSpeculation)
  67. : LICMPass(LICMOptions(MssaOptCap, MssaNoAccForPromotionCap,
  68. AllowSpeculation)) {}
  69. LICMPass(LICMOptions Opts) : Opts(Opts) {}
  70. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  71. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  72. void printPipeline(raw_ostream &OS,
  73. function_ref<StringRef(StringRef)> MapClassName2PassName);
  74. };
  75. /// Performs LoopNest Invariant Code Motion Pass.
  76. class LNICMPass : public PassInfoMixin<LNICMPass> {
  77. LICMOptions Opts;
  78. public:
  79. LNICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
  80. bool AllowSpeculation)
  81. : LNICMPass(LICMOptions(MssaOptCap, MssaNoAccForPromotionCap,
  82. AllowSpeculation)) {}
  83. LNICMPass(LICMOptions Opts) : Opts(Opts) {}
  84. PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &AM,
  85. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  86. void printPipeline(raw_ostream &OS,
  87. function_ref<StringRef(StringRef)> MapClassName2PassName);
  88. };
  89. } // end namespace llvm
  90. #endif // LLVM_TRANSFORMS_SCALAR_LICM_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif