SROA.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SROA.h - Scalar Replacement Of Aggregates ----------------*- 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. /// \file
  14. /// This file provides the interface for LLVM's Scalar Replacement of
  15. /// Aggregates pass. This pass provides both aggregate splitting and the
  16. /// primary SSA formation used in the compiler.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_TRANSFORMS_SCALAR_SROA_H
  20. #define LLVM_TRANSFORMS_SCALAR_SROA_H
  21. #include "llvm/ADT/SetVector.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/IR/PassManager.h"
  24. #include "llvm/IR/ValueHandle.h"
  25. #include <vector>
  26. namespace llvm {
  27. class AllocaInst;
  28. class AssumptionCache;
  29. class DominatorTree;
  30. class Function;
  31. class LLVMContext;
  32. class PHINode;
  33. class SelectInst;
  34. class Use;
  35. /// A private "module" namespace for types and utilities used by SROA. These
  36. /// are implementation details and should not be used by clients.
  37. namespace sroa LLVM_LIBRARY_VISIBILITY {
  38. class AllocaSliceRewriter;
  39. class AllocaSlices;
  40. class Partition;
  41. class SROALegacyPass;
  42. } // end namespace sroa
  43. /// An optimization pass providing Scalar Replacement of Aggregates.
  44. ///
  45. /// This pass takes allocations which can be completely analyzed (that is, they
  46. /// don't escape) and tries to turn them into scalar SSA values. There are
  47. /// a few steps to this process.
  48. ///
  49. /// 1) It takes allocations of aggregates and analyzes the ways in which they
  50. /// are used to try to split them into smaller allocations, ideally of
  51. /// a single scalar data type. It will split up memcpy and memset accesses
  52. /// as necessary and try to isolate individual scalar accesses.
  53. /// 2) It will transform accesses into forms which are suitable for SSA value
  54. /// promotion. This can be replacing a memset with a scalar store of an
  55. /// integer value, or it can involve speculating operations on a PHI or
  56. /// select to be a PHI or select of the results.
  57. /// 3) Finally, this will try to detect a pattern of accesses which map cleanly
  58. /// onto insert and extract operations on a vector value, and convert them to
  59. /// this form. By doing so, it will enable promotion of vector aggregates to
  60. /// SSA vector values.
  61. class SROAPass : public PassInfoMixin<SROAPass> {
  62. LLVMContext *C = nullptr;
  63. DominatorTree *DT = nullptr;
  64. AssumptionCache *AC = nullptr;
  65. /// Worklist of alloca instructions to simplify.
  66. ///
  67. /// Each alloca in the function is added to this. Each new alloca formed gets
  68. /// added to it as well to recursively simplify unless that alloca can be
  69. /// directly promoted. Finally, each time we rewrite a use of an alloca other
  70. /// the one being actively rewritten, we add it back onto the list if not
  71. /// already present to ensure it is re-visited.
  72. SetVector<AllocaInst *, SmallVector<AllocaInst *, 16>> Worklist;
  73. /// A collection of instructions to delete.
  74. /// We try to batch deletions to simplify code and make things a bit more
  75. /// efficient. We also make sure there is no dangling pointers.
  76. SmallVector<WeakVH, 8> DeadInsts;
  77. /// Post-promotion worklist.
  78. ///
  79. /// Sometimes we discover an alloca which has a high probability of becoming
  80. /// viable for SROA after a round of promotion takes place. In those cases,
  81. /// the alloca is enqueued here for re-processing.
  82. ///
  83. /// Note that we have to be very careful to clear allocas out of this list in
  84. /// the event they are deleted.
  85. SetVector<AllocaInst *, SmallVector<AllocaInst *, 16>> PostPromotionWorklist;
  86. /// A collection of alloca instructions we can directly promote.
  87. std::vector<AllocaInst *> PromotableAllocas;
  88. /// A worklist of PHIs to speculate prior to promoting allocas.
  89. ///
  90. /// All of these PHIs have been checked for the safety of speculation and by
  91. /// being speculated will allow promoting allocas currently in the promotable
  92. /// queue.
  93. SetVector<PHINode *, SmallVector<PHINode *, 2>> SpeculatablePHIs;
  94. /// A worklist of select instructions to speculate prior to promoting
  95. /// allocas.
  96. ///
  97. /// All of these select instructions have been checked for the safety of
  98. /// speculation and by being speculated will allow promoting allocas
  99. /// currently in the promotable queue.
  100. SetVector<SelectInst *, SmallVector<SelectInst *, 2>> SpeculatableSelects;
  101. public:
  102. SROAPass() = default;
  103. /// Run the pass over the function.
  104. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  105. private:
  106. friend class sroa::AllocaSliceRewriter;
  107. friend class sroa::SROALegacyPass;
  108. /// Helper used by both the public run method and by the legacy pass.
  109. PreservedAnalyses runImpl(Function &F, DominatorTree &RunDT,
  110. AssumptionCache &RunAC);
  111. bool presplitLoadsAndStores(AllocaInst &AI, sroa::AllocaSlices &AS);
  112. AllocaInst *rewritePartition(AllocaInst &AI, sroa::AllocaSlices &AS,
  113. sroa::Partition &P);
  114. bool splitAlloca(AllocaInst &AI, sroa::AllocaSlices &AS);
  115. bool runOnAlloca(AllocaInst &AI);
  116. void clobberUse(Use &U);
  117. bool deleteDeadInstructions(SmallPtrSetImpl<AllocaInst *> &DeletedAllocas);
  118. bool promoteAllocas(Function &F);
  119. };
  120. } // end namespace llvm
  121. #endif // LLVM_TRANSFORMS_SCALAR_SROA_H
  122. #ifdef __GNUC__
  123. #pragma GCC diagnostic pop
  124. #endif