1 //===- ThreadSafetyTIL.cpp ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT in the llvm repository for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
11 #include "clang/Basic/LLVM.h"
12 #include "llvm/Support/Casting.h"
16 using namespace clang;
17 using namespace threadSafety;
20 StringRef til::getUnaryOpcodeString(TIL_UnaryOpcode Op) {
22 case UOP_Minus: return "-";
23 case UOP_BitNot: return "~";
24 case UOP_LogicNot: return "!";
29 StringRef til::getBinaryOpcodeString(TIL_BinaryOpcode Op) {
31 case BOP_Mul: return "*";
32 case BOP_Div: return "/";
33 case BOP_Rem: return "%";
34 case BOP_Add: return "+";
35 case BOP_Sub: return "-";
36 case BOP_Shl: return "<<";
37 case BOP_Shr: return ">>";
38 case BOP_BitAnd: return "&";
39 case BOP_BitXor: return "^";
40 case BOP_BitOr: return "|";
41 case BOP_Eq: return "==";
42 case BOP_Neq: return "!=";
43 case BOP_Lt: return "<";
44 case BOP_Leq: return "<=";
45 case BOP_Cmp: return "<=>";
46 case BOP_LogicAnd: return "&&";
47 case BOP_LogicOr: return "||";
52 SExpr* Future::force() {
53 Status = FS_evaluating;
59 unsigned BasicBlock::addPredecessor(BasicBlock *Pred) {
60 unsigned Idx = Predecessors.size();
61 Predecessors.reserveCheck(1, Arena);
62 Predecessors.push_back(Pred);
63 for (auto *E : Args) {
64 if (auto *Ph = dyn_cast<Phi>(E)) {
65 Ph->values().reserveCheck(1, Arena);
66 Ph->values().push_back(nullptr);
72 void BasicBlock::reservePredecessors(unsigned NumPreds) {
73 Predecessors.reserve(NumPreds, Arena);
74 for (auto *E : Args) {
75 if (auto *Ph = dyn_cast<Phi>(E)) {
76 Ph->values().reserve(NumPreds, Arena);
81 // If E is a variable, then trace back through any aliases or redundant
82 // Phi nodes to find the canonical definition.
83 const SExpr *til::getCanonicalVal(const SExpr *E) {
85 if (const auto *V = dyn_cast<Variable>(E)) {
86 if (V->kind() == Variable::VK_Let) {
91 if (const auto *Ph = dyn_cast<Phi>(E)) {
92 if (Ph->status() == Phi::PH_SingleVal) {
102 // If E is a variable, then trace back through any aliases or redundant
103 // Phi nodes to find the canonical definition.
104 // The non-const version will simplify incomplete Phi nodes.
105 SExpr *til::simplifyToCanonicalVal(SExpr *E) {
107 if (auto *V = dyn_cast<Variable>(E)) {
108 if (V->kind() != Variable::VK_Let)
110 // Eliminate redundant variables, e.g. x = y, or x = 5,
111 // but keep anything more complicated.
112 if (til::ThreadSafetyTIL::isTrivial(V->definition())) {
118 if (auto *Ph = dyn_cast<Phi>(E)) {
119 if (Ph->status() == Phi::PH_Incomplete)
120 simplifyIncompleteArg(Ph);
121 // Eliminate redundant Phi nodes.
122 if (Ph->status() == Phi::PH_SingleVal) {
131 // Trace the arguments of an incomplete Phi node to see if they have the same
132 // canonical definition. If so, mark the Phi node as redundant.
133 // getCanonicalVal() will recursively call simplifyIncompletePhi().
134 void til::simplifyIncompleteArg(til::Phi *Ph) {
135 assert(Ph && Ph->status() == Phi::PH_Incomplete);
137 // eliminate infinite recursion -- assume that this node is not redundant.
138 Ph->setStatus(Phi::PH_MultiVal);
140 SExpr *E0 = simplifyToCanonicalVal(Ph->values()[0]);
141 for (unsigned i = 1, n = Ph->values().size(); i < n; ++i) {
142 SExpr *Ei = simplifyToCanonicalVal(Ph->values()[i]);
144 continue; // Recursive reference to itself. Don't count.
146 return; // Status is already set to MultiVal.
149 Ph->setStatus(Phi::PH_SingleVal);
152 // Renumbers the arguments and instructions to have unique, sequential IDs.
153 unsigned BasicBlock::renumberInstrs(unsigned ID) {
154 for (auto *Arg : Args)
155 Arg->setID(this, ID++);
156 for (auto *Instr : Instrs)
157 Instr->setID(this, ID++);
158 TermInstr->setID(this, ID++);
162 // Sorts the CFGs blocks using a reverse post-order depth-first traversal.
163 // Each block will be written into the Blocks array in order, and its BlockID
164 // will be set to the index in the array. Sorting should start from the entry
165 // block, and ID should be the total number of blocks.
166 unsigned BasicBlock::topologicalSort(SimpleArray<BasicBlock *> &Blocks,
168 if (Visited) return ID;
170 for (auto *Block : successors())
171 ID = Block->topologicalSort(Blocks, ID);
172 // set ID and update block array in place.
173 // We may lose pointers to unreachable blocks.
176 Blocks[BlockID] = this;
180 // Performs a reverse topological traversal, starting from the exit block and
181 // following back-edges. The dominator is serialized before any predecessors,
182 // which guarantees that all blocks are serialized after their dominator and
183 // before their post-dominator (because it's a reverse topological traversal).
184 // ID should be initially set to 0.
186 // This sort assumes that (1) dominators have been computed, (2) there are no
187 // critical edges, and (3) the entry block is reachable from the exit block
188 // and no blocks are accessible via traversal of back-edges from the exit that
189 // weren't accessible via forward edges from the entry.
190 unsigned BasicBlock::topologicalFinalSort(SimpleArray<BasicBlock *> &Blocks,
192 // Visited is assumed to have been set by the topologicalSort. This pass
193 // assumes !Visited means that we've visited this node before.
194 if (!Visited) return ID;
196 if (DominatorNode.Parent)
197 ID = DominatorNode.Parent->topologicalFinalSort(Blocks, ID);
198 for (auto *Pred : Predecessors)
199 ID = Pred->topologicalFinalSort(Blocks, ID);
200 assert(static_cast<size_t>(ID) < Blocks.size());
202 Blocks[BlockID] = this;
206 // Computes the immediate dominator of the current block. Assumes that all of
207 // its predecessors have already computed their dominators. This is achieved
208 // by visiting the nodes in topological order.
209 void BasicBlock::computeDominator() {
210 BasicBlock *Candidate = nullptr;
211 // Walk backwards from each predecessor to find the common dominator node.
212 for (auto *Pred : Predecessors) {
214 if (Pred->BlockID >= BlockID) continue;
215 // If we don't yet have a candidate for dominator yet, take this one.
216 if (Candidate == nullptr) {
220 // Walk the alternate and current candidate back to find a common ancestor.
221 auto *Alternate = Pred;
222 while (Alternate != Candidate) {
223 if (Candidate->BlockID > Alternate->BlockID)
224 Candidate = Candidate->DominatorNode.Parent;
226 Alternate = Alternate->DominatorNode.Parent;
229 DominatorNode.Parent = Candidate;
230 DominatorNode.SizeOfSubTree = 1;
233 // Computes the immediate post-dominator of the current block. Assumes that all
234 // of its successors have already computed their post-dominators. This is
235 // achieved visiting the nodes in reverse topological order.
236 void BasicBlock::computePostDominator() {
237 BasicBlock *Candidate = nullptr;
238 // Walk back from each predecessor to find the common post-dominator node.
239 for (auto *Succ : successors()) {
241 if (Succ->BlockID <= BlockID) continue;
242 // If we don't yet have a candidate for post-dominator yet, take this one.
243 if (Candidate == nullptr) {
247 // Walk the alternate and current candidate back to find a common ancestor.
248 auto *Alternate = Succ;
249 while (Alternate != Candidate) {
250 if (Candidate->BlockID < Alternate->BlockID)
251 Candidate = Candidate->PostDominatorNode.Parent;
253 Alternate = Alternate->PostDominatorNode.Parent;
256 PostDominatorNode.Parent = Candidate;
257 PostDominatorNode.SizeOfSubTree = 1;
260 // Renumber instructions in all blocks
261 void SCFG::renumberInstrs() {
262 unsigned InstrID = 0;
263 for (auto *Block : Blocks)
264 InstrID = Block->renumberInstrs(InstrID);
267 static inline void computeNodeSize(BasicBlock *B,
268 BasicBlock::TopologyNode BasicBlock::*TN) {
269 BasicBlock::TopologyNode *N = &(B->*TN);
271 BasicBlock::TopologyNode *P = &(N->Parent->*TN);
272 // Initially set ID relative to the (as yet uncomputed) parent ID
273 N->NodeID = P->SizeOfSubTree;
274 P->SizeOfSubTree += N->SizeOfSubTree;
278 static inline void computeNodeID(BasicBlock *B,
279 BasicBlock::TopologyNode BasicBlock::*TN) {
280 BasicBlock::TopologyNode *N = &(B->*TN);
282 BasicBlock::TopologyNode *P = &(N->Parent->*TN);
283 N->NodeID += P->NodeID; // Fix NodeIDs relative to starting node.
287 // Normalizes a CFG. Normalization has a few major components:
288 // 1) Removing unreachable blocks.
289 // 2) Computing dominators and post-dominators
290 // 3) Topologically sorting the blocks into the "Blocks" array.
291 void SCFG::computeNormalForm() {
292 // Topologically sort the blocks starting from the entry block.
293 unsigned NumUnreachableBlocks = Entry->topologicalSort(Blocks, Blocks.size());
294 if (NumUnreachableBlocks > 0) {
295 // If there were unreachable blocks shift everything down, and delete them.
296 for (unsigned I = NumUnreachableBlocks, E = Blocks.size(); I < E; ++I) {
297 unsigned NI = I - NumUnreachableBlocks;
298 Blocks[NI] = Blocks[I];
299 Blocks[NI]->BlockID = NI;
300 // FIXME: clean up predecessor pointers to unreachable blocks?
302 Blocks.drop(NumUnreachableBlocks);
305 // Compute dominators.
306 for (auto *Block : Blocks)
307 Block->computeDominator();
309 // Once dominators have been computed, the final sort may be performed.
310 unsigned NumBlocks = Exit->topologicalFinalSort(Blocks, 0);
311 assert(static_cast<size_t>(NumBlocks) == Blocks.size());
314 // Renumber the instructions now that we have a final sort.
317 // Compute post-dominators and compute the sizes of each node in the
319 for (auto *Block : Blocks.reverse()) {
320 Block->computePostDominator();
321 computeNodeSize(Block, &BasicBlock::DominatorNode);
323 // Compute the sizes of each node in the post-dominator tree and assign IDs in
324 // the dominator tree.
325 for (auto *Block : Blocks) {
326 computeNodeID(Block, &BasicBlock::DominatorNode);
327 computeNodeSize(Block, &BasicBlock::PostDominatorNode);
329 // Assign IDs in the post-dominator tree.
330 for (auto *Block : Blocks.reverse()) {
331 computeNodeID(Block, &BasicBlock::PostDominatorNode);