SROA.h 5.5 KB

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