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