SafeStackColoring.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //===- SafeStackColoring.h - SafeStack frame coloring ----------*- C++ -*--===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_LIB_CODEGEN_SAFESTACKCOLORING_H
  9. #define LLVM_LIB_CODEGEN_SAFESTACKCOLORING_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/BitVector.h"
  12. #include "llvm/ADT/DenseMap.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cassert>
  17. #include <utility>
  18. namespace llvm {
  19. class BasicBlock;
  20. class Function;
  21. class Instruction;
  22. namespace safestack {
  23. /// Compute live ranges of allocas.
  24. /// Live ranges are represented as sets of "interesting" instructions, which are
  25. /// defined as instructions that may start or end an alloca's lifetime. These
  26. /// are:
  27. /// * lifetime.start and lifetime.end intrinsics
  28. /// * first instruction of any basic block
  29. /// Interesting instructions are numbered in the depth-first walk of the CFG,
  30. /// and in the program order inside each basic block.
  31. class StackColoring {
  32. /// A class representing liveness information for a single basic block.
  33. /// Each bit in the BitVector represents the liveness property
  34. /// for a different stack slot.
  35. struct BlockLifetimeInfo {
  36. /// Which slots BEGINs in each basic block.
  37. BitVector Begin;
  38. /// Which slots ENDs in each basic block.
  39. BitVector End;
  40. /// Which slots are marked as LIVE_IN, coming into each basic block.
  41. BitVector LiveIn;
  42. /// Which slots are marked as LIVE_OUT, coming out of each basic block.
  43. BitVector LiveOut;
  44. };
  45. public:
  46. /// This class represents a set of interesting instructions where an alloca is
  47. /// live.
  48. struct LiveRange {
  49. BitVector bv;
  50. void SetMaximum(int size) { bv.resize(size); }
  51. void AddRange(unsigned start, unsigned end) { bv.set(start, end); }
  52. bool Overlaps(const LiveRange &Other) const {
  53. return bv.anyCommon(Other.bv);
  54. }
  55. void Join(const LiveRange &Other) { bv |= Other.bv; }
  56. };
  57. private:
  58. Function &F;
  59. /// Maps active slots (per bit) for each basic block.
  60. using LivenessMap = DenseMap<BasicBlock *, BlockLifetimeInfo>;
  61. LivenessMap BlockLiveness;
  62. /// Number of interesting instructions.
  63. int NumInst = -1;
  64. /// Numeric ids for interesting instructions.
  65. DenseMap<Instruction *, unsigned> InstructionNumbering;
  66. /// A range [Start, End) of instruction ids for each basic block.
  67. /// Instructions inside each BB have monotonic and consecutive ids.
  68. DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
  69. ArrayRef<AllocaInst *> Allocas;
  70. unsigned NumAllocas;
  71. DenseMap<AllocaInst *, unsigned> AllocaNumbering;
  72. /// LiveRange for allocas.
  73. SmallVector<LiveRange, 8> LiveRanges;
  74. /// The set of allocas that have at least one lifetime.start. All other
  75. /// allocas get LiveRange that corresponds to the entire function.
  76. BitVector InterestingAllocas;
  77. SmallVector<Instruction *, 8> Markers;
  78. struct Marker {
  79. unsigned AllocaNo;
  80. bool IsStart;
  81. };
  82. /// List of {InstNo, {AllocaNo, IsStart}} for each BB, ordered by InstNo.
  83. DenseMap<BasicBlock *, SmallVector<std::pair<unsigned, Marker>, 4>> BBMarkers;
  84. void dumpAllocas();
  85. void dumpBlockLiveness();
  86. void dumpLiveRanges();
  87. bool readMarker(Instruction *I, bool *IsStart);
  88. void collectMarkers();
  89. void calculateLocalLiveness();
  90. void calculateLiveIntervals();
  91. public:
  92. StackColoring(Function &F, ArrayRef<AllocaInst *> Allocas)
  93. : F(F), Allocas(Allocas), NumAllocas(Allocas.size()) {}
  94. void run();
  95. void removeAllMarkers();
  96. /// Returns a set of "interesting" instructions where the given alloca is
  97. /// live. Not all instructions in a function are interesting: we pick a set
  98. /// that is large enough for LiveRange::Overlaps to be correct.
  99. const LiveRange &getLiveRange(AllocaInst *AI);
  100. /// Returns a live range that represents an alloca that is live throughout the
  101. /// entire function.
  102. LiveRange getFullLiveRange() {
  103. assert(NumInst >= 0);
  104. LiveRange R;
  105. R.SetMaximum(NumInst);
  106. R.AddRange(0, NumInst);
  107. return R;
  108. }
  109. };
  110. static inline raw_ostream &operator<<(raw_ostream &OS, const BitVector &V) {
  111. OS << "{";
  112. int idx = V.find_first();
  113. bool first = true;
  114. while (idx >= 0) {
  115. if (!first) {
  116. OS << ", ";
  117. }
  118. first = false;
  119. OS << idx;
  120. idx = V.find_next(idx);
  121. }
  122. OS << "}";
  123. return OS;
  124. }
  125. static inline raw_ostream &operator<<(raw_ostream &OS,
  126. const StackColoring::LiveRange &R) {
  127. return OS << R.bv;
  128. }
  129. } // end namespace safestack
  130. } // end namespace llvm
  131. #endif // LLVM_LIB_CODEGEN_SAFESTACKCOLORING_H