ThreadSafetyTIL.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //===- ThreadSafetyTIL.cpp -------------------------------------*- 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 in the llvm repository for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
  10. #include "clang/Analysis/Analyses/ThreadSafetyTraverse.h"
  11. using namespace clang;
  12. using namespace threadSafety;
  13. using namespace til;
  14. StringRef til::getUnaryOpcodeString(TIL_UnaryOpcode Op) {
  15. switch (Op) {
  16. case UOP_Minus: return "-";
  17. case UOP_BitNot: return "~";
  18. case UOP_LogicNot: return "!";
  19. }
  20. return "";
  21. }
  22. StringRef til::getBinaryOpcodeString(TIL_BinaryOpcode Op) {
  23. switch (Op) {
  24. case BOP_Mul: return "*";
  25. case BOP_Div: return "/";
  26. case BOP_Rem: return "%";
  27. case BOP_Add: return "+";
  28. case BOP_Sub: return "-";
  29. case BOP_Shl: return "<<";
  30. case BOP_Shr: return ">>";
  31. case BOP_BitAnd: return "&";
  32. case BOP_BitXor: return "^";
  33. case BOP_BitOr: return "|";
  34. case BOP_Eq: return "==";
  35. case BOP_Neq: return "!=";
  36. case BOP_Lt: return "<";
  37. case BOP_Leq: return "<=";
  38. case BOP_Cmp: return "<=>";
  39. case BOP_LogicAnd: return "&&";
  40. case BOP_LogicOr: return "||";
  41. }
  42. return "";
  43. }
  44. SExpr* Future::force() {
  45. Status = FS_evaluating;
  46. Result = compute();
  47. Status = FS_done;
  48. return Result;
  49. }
  50. unsigned BasicBlock::addPredecessor(BasicBlock *Pred) {
  51. unsigned Idx = Predecessors.size();
  52. Predecessors.reserveCheck(1, Arena);
  53. Predecessors.push_back(Pred);
  54. for (SExpr *E : Args) {
  55. if (Phi* Ph = dyn_cast<Phi>(E)) {
  56. Ph->values().reserveCheck(1, Arena);
  57. Ph->values().push_back(nullptr);
  58. }
  59. }
  60. return Idx;
  61. }
  62. void BasicBlock::reservePredecessors(unsigned NumPreds) {
  63. Predecessors.reserve(NumPreds, Arena);
  64. for (SExpr *E : Args) {
  65. if (Phi* Ph = dyn_cast<Phi>(E)) {
  66. Ph->values().reserve(NumPreds, Arena);
  67. }
  68. }
  69. }
  70. // If E is a variable, then trace back through any aliases or redundant
  71. // Phi nodes to find the canonical definition.
  72. const SExpr *til::getCanonicalVal(const SExpr *E) {
  73. while (true) {
  74. if (auto *V = dyn_cast<Variable>(E)) {
  75. if (V->kind() == Variable::VK_Let) {
  76. E = V->definition();
  77. continue;
  78. }
  79. }
  80. if (const Phi *Ph = dyn_cast<Phi>(E)) {
  81. if (Ph->status() == Phi::PH_SingleVal) {
  82. E = Ph->values()[0];
  83. continue;
  84. }
  85. }
  86. break;
  87. }
  88. return E;
  89. }
  90. // If E is a variable, then trace back through any aliases or redundant
  91. // Phi nodes to find the canonical definition.
  92. // The non-const version will simplify incomplete Phi nodes.
  93. SExpr *til::simplifyToCanonicalVal(SExpr *E) {
  94. while (true) {
  95. if (auto *V = dyn_cast<Variable>(E)) {
  96. if (V->kind() != Variable::VK_Let)
  97. return V;
  98. // Eliminate redundant variables, e.g. x = y, or x = 5,
  99. // but keep anything more complicated.
  100. if (til::ThreadSafetyTIL::isTrivial(V->definition())) {
  101. E = V->definition();
  102. continue;
  103. }
  104. return V;
  105. }
  106. if (auto *Ph = dyn_cast<Phi>(E)) {
  107. if (Ph->status() == Phi::PH_Incomplete)
  108. simplifyIncompleteArg(Ph);
  109. // Eliminate redundant Phi nodes.
  110. if (Ph->status() == Phi::PH_SingleVal) {
  111. E = Ph->values()[0];
  112. continue;
  113. }
  114. }
  115. return E;
  116. }
  117. }
  118. // Trace the arguments of an incomplete Phi node to see if they have the same
  119. // canonical definition. If so, mark the Phi node as redundant.
  120. // getCanonicalVal() will recursively call simplifyIncompletePhi().
  121. void til::simplifyIncompleteArg(til::Phi *Ph) {
  122. assert(Ph && Ph->status() == Phi::PH_Incomplete);
  123. // eliminate infinite recursion -- assume that this node is not redundant.
  124. Ph->setStatus(Phi::PH_MultiVal);
  125. SExpr *E0 = simplifyToCanonicalVal(Ph->values()[0]);
  126. for (unsigned i=1, n=Ph->values().size(); i<n; ++i) {
  127. SExpr *Ei = simplifyToCanonicalVal(Ph->values()[i]);
  128. if (Ei == Ph)
  129. continue; // Recursive reference to itself. Don't count.
  130. if (Ei != E0) {
  131. return; // Status is already set to MultiVal.
  132. }
  133. }
  134. Ph->setStatus(Phi::PH_SingleVal);
  135. }
  136. // Renumbers the arguments and instructions to have unique, sequential IDs.
  137. int BasicBlock::renumberInstrs(int ID) {
  138. for (auto *Arg : Args)
  139. Arg->setID(this, ID++);
  140. for (auto *Instr : Instrs)
  141. Instr->setID(this, ID++);
  142. TermInstr->setID(this, ID++);
  143. return ID;
  144. }
  145. // Sorts the CFGs blocks using a reverse post-order depth-first traversal.
  146. // Each block will be written into the Blocks array in order, and its BlockID
  147. // will be set to the index in the array. Sorting should start from the entry
  148. // block, and ID should be the total number of blocks.
  149. int BasicBlock::topologicalSort(SimpleArray<BasicBlock*>& Blocks, int ID) {
  150. if (Visited) return ID;
  151. Visited = true;
  152. for (auto *Block : successors())
  153. ID = Block->topologicalSort(Blocks, ID);
  154. // set ID and update block array in place.
  155. // We may lose pointers to unreachable blocks.
  156. assert(ID > 0);
  157. BlockID = --ID;
  158. Blocks[BlockID] = this;
  159. return ID;
  160. }
  161. // Performs a reverse topological traversal, starting from the exit block and
  162. // following back-edges. The dominator is serialized before any predecessors,
  163. // which guarantees that all blocks are serialized after their dominator and
  164. // before their post-dominator (because it's a reverse topological traversal).
  165. // ID should be initially set to 0.
  166. //
  167. // This sort assumes that (1) dominators have been computed, (2) there are no
  168. // critical edges, and (3) the entry block is reachable from the exit block
  169. // and no blocks are accessible via traversal of back-edges from the exit that
  170. // weren't accessible via forward edges from the entry.
  171. int BasicBlock::topologicalFinalSort(SimpleArray<BasicBlock*>& Blocks, int ID) {
  172. // Visited is assumed to have been set by the topologicalSort. This pass
  173. // assumes !Visited means that we've visited this node before.
  174. if (!Visited) return ID;
  175. Visited = false;
  176. if (DominatorNode.Parent)
  177. ID = DominatorNode.Parent->topologicalFinalSort(Blocks, ID);
  178. for (auto *Pred : Predecessors)
  179. ID = Pred->topologicalFinalSort(Blocks, ID);
  180. assert(static_cast<size_t>(ID) < Blocks.size());
  181. BlockID = ID++;
  182. Blocks[BlockID] = this;
  183. return ID;
  184. }
  185. // Computes the immediate dominator of the current block. Assumes that all of
  186. // its predecessors have already computed their dominators. This is achieved
  187. // by visiting the nodes in topological order.
  188. void BasicBlock::computeDominator() {
  189. BasicBlock *Candidate = nullptr;
  190. // Walk backwards from each predecessor to find the common dominator node.
  191. for (auto *Pred : Predecessors) {
  192. // Skip back-edges
  193. if (Pred->BlockID >= BlockID) continue;
  194. // If we don't yet have a candidate for dominator yet, take this one.
  195. if (Candidate == nullptr) {
  196. Candidate = Pred;
  197. continue;
  198. }
  199. // Walk the alternate and current candidate back to find a common ancestor.
  200. auto *Alternate = Pred;
  201. while (Alternate != Candidate) {
  202. if (Candidate->BlockID > Alternate->BlockID)
  203. Candidate = Candidate->DominatorNode.Parent;
  204. else
  205. Alternate = Alternate->DominatorNode.Parent;
  206. }
  207. }
  208. DominatorNode.Parent = Candidate;
  209. DominatorNode.SizeOfSubTree = 1;
  210. }
  211. // Computes the immediate post-dominator of the current block. Assumes that all
  212. // of its successors have already computed their post-dominators. This is
  213. // achieved visiting the nodes in reverse topological order.
  214. void BasicBlock::computePostDominator() {
  215. BasicBlock *Candidate = nullptr;
  216. // Walk back from each predecessor to find the common post-dominator node.
  217. for (auto *Succ : successors()) {
  218. // Skip back-edges
  219. if (Succ->BlockID <= BlockID) continue;
  220. // If we don't yet have a candidate for post-dominator yet, take this one.
  221. if (Candidate == nullptr) {
  222. Candidate = Succ;
  223. continue;
  224. }
  225. // Walk the alternate and current candidate back to find a common ancestor.
  226. auto *Alternate = Succ;
  227. while (Alternate != Candidate) {
  228. if (Candidate->BlockID < Alternate->BlockID)
  229. Candidate = Candidate->PostDominatorNode.Parent;
  230. else
  231. Alternate = Alternate->PostDominatorNode.Parent;
  232. }
  233. }
  234. PostDominatorNode.Parent = Candidate;
  235. PostDominatorNode.SizeOfSubTree = 1;
  236. }
  237. // Renumber instructions in all blocks
  238. void SCFG::renumberInstrs() {
  239. int InstrID = 0;
  240. for (auto *Block : Blocks)
  241. InstrID = Block->renumberInstrs(InstrID);
  242. }
  243. static inline void computeNodeSize(BasicBlock *B,
  244. BasicBlock::TopologyNode BasicBlock::*TN) {
  245. BasicBlock::TopologyNode *N = &(B->*TN);
  246. if (N->Parent) {
  247. BasicBlock::TopologyNode *P = &(N->Parent->*TN);
  248. // Initially set ID relative to the (as yet uncomputed) parent ID
  249. N->NodeID = P->SizeOfSubTree;
  250. P->SizeOfSubTree += N->SizeOfSubTree;
  251. }
  252. }
  253. static inline void computeNodeID(BasicBlock *B,
  254. BasicBlock::TopologyNode BasicBlock::*TN) {
  255. BasicBlock::TopologyNode *N = &(B->*TN);
  256. if (N->Parent) {
  257. BasicBlock::TopologyNode *P = &(N->Parent->*TN);
  258. N->NodeID += P->NodeID; // Fix NodeIDs relative to starting node.
  259. }
  260. }
  261. // Normalizes a CFG. Normalization has a few major components:
  262. // 1) Removing unreachable blocks.
  263. // 2) Computing dominators and post-dominators
  264. // 3) Topologically sorting the blocks into the "Blocks" array.
  265. void SCFG::computeNormalForm() {
  266. // Topologically sort the blocks starting from the entry block.
  267. int NumUnreachableBlocks = Entry->topologicalSort(Blocks, Blocks.size());
  268. if (NumUnreachableBlocks > 0) {
  269. // If there were unreachable blocks shift everything down, and delete them.
  270. for (size_t I = NumUnreachableBlocks, E = Blocks.size(); I < E; ++I) {
  271. size_t NI = I - NumUnreachableBlocks;
  272. Blocks[NI] = Blocks[I];
  273. Blocks[NI]->BlockID = NI;
  274. // FIXME: clean up predecessor pointers to unreachable blocks?
  275. }
  276. Blocks.drop(NumUnreachableBlocks);
  277. }
  278. // Compute dominators.
  279. for (auto *Block : Blocks)
  280. Block->computeDominator();
  281. // Once dominators have been computed, the final sort may be performed.
  282. int NumBlocks = Exit->topologicalFinalSort(Blocks, 0);
  283. assert(static_cast<size_t>(NumBlocks) == Blocks.size());
  284. (void) NumBlocks;
  285. // Renumber the instructions now that we have a final sort.
  286. renumberInstrs();
  287. // Compute post-dominators and compute the sizes of each node in the
  288. // dominator tree.
  289. for (auto *Block : Blocks.reverse()) {
  290. Block->computePostDominator();
  291. computeNodeSize(Block, &BasicBlock::DominatorNode);
  292. }
  293. // Compute the sizes of each node in the post-dominator tree and assign IDs in
  294. // the dominator tree.
  295. for (auto *Block : Blocks) {
  296. computeNodeID(Block, &BasicBlock::DominatorNode);
  297. computeNodeSize(Block, &BasicBlock::PostDominatorNode);
  298. }
  299. // Assign IDs in the post-dominator tree.
  300. for (auto *Block : Blocks.reverse()) {
  301. computeNodeID(Block, &BasicBlock::PostDominatorNode);
  302. }
  303. }