LICM.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/LoopInfo.h"
  39. #include "llvm/IR/PassManager.h"
  40. #include "llvm/Support/CommandLine.h"
  41. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  42. namespace llvm {
  43. extern cl::opt<unsigned> SetLicmMssaOptCap;
  44. extern cl::opt<unsigned> SetLicmMssaNoAccForPromotionCap;
  45. /// Performs Loop Invariant Code Motion Pass.
  46. class LICMPass : public PassInfoMixin<LICMPass> {
  47. unsigned LicmMssaOptCap;
  48. unsigned LicmMssaNoAccForPromotionCap;
  49. bool LicmAllowSpeculation;
  50. public:
  51. LICMPass()
  52. : LicmMssaOptCap(SetLicmMssaOptCap),
  53. LicmMssaNoAccForPromotionCap(SetLicmMssaNoAccForPromotionCap),
  54. LicmAllowSpeculation(true) {}
  55. LICMPass(unsigned LicmMssaOptCap, unsigned LicmMssaNoAccForPromotionCap,
  56. bool LicmAllowSpeculation)
  57. : LicmMssaOptCap(LicmMssaOptCap),
  58. LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap),
  59. LicmAllowSpeculation(LicmAllowSpeculation) {}
  60. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  61. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  62. };
  63. /// Performs LoopNest Invariant Code Motion Pass.
  64. class LNICMPass : public PassInfoMixin<LNICMPass> {
  65. unsigned LicmMssaOptCap;
  66. unsigned LicmMssaNoAccForPromotionCap;
  67. bool LicmAllowSpeculation;
  68. public:
  69. LNICMPass()
  70. : LicmMssaOptCap(SetLicmMssaOptCap),
  71. LicmMssaNoAccForPromotionCap(SetLicmMssaNoAccForPromotionCap),
  72. LicmAllowSpeculation(true) {}
  73. LNICMPass(unsigned LicmMssaOptCap, unsigned LicmMssaNoAccForPromotionCap,
  74. bool LicmAllowSpeculation)
  75. : LicmMssaOptCap(LicmMssaOptCap),
  76. LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap),
  77. LicmAllowSpeculation(LicmAllowSpeculation) {}
  78. PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &AM,
  79. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  80. };
  81. } // end namespace llvm
  82. #endif // LLVM_TRANSFORMS_SCALAR_LICM_H
  83. #ifdef __GNUC__
  84. #pragma GCC diagnostic pop
  85. #endif