SafeStackColoring.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //===-- SafeStackColoring.cpp - SafeStack frame coloring -------*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "SafeStackColoring.h"
  10. #include "llvm/ADT/DepthFirstIterator.h"
  11. #include "llvm/IR/CFG.h"
  12. #include "llvm/IR/Instructions.h"
  13. #include "llvm/IR/IntrinsicInst.h"
  14. #include "llvm/Support/Debug.h"
  15. using namespace llvm;
  16. using namespace llvm::safestack;
  17. #define DEBUG_TYPE "safestackcoloring"
  18. static cl::opt<bool> ClColoring("safe-stack-coloring",
  19. cl::desc("enable safe stack coloring"),
  20. cl::Hidden, cl::init(true));
  21. const StackColoring::LiveRange &StackColoring::getLiveRange(AllocaInst *AI) {
  22. const auto IT = AllocaNumbering.find(AI);
  23. assert(IT != AllocaNumbering.end());
  24. return LiveRanges[IT->second];
  25. }
  26. bool StackColoring::readMarker(Instruction *I, bool *IsStart) {
  27. auto *II = dyn_cast<IntrinsicInst>(I);
  28. if (!II || (II->getIntrinsicID() != Intrinsic::lifetime_start &&
  29. II->getIntrinsicID() != Intrinsic::lifetime_end))
  30. return false;
  31. *IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
  32. return true;
  33. }
  34. void StackColoring::removeAllMarkers() {
  35. for (auto *I : Markers) {
  36. auto *Op = dyn_cast<Instruction>(I->getOperand(1));
  37. I->eraseFromParent();
  38. // Remove the operand bitcast, too, if it has no more uses left.
  39. if (Op && Op->use_empty())
  40. Op->eraseFromParent();
  41. }
  42. }
  43. void StackColoring::collectMarkers() {
  44. InterestingAllocas.resize(NumAllocas);
  45. DenseMap<BasicBlock *, SmallDenseMap<Instruction *, Marker>> BBMarkerSet;
  46. // Compute the set of start/end markers per basic block.
  47. for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
  48. AllocaInst *AI = Allocas[AllocaNo];
  49. SmallVector<Instruction *, 8> WorkList;
  50. WorkList.push_back(AI);
  51. while (!WorkList.empty()) {
  52. Instruction *I = WorkList.pop_back_val();
  53. for (User *U : I->users()) {
  54. if (auto *BI = dyn_cast<BitCastInst>(U)) {
  55. WorkList.push_back(BI);
  56. continue;
  57. }
  58. auto *UI = dyn_cast<Instruction>(U);
  59. if (!UI)
  60. continue;
  61. bool IsStart;
  62. if (!readMarker(UI, &IsStart))
  63. continue;
  64. if (IsStart)
  65. InterestingAllocas.set(AllocaNo);
  66. BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
  67. Markers.push_back(UI);
  68. }
  69. }
  70. }
  71. // Compute instruction numbering. Only the following instructions are
  72. // considered:
  73. // * Basic block entries
  74. // * Lifetime markers
  75. // For each basic block, compute
  76. // * the list of markers in the instruction order
  77. // * the sets of allocas whose lifetime starts or ends in this BB
  78. DEBUG(dbgs() << "Instructions:\n");
  79. unsigned InstNo = 0;
  80. for (BasicBlock *BB : depth_first(&F)) {
  81. DEBUG(dbgs() << " " << InstNo << ": BB " << BB->getName() << "\n");
  82. unsigned BBStart = InstNo++;
  83. BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
  84. BlockInfo.Begin.resize(NumAllocas);
  85. BlockInfo.End.resize(NumAllocas);
  86. BlockInfo.LiveIn.resize(NumAllocas);
  87. BlockInfo.LiveOut.resize(NumAllocas);
  88. auto &BlockMarkerSet = BBMarkerSet[BB];
  89. if (BlockMarkerSet.empty()) {
  90. unsigned BBEnd = InstNo;
  91. BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
  92. continue;
  93. }
  94. auto ProcessMarker = [&](Instruction *I, const Marker &M) {
  95. DEBUG(dbgs() << " " << InstNo << ": "
  96. << (M.IsStart ? "start " : "end ") << M.AllocaNo << ", "
  97. << *I << "\n");
  98. BBMarkers[BB].push_back({InstNo, M});
  99. InstructionNumbering[I] = InstNo++;
  100. if (M.IsStart) {
  101. if (BlockInfo.End.test(M.AllocaNo))
  102. BlockInfo.End.reset(M.AllocaNo);
  103. BlockInfo.Begin.set(M.AllocaNo);
  104. } else {
  105. if (BlockInfo.Begin.test(M.AllocaNo))
  106. BlockInfo.Begin.reset(M.AllocaNo);
  107. BlockInfo.End.set(M.AllocaNo);
  108. }
  109. };
  110. if (BlockMarkerSet.size() == 1) {
  111. ProcessMarker(BlockMarkerSet.begin()->getFirst(),
  112. BlockMarkerSet.begin()->getSecond());
  113. } else {
  114. // Scan the BB to determine the marker order.
  115. for (Instruction &I : *BB) {
  116. auto It = BlockMarkerSet.find(&I);
  117. if (It == BlockMarkerSet.end())
  118. continue;
  119. ProcessMarker(&I, It->getSecond());
  120. }
  121. }
  122. unsigned BBEnd = InstNo;
  123. BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
  124. }
  125. NumInst = InstNo;
  126. }
  127. void StackColoring::calculateLocalLiveness() {
  128. bool changed = true;
  129. while (changed) {
  130. changed = false;
  131. for (BasicBlock *BB : depth_first(&F)) {
  132. BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
  133. // Compute LiveIn by unioning together the LiveOut sets of all preds.
  134. BitVector LocalLiveIn;
  135. for (auto *PredBB : predecessors(BB)) {
  136. LivenessMap::const_iterator I = BlockLiveness.find(PredBB);
  137. assert(I != BlockLiveness.end() && "Predecessor not found");
  138. LocalLiveIn |= I->second.LiveOut;
  139. }
  140. // Compute LiveOut by subtracting out lifetimes that end in this
  141. // block, then adding in lifetimes that begin in this block. If
  142. // we have both BEGIN and END markers in the same basic block
  143. // then we know that the BEGIN marker comes after the END,
  144. // because we already handle the case where the BEGIN comes
  145. // before the END when collecting the markers (and building the
  146. // BEGIN/END vectors).
  147. BitVector LocalLiveOut = LocalLiveIn;
  148. LocalLiveOut.reset(BlockInfo.End);
  149. LocalLiveOut |= BlockInfo.Begin;
  150. // Update block LiveIn set, noting whether it has changed.
  151. if (LocalLiveIn.test(BlockInfo.LiveIn)) {
  152. changed = true;
  153. BlockInfo.LiveIn |= LocalLiveIn;
  154. }
  155. // Update block LiveOut set, noting whether it has changed.
  156. if (LocalLiveOut.test(BlockInfo.LiveOut)) {
  157. changed = true;
  158. BlockInfo.LiveOut |= LocalLiveOut;
  159. }
  160. }
  161. } // while changed.
  162. }
  163. void StackColoring::calculateLiveIntervals() {
  164. for (auto IT : BlockLiveness) {
  165. BasicBlock *BB = IT.getFirst();
  166. BlockLifetimeInfo &BlockInfo = IT.getSecond();
  167. unsigned BBStart, BBEnd;
  168. std::tie(BBStart, BBEnd) = BlockInstRange[BB];
  169. BitVector Started, Ended;
  170. Started.resize(NumAllocas);
  171. Ended.resize(NumAllocas);
  172. SmallVector<unsigned, 8> Start;
  173. Start.resize(NumAllocas);
  174. // LiveIn ranges start at the first instruction.
  175. for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
  176. if (BlockInfo.LiveIn.test(AllocaNo)) {
  177. Started.set(AllocaNo);
  178. Start[AllocaNo] = BBStart;
  179. }
  180. }
  181. for (auto &It : BBMarkers[BB]) {
  182. unsigned InstNo = It.first;
  183. bool IsStart = It.second.IsStart;
  184. unsigned AllocaNo = It.second.AllocaNo;
  185. if (IsStart) {
  186. assert(!Started.test(AllocaNo) || Start[AllocaNo] == BBStart);
  187. if (!Started.test(AllocaNo)) {
  188. Started.set(AllocaNo);
  189. Ended.reset(AllocaNo);
  190. Start[AllocaNo] = InstNo;
  191. }
  192. } else {
  193. assert(!Ended.test(AllocaNo));
  194. if (Started.test(AllocaNo)) {
  195. LiveRanges[AllocaNo].AddRange(Start[AllocaNo], InstNo);
  196. Started.reset(AllocaNo);
  197. }
  198. Ended.set(AllocaNo);
  199. }
  200. }
  201. for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
  202. if (Started.test(AllocaNo))
  203. LiveRanges[AllocaNo].AddRange(Start[AllocaNo], BBEnd);
  204. }
  205. }
  206. LLVM_DUMP_METHOD void StackColoring::dumpAllocas() {
  207. dbgs() << "Allocas:\n";
  208. for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
  209. dbgs() << " " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n";
  210. }
  211. LLVM_DUMP_METHOD void StackColoring::dumpBlockLiveness() {
  212. dbgs() << "Block liveness:\n";
  213. for (auto IT : BlockLiveness) {
  214. BasicBlock *BB = IT.getFirst();
  215. BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
  216. auto BlockRange = BlockInstRange[BB];
  217. dbgs() << " BB [" << BlockRange.first << ", " << BlockRange.second
  218. << "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End
  219. << ", livein " << BlockInfo.LiveIn << ", liveout "
  220. << BlockInfo.LiveOut << "\n";
  221. }
  222. }
  223. LLVM_DUMP_METHOD void StackColoring::dumpLiveRanges() {
  224. dbgs() << "Alloca liveness:\n";
  225. for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
  226. LiveRange &Range = LiveRanges[AllocaNo];
  227. dbgs() << " " << AllocaNo << ": " << Range << "\n";
  228. }
  229. }
  230. void StackColoring::run() {
  231. DEBUG(dumpAllocas());
  232. for (unsigned I = 0; I < NumAllocas; ++I)
  233. AllocaNumbering[Allocas[I]] = I;
  234. LiveRanges.resize(NumAllocas);
  235. collectMarkers();
  236. if (!ClColoring) {
  237. for (auto &R : LiveRanges) {
  238. R.SetMaximum(1);
  239. R.AddRange(0, 1);
  240. }
  241. return;
  242. }
  243. for (auto &R : LiveRanges)
  244. R.SetMaximum(NumInst);
  245. for (unsigned I = 0; I < NumAllocas; ++I)
  246. if (!InterestingAllocas.test(I))
  247. LiveRanges[I] = getFullLiveRange();
  248. calculateLocalLiveness();
  249. DEBUG(dumpBlockLiveness());
  250. calculateLiveIntervals();
  251. DEBUG(dumpLiveRanges());
  252. }