LiveValues.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "LiveValues.h"
  2. #include "llvm/IR/CFG.h"
  3. #include "llvm/IR/InstIterator.h"
  4. #include "llvm/IR/Instructions.h"
  5. using namespace llvm;
  6. static void applyMapping(InstructionSetVector& iset, llvm::DenseMap<llvm::Instruction *, llvm::Instruction *>& imap)
  7. {
  8. // There will be probably be few entries in the imap, so apply them one at a time to the iset.
  9. for (auto& kv : imap)
  10. {
  11. if (iset.count(kv.first) != 0)
  12. {
  13. iset.remove(kv.first);
  14. iset.insert(kv.second);
  15. }
  16. }
  17. }
  18. // Compute liveness of a value at basic blocks. Roughly based on
  19. // Algorithm 6 & 7 from the paper "Computing Liveness Sets for SSA-
  20. // Form Programs" by Brander et al., 2011.
  21. LiveValues::LiveValues(ArrayRef<Instruction*> computeLiveAt)
  22. {
  23. m_liveSets.resize(computeLiveAt.size());
  24. // Build index and set of active blocks
  25. for (unsigned int i = 0; i < computeLiveAt.size(); i++)
  26. {
  27. Instruction* v = computeLiveAt[i];
  28. m_computeLiveAtIndex.insert(std::make_pair(v, i));
  29. m_activeBlocks.insert(v->getParent());
  30. }
  31. if (computeLiveAt.size() > 0)
  32. {
  33. m_function = computeLiveAt[0]->getParent()->getParent();
  34. }
  35. }
  36. // Go over all the instructions between begin (included) and end (excluded) and mark the given value
  37. // live for code locations contained in the given range.
  38. void LiveValues::markLiveRange(Instruction* value, BasicBlock::iterator begin, BasicBlock::iterator end)
  39. {
  40. BasicBlock* B = begin->getParent();
  41. if (m_activeBlocks.count(B) == 0)
  42. return; // Nothing to mark in this block
  43. for (BasicBlock::iterator I = begin; I != end; ++I)
  44. {
  45. if (m_computeLiveAtIndex.count(I))
  46. {
  47. // Mark this value
  48. unsigned int index = m_computeLiveAtIndex[I];
  49. m_liveSets[index].insert(value);
  50. m_allLiveSet.insert(value);
  51. // Also store for each value where it is live.
  52. m_liveAtIndices[value].insert(index);
  53. }
  54. }
  55. }
  56. void LiveValues::upAndMark(Instruction* def, Use& use, BlockSet& scanned)
  57. {
  58. // Determine the starting point for the backwards search.
  59. // (Remember that Use represents an edge between the definition of a value and its use)
  60. // In the case in which the user of the use is a phi node we start the search from the terminator
  61. // of the preceding block.
  62. // This allows to avoid going through loop back-edges in cases like these:
  63. // |
  64. // | (y)
  65. // v
  66. // -----------------
  67. // (x) | z = phi(x, y) |
  68. // ----> | ... |
  69. // | | x = z + 1 |
  70. // | -----------------
  71. // | |
  72. // | |
  73. // | |
  74. // | v
  75. // | -----------------
  76. // | | |
  77. // ------| INDIRECT CALL |
  78. // | |
  79. // -----------------
  80. // | (Start the search for the definition of x (backwards) from here!)
  81. // v
  82. //
  83. // Notice that here x is live across the call. This case is tricky because the def comes 'after'
  84. // the use. The def still dominates the use because phi nodes logically use their input values on the
  85. // edges, i.e. on the terminator of the preceding blocks.
  86. //
  87. // This has the advantage of being able to traverse edges strictly backwards.
  88. Instruction* startingPoint = dyn_cast<Instruction>(use.getUser());
  89. if (PHINode* usePHI = dyn_cast<PHINode>(startingPoint))
  90. {
  91. BasicBlock* predecessor = usePHI->getIncomingBlock(use);
  92. startingPoint = predecessor->getTerminator();
  93. }
  94. BasicBlock* startingPointBB = startingPoint->getParent();
  95. BasicBlock* defBB = def->getParent();
  96. // Start a bottom-up recursive search from startingPoint to the definition of the current value.
  97. // Mark all the code ranges that we encounter on the way a having the current value 'live'.
  98. // 'scanned' contains the blocks that we have scanned to the bottom of the block and the we know
  99. // already having the current value 'live'.
  100. SmallVector<BasicBlock*, 16> worklist;
  101. worklist.push_back(startingPointBB);
  102. BlockSet visited;
  103. while (!worklist.empty())
  104. {
  105. BasicBlock* B = worklist.pop_back_val();
  106. if (scanned.count(B) != 0)
  107. continue;
  108. // We have reached the block that contains the definition of the value. We are done for this
  109. // branch of the search.
  110. if (B == defBB)
  111. {
  112. if (defBB == startingPointBB)
  113. {
  114. // If the first block that we visit is also the last mark only the range of instructions
  115. // between the def and the starting point.
  116. // -----------------
  117. // | |
  118. // | x = // def | <--
  119. // | | !
  120. // | | ! This is the range in which x is live.
  121. // | | !
  122. // | = x // use | <--
  123. // | |
  124. // -----------------
  125. markLiveRange(def, ++BasicBlock::iterator(def), BasicBlock::iterator(startingPoint));
  126. }
  127. else
  128. {
  129. markLiveRange(def, ++BasicBlock::iterator(def), defBB->end());
  130. scanned.insert(B);
  131. }
  132. }
  133. else
  134. {
  135. if (B == startingPointBB)
  136. {
  137. // We are in the starting-point block.
  138. // This can mean two things:
  139. // 1. We are in the first iteration, mark the range between begin and starting point as
  140. // live.
  141. if (visited.count(B) == 0)
  142. {
  143. markLiveRange(def, B->begin(), BasicBlock::iterator(startingPoint));
  144. }
  145. // 2. We came back here because the starting point is in a loop.
  146. // In this case mark the whole block as live range and don't come back anymore.
  147. else
  148. {
  149. markLiveRange(def, B->begin(), B->end());
  150. scanned.insert(B);
  151. }
  152. // The if statement above allows to manage situations like this:
  153. // BB0
  154. // -----------------
  155. // | x = ... |
  156. // -----------------
  157. // |
  158. // |
  159. // |
  160. // BB1 v
  161. // -----------------<-- <--
  162. // | | ! !
  163. // ----->| | ! First range marked !
  164. // | | | ! !
  165. // | | ... = x |<-- ! Second and final range marked
  166. // | | | !
  167. // | | INDIRECT CALL | !
  168. // | | | !
  169. // | ----------------- <--
  170. // | |
  171. // ---------------
  172. // x is defined outside a loop and used inside a loop. This means that it is live inside the
  173. // whole loop.
  174. // So, we first mark the range from the use of x to the top of BB1 and, when we visit BB1
  175. // again (because BB1 is a predecessor of BB1) we mark the whole block as live range.
  176. // <rant>
  177. // This case could have been managed much more easily and efficiently if we had access to
  178. // LLVM LoopInfo analysis pass.
  179. // We could have done the following: x is uses in a loop and defined outside of it => mark
  180. // the whole loop body as live range.
  181. // </rant>
  182. }
  183. else
  184. {
  185. // We are in an intermediate block on the way to the definition mark it, all as live range.
  186. markLiveRange(def, B->begin(), B->end());
  187. scanned.insert(B);
  188. }
  189. visited.insert(B);
  190. for (pred_iterator P = pred_begin(B), PE = pred_end(B); P != PE; ++P)
  191. {
  192. worklist.push_back(*P);
  193. }
  194. }
  195. }
  196. }
  197. void LiveValues::run()
  198. {
  199. if (m_computeLiveAtIndex.empty())
  200. return;
  201. // for each variable v do
  202. for (inst_iterator I = inst_begin(m_function), E = inst_end(m_function); I != E; ++I)
  203. {
  204. Instruction* v = &*I;
  205. assert(v->getParent()->getParent() == m_function);
  206. // for each block B where v is used do
  207. BlockSet scanned;
  208. for (Value::use_iterator U = v->use_begin(), UE = v->use_end(); U != UE; ++U)
  209. {
  210. Instruction* user = cast<Instruction>(U->getUser());
  211. assert(user->getParent()->getParent() == m_function);
  212. (void)user;
  213. upAndMark(v, *U, scanned);
  214. }
  215. }
  216. }
  217. void LiveValues::remapLiveValues(llvm::DenseMap<llvm::Instruction*, llvm::Instruction*>& imap)
  218. {
  219. applyMapping(m_allLiveSet, imap);
  220. for (auto& liveSet : m_liveSets)
  221. applyMapping(liveSet, imap);
  222. }
  223. const LiveValues::Indices* LiveValues::getIndicesWhereLive(const Value* value) const
  224. {
  225. const auto& iter = m_liveAtIndices.find(value);
  226. if (iter == m_liveAtIndices.end())
  227. return nullptr;
  228. return &iter->second;
  229. }
  230. void LiveValues::setIndicesWhereLive(Value* value, const Indices* indices)
  231. {
  232. for (unsigned int idx : *indices)
  233. setLiveAtIndex(value, idx, true);
  234. }
  235. bool LiveValues::liveInDisjointRegions(const Value* valueA, const Value* valueB) const
  236. {
  237. const Indices* indicesA = getIndicesWhereLive(valueA);
  238. if (!indicesA)
  239. return true;
  240. const Indices* indicesB = getIndicesWhereLive(valueB);
  241. if (!indicesB)
  242. return true;
  243. for (const unsigned int index : *indicesA)
  244. {
  245. if (indicesB->count(index))
  246. return false;
  247. }
  248. return true;
  249. }
  250. void LiveValues::setLiveAtIndex(Value* value, unsigned int index, bool live)
  251. {
  252. assert(index <= m_computeLiveAtIndex.size());
  253. if (live)
  254. {
  255. m_liveAtIndices[value].insert(index);
  256. Instruction* inst = cast<Instruction>(value);
  257. m_liveSets[index].insert(inst);
  258. m_allLiveSet.insert(inst);
  259. }
  260. else
  261. {
  262. m_liveAtIndices[value].remove(index);
  263. Instruction* inst = cast<Instruction>(value);
  264. m_liveSets[index].remove(inst);
  265. if (m_liveAtIndices[value].empty())
  266. m_allLiveSet.remove(inst);
  267. }
  268. }
  269. void LiveValues::setLiveAtAllIndices(llvm::Value* value, bool live)
  270. {
  271. Instruction* inst = cast<Instruction>(value);
  272. if (live)
  273. {
  274. for (unsigned int index = 0; index < m_computeLiveAtIndex.size(); ++index)
  275. {
  276. m_liveAtIndices[value].insert(index);
  277. m_liveSets[index].insert(inst);
  278. }
  279. m_allLiveSet.insert(inst);
  280. }
  281. else
  282. {
  283. for (unsigned int index = 0; index < m_computeLiveAtIndex.size(); ++index)
  284. {
  285. m_liveAtIndices[value].remove(index);
  286. m_liveSets[index].remove(inst);
  287. }
  288. if (m_liveAtIndices[value].empty())
  289. m_allLiveSet.remove(inst);
  290. }
  291. }
  292. bool LiveValues::getLiveAtIndex(const Value* value, unsigned int index) const
  293. {
  294. assert(index <= m_computeLiveAtIndex.size());
  295. const auto& it = m_liveAtIndices.find(value);
  296. if (it == m_liveAtIndices.end())
  297. return false;
  298. return (it->second.count(index) != 0);
  299. }