1 //===- DomTreeUpdater.cpp - DomTree/Post DomTree Updater --------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the DomTreeUpdater class, which provides a uniform way 10 // to update dominator tree related data structures. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/DomTreeUpdater.h" 15 #include "llvm/ADT/SmallSet.h" 16 #include "llvm/Analysis/PostDominators.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/Instructions.h" 19 #include "llvm/Support/GenericDomTree.h" 20 #include <algorithm> 21 #include <functional> 22 #include <utility> 23 24 namespace llvm { 25 26 bool DomTreeUpdater::isUpdateValid( 27 const DominatorTree::UpdateType Update) const { 28 const auto *From = Update.getFrom(); 29 const auto *To = Update.getTo(); 30 const auto Kind = Update.getKind(); 31 32 // Discard updates by inspecting the current state of successors of From. 33 // Since isUpdateValid() must be called *after* the Terminator of From is 34 // altered we can determine if the update is unnecessary for batch updates 35 // or invalid for a single update. 36 const bool HasEdge = llvm::is_contained(successors(From), To); 37 38 // If the IR does not match the update, 39 // 1. In batch updates, this update is unnecessary. 40 // 2. When called by insertEdge*()/deleteEdge*(), this update is invalid. 41 // Edge does not exist in IR. 42 if (Kind == DominatorTree::Insert && !HasEdge) 43 return false; 44 45 // Edge exists in IR. 46 if (Kind == DominatorTree::Delete && HasEdge) 47 return false; 48 49 return true; 50 } 51 52 bool DomTreeUpdater::isSelfDominance( 53 const DominatorTree::UpdateType Update) const { 54 // Won't affect DomTree and PostDomTree. 55 return Update.getFrom() == Update.getTo(); 56 } 57 58 void DomTreeUpdater::applyDomTreeUpdates() { 59 // No pending DomTreeUpdates. 60 if (Strategy != UpdateStrategy::Lazy || !DT) 61 return; 62 63 // Only apply updates not are applied by DomTree. 64 if (hasPendingDomTreeUpdates()) { 65 const auto I = PendUpdates.begin() + PendDTUpdateIndex; 66 const auto E = PendUpdates.end(); 67 assert(I < E && "Iterator range invalid; there should be DomTree updates."); 68 DT->applyUpdates(ArrayRef<DominatorTree::UpdateType>(I, E)); 69 PendDTUpdateIndex = PendUpdates.size(); 70 } 71 } 72 73 void DomTreeUpdater::flush() { 74 applyDomTreeUpdates(); 75 applyPostDomTreeUpdates(); 76 dropOutOfDateUpdates(); 77 } 78 79 void DomTreeUpdater::applyPostDomTreeUpdates() { 80 // No pending PostDomTreeUpdates. 81 if (Strategy != UpdateStrategy::Lazy || !PDT) 82 return; 83 84 // Only apply updates not are applied by PostDomTree. 85 if (hasPendingPostDomTreeUpdates()) { 86 const auto I = PendUpdates.begin() + PendPDTUpdateIndex; 87 const auto E = PendUpdates.end(); 88 assert(I < E && 89 "Iterator range invalid; there should be PostDomTree updates."); 90 PDT->applyUpdates(ArrayRef<DominatorTree::UpdateType>(I, E)); 91 PendPDTUpdateIndex = PendUpdates.size(); 92 } 93 } 94 95 void DomTreeUpdater::tryFlushDeletedBB() { 96 if (!hasPendingUpdates()) 97 forceFlushDeletedBB(); 98 } 99 100 bool DomTreeUpdater::forceFlushDeletedBB() { 101 if (DeletedBBs.empty()) 102 return false; 103 104 for (auto *BB : DeletedBBs) { 105 // After calling deleteBB or callbackDeleteBB under Lazy UpdateStrategy, 106 // validateDeleteBB() removes all instructions of DelBB and adds an 107 // UnreachableInst as its terminator. So we check whether the BasicBlock to 108 // delete only has an UnreachableInst inside. 109 assert(BB->getInstList().size() == 1 && 110 isa<UnreachableInst>(BB->getTerminator()) && 111 "DelBB has been modified while awaiting deletion."); 112 BB->removeFromParent(); 113 eraseDelBBNode(BB); 114 delete BB; 115 } 116 DeletedBBs.clear(); 117 Callbacks.clear(); 118 return true; 119 } 120 121 void DomTreeUpdater::recalculate(Function &F) { 122 123 if (Strategy == UpdateStrategy::Eager) { 124 if (DT) 125 DT->recalculate(F); 126 if (PDT) 127 PDT->recalculate(F); 128 return; 129 } 130 131 // There is little performance gain if we pend the recalculation under 132 // Lazy UpdateStrategy so we recalculate available trees immediately. 133 134 // Prevent forceFlushDeletedBB() from erasing DomTree or PostDomTree nodes. 135 IsRecalculatingDomTree = IsRecalculatingPostDomTree = true; 136 137 // Because all trees are going to be up-to-date after recalculation, 138 // flush awaiting deleted BasicBlocks. 139 forceFlushDeletedBB(); 140 if (DT) 141 DT->recalculate(F); 142 if (PDT) 143 PDT->recalculate(F); 144 145 // Resume forceFlushDeletedBB() to erase DomTree or PostDomTree nodes. 146 IsRecalculatingDomTree = IsRecalculatingPostDomTree = false; 147 PendDTUpdateIndex = PendPDTUpdateIndex = PendUpdates.size(); 148 dropOutOfDateUpdates(); 149 } 150 151 bool DomTreeUpdater::hasPendingUpdates() const { 152 return hasPendingDomTreeUpdates() || hasPendingPostDomTreeUpdates(); 153 } 154 155 bool DomTreeUpdater::hasPendingDomTreeUpdates() const { 156 if (!DT) 157 return false; 158 return PendUpdates.size() != PendDTUpdateIndex; 159 } 160 161 bool DomTreeUpdater::hasPendingPostDomTreeUpdates() const { 162 if (!PDT) 163 return false; 164 return PendUpdates.size() != PendPDTUpdateIndex; 165 } 166 167 bool DomTreeUpdater::isBBPendingDeletion(llvm::BasicBlock *DelBB) const { 168 if (Strategy == UpdateStrategy::Eager || DeletedBBs.empty()) 169 return false; 170 return DeletedBBs.contains(DelBB); 171 } 172 173 // The DT and PDT require the nodes related to updates 174 // are not deleted when update functions are called. 175 // So BasicBlock deletions must be pended when the 176 // UpdateStrategy is Lazy. When the UpdateStrategy is 177 // Eager, the BasicBlock will be deleted immediately. 178 void DomTreeUpdater::deleteBB(BasicBlock *DelBB) { 179 validateDeleteBB(DelBB); 180 if (Strategy == UpdateStrategy::Lazy) { 181 DeletedBBs.insert(DelBB); 182 return; 183 } 184 185 DelBB->removeFromParent(); 186 eraseDelBBNode(DelBB); 187 delete DelBB; 188 } 189 190 void DomTreeUpdater::callbackDeleteBB( 191 BasicBlock *DelBB, std::function<void(BasicBlock *)> Callback) { 192 validateDeleteBB(DelBB); 193 if (Strategy == UpdateStrategy::Lazy) { 194 Callbacks.push_back(CallBackOnDeletion(DelBB, Callback)); 195 DeletedBBs.insert(DelBB); 196 return; 197 } 198 199 DelBB->removeFromParent(); 200 eraseDelBBNode(DelBB); 201 Callback(DelBB); 202 delete DelBB; 203 } 204 205 void DomTreeUpdater::eraseDelBBNode(BasicBlock *DelBB) { 206 if (DT && !IsRecalculatingDomTree) 207 if (DT->getNode(DelBB)) 208 DT->eraseNode(DelBB); 209 210 if (PDT && !IsRecalculatingPostDomTree) 211 if (PDT->getNode(DelBB)) 212 PDT->eraseNode(DelBB); 213 } 214 215 void DomTreeUpdater::validateDeleteBB(BasicBlock *DelBB) { 216 assert(DelBB && "Invalid push_back of nullptr DelBB."); 217 assert(pred_empty(DelBB) && "DelBB has one or more predecessors."); 218 // DelBB is unreachable and all its instructions are dead. 219 while (!DelBB->empty()) { 220 Instruction &I = DelBB->back(); 221 // Replace used instructions with an arbitrary value (undef). 222 if (!I.use_empty()) 223 I.replaceAllUsesWith(llvm::UndefValue::get(I.getType())); 224 DelBB->getInstList().pop_back(); 225 } 226 // Make sure DelBB has a valid terminator instruction. As long as DelBB is a 227 // Child of Function F it must contain valid IR. 228 new UnreachableInst(DelBB->getContext(), DelBB); 229 } 230 231 void DomTreeUpdater::applyUpdates(ArrayRef<DominatorTree::UpdateType> Updates) { 232 if (!DT && !PDT) 233 return; 234 235 if (Strategy == UpdateStrategy::Lazy) { 236 PendUpdates.reserve(PendUpdates.size() + Updates.size()); 237 for (const auto &U : Updates) 238 if (!isSelfDominance(U)) 239 PendUpdates.push_back(U); 240 241 return; 242 } 243 244 if (DT) 245 DT->applyUpdates(Updates); 246 if (PDT) 247 PDT->applyUpdates(Updates); 248 } 249 250 void DomTreeUpdater::applyUpdatesPermissive( 251 ArrayRef<DominatorTree::UpdateType> Updates) { 252 if (!DT && !PDT) 253 return; 254 255 SmallSet<std::pair<BasicBlock *, BasicBlock *>, 8> Seen; 256 SmallVector<DominatorTree::UpdateType, 8> DeduplicatedUpdates; 257 for (const auto &U : Updates) { 258 auto Edge = std::make_pair(U.getFrom(), U.getTo()); 259 // Because it is illegal to submit updates that have already been applied 260 // and updates to an edge need to be strictly ordered, 261 // it is safe to infer the existence of an edge from the first update 262 // to this edge. 263 // If the first update to an edge is "Delete", it means that the edge 264 // existed before. If the first update to an edge is "Insert", it means 265 // that the edge didn't exist before. 266 // 267 // For example, if the user submits {{Delete, A, B}, {Insert, A, B}}, 268 // because 269 // 1. it is illegal to submit updates that have already been applied, 270 // i.e., user cannot delete an nonexistent edge, 271 // 2. updates to an edge need to be strictly ordered, 272 // So, initially edge A -> B existed. 273 // We can then safely ignore future updates to this edge and directly 274 // inspect the current CFG: 275 // a. If the edge still exists, because the user cannot insert an existent 276 // edge, so both {Delete, A, B}, {Insert, A, B} actually happened and 277 // resulted in a no-op. DTU won't submit any update in this case. 278 // b. If the edge doesn't exist, we can then infer that {Delete, A, B} 279 // actually happened but {Insert, A, B} was an invalid update which never 280 // happened. DTU will submit {Delete, A, B} in this case. 281 if (!isSelfDominance(U) && Seen.count(Edge) == 0) { 282 Seen.insert(Edge); 283 // If the update doesn't appear in the CFG, it means that 284 // either the change isn't made or relevant operations 285 // result in a no-op. 286 if (isUpdateValid(U)) { 287 if (isLazy()) 288 PendUpdates.push_back(U); 289 else 290 DeduplicatedUpdates.push_back(U); 291 } 292 } 293 } 294 295 if (Strategy == UpdateStrategy::Lazy) 296 return; 297 298 if (DT) 299 DT->applyUpdates(DeduplicatedUpdates); 300 if (PDT) 301 PDT->applyUpdates(DeduplicatedUpdates); 302 } 303 304 DominatorTree &DomTreeUpdater::getDomTree() { 305 assert(DT && "Invalid acquisition of a null DomTree"); 306 applyDomTreeUpdates(); 307 dropOutOfDateUpdates(); 308 return *DT; 309 } 310 311 PostDominatorTree &DomTreeUpdater::getPostDomTree() { 312 assert(PDT && "Invalid acquisition of a null PostDomTree"); 313 applyPostDomTreeUpdates(); 314 dropOutOfDateUpdates(); 315 return *PDT; 316 } 317 318 void DomTreeUpdater::insertEdge(BasicBlock *From, BasicBlock *To) { 319 320 #ifndef NDEBUG 321 assert(isUpdateValid({DominatorTree::Insert, From, To}) && 322 "Inserted edge does not appear in the CFG"); 323 #endif 324 325 if (!DT && !PDT) 326 return; 327 328 // Won't affect DomTree and PostDomTree; discard update. 329 if (From == To) 330 return; 331 332 if (Strategy == UpdateStrategy::Eager) { 333 if (DT) 334 DT->insertEdge(From, To); 335 if (PDT) 336 PDT->insertEdge(From, To); 337 return; 338 } 339 340 PendUpdates.push_back({DominatorTree::Insert, From, To}); 341 } 342 343 void DomTreeUpdater::insertEdgeRelaxed(BasicBlock *From, BasicBlock *To) { 344 if (From == To) 345 return; 346 347 if (!DT && !PDT) 348 return; 349 350 if (!isUpdateValid({DominatorTree::Insert, From, To})) 351 return; 352 353 if (Strategy == UpdateStrategy::Eager) { 354 if (DT) 355 DT->insertEdge(From, To); 356 if (PDT) 357 PDT->insertEdge(From, To); 358 return; 359 } 360 361 PendUpdates.push_back({DominatorTree::Insert, From, To}); 362 } 363 364 void DomTreeUpdater::deleteEdge(BasicBlock *From, BasicBlock *To) { 365 366 #ifndef NDEBUG 367 assert(isUpdateValid({DominatorTree::Delete, From, To}) && 368 "Deleted edge still exists in the CFG!"); 369 #endif 370 371 if (!DT && !PDT) 372 return; 373 374 // Won't affect DomTree and PostDomTree; discard update. 375 if (From == To) 376 return; 377 378 if (Strategy == UpdateStrategy::Eager) { 379 if (DT) 380 DT->deleteEdge(From, To); 381 if (PDT) 382 PDT->deleteEdge(From, To); 383 return; 384 } 385 386 PendUpdates.push_back({DominatorTree::Delete, From, To}); 387 } 388 389 void DomTreeUpdater::deleteEdgeRelaxed(BasicBlock *From, BasicBlock *To) { 390 if (From == To) 391 return; 392 393 if (!DT && !PDT) 394 return; 395 396 if (!isUpdateValid({DominatorTree::Delete, From, To})) 397 return; 398 399 if (Strategy == UpdateStrategy::Eager) { 400 if (DT) 401 DT->deleteEdge(From, To); 402 if (PDT) 403 PDT->deleteEdge(From, To); 404 return; 405 } 406 407 PendUpdates.push_back({DominatorTree::Delete, From, To}); 408 } 409 410 void DomTreeUpdater::dropOutOfDateUpdates() { 411 if (Strategy == DomTreeUpdater::UpdateStrategy::Eager) 412 return; 413 414 tryFlushDeletedBB(); 415 416 // Drop all updates applied by both trees. 417 if (!DT) 418 PendDTUpdateIndex = PendUpdates.size(); 419 if (!PDT) 420 PendPDTUpdateIndex = PendUpdates.size(); 421 422 const size_t dropIndex = std::min(PendDTUpdateIndex, PendPDTUpdateIndex); 423 const auto B = PendUpdates.begin(); 424 const auto E = PendUpdates.begin() + dropIndex; 425 assert(B <= E && "Iterator out of range."); 426 PendUpdates.erase(B, E); 427 // Calculate current index. 428 PendDTUpdateIndex -= dropIndex; 429 PendPDTUpdateIndex -= dropIndex; 430 } 431 432 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 433 LLVM_DUMP_METHOD void DomTreeUpdater::dump() const { 434 raw_ostream &OS = llvm::dbgs(); 435 436 OS << "Available Trees: "; 437 if (DT || PDT) { 438 if (DT) 439 OS << "DomTree "; 440 if (PDT) 441 OS << "PostDomTree "; 442 OS << "\n"; 443 } else 444 OS << "None\n"; 445 446 OS << "UpdateStrategy: "; 447 if (Strategy == UpdateStrategy::Eager) { 448 OS << "Eager\n"; 449 return; 450 } else 451 OS << "Lazy\n"; 452 int Index = 0; 453 454 auto printUpdates = 455 [&](ArrayRef<DominatorTree::UpdateType>::const_iterator begin, 456 ArrayRef<DominatorTree::UpdateType>::const_iterator end) { 457 if (begin == end) 458 OS << " None\n"; 459 Index = 0; 460 for (auto It = begin, ItEnd = end; It != ItEnd; ++It) { 461 auto U = *It; 462 OS << " " << Index << " : "; 463 ++Index; 464 if (U.getKind() == DominatorTree::Insert) 465 OS << "Insert, "; 466 else 467 OS << "Delete, "; 468 BasicBlock *From = U.getFrom(); 469 if (From) { 470 auto S = From->getName(); 471 if (!From->hasName()) 472 S = "(no name)"; 473 OS << S << "(" << From << "), "; 474 } else { 475 OS << "(badref), "; 476 } 477 BasicBlock *To = U.getTo(); 478 if (To) { 479 auto S = To->getName(); 480 if (!To->hasName()) 481 S = "(no_name)"; 482 OS << S << "(" << To << ")\n"; 483 } else { 484 OS << "(badref)\n"; 485 } 486 } 487 }; 488 489 if (DT) { 490 const auto I = PendUpdates.begin() + PendDTUpdateIndex; 491 assert(PendUpdates.begin() <= I && I <= PendUpdates.end() && 492 "Iterator out of range."); 493 OS << "Applied but not cleared DomTreeUpdates:\n"; 494 printUpdates(PendUpdates.begin(), I); 495 OS << "Pending DomTreeUpdates:\n"; 496 printUpdates(I, PendUpdates.end()); 497 } 498 499 if (PDT) { 500 const auto I = PendUpdates.begin() + PendPDTUpdateIndex; 501 assert(PendUpdates.begin() <= I && I <= PendUpdates.end() && 502 "Iterator out of range."); 503 OS << "Applied but not cleared PostDomTreeUpdates:\n"; 504 printUpdates(PendUpdates.begin(), I); 505 OS << "Pending PostDomTreeUpdates:\n"; 506 printUpdates(I, PendUpdates.end()); 507 } 508 509 OS << "Pending DeletedBBs:\n"; 510 Index = 0; 511 for (const auto *BB : DeletedBBs) { 512 OS << " " << Index << " : "; 513 ++Index; 514 if (BB->hasName()) 515 OS << BB->getName() << "("; 516 else 517 OS << "(no_name)("; 518 OS << BB << ")\n"; 519 } 520 521 OS << "Pending Callbacks:\n"; 522 Index = 0; 523 for (const auto &BB : Callbacks) { 524 OS << " " << Index << " : "; 525 ++Index; 526 if (BB->hasName()) 527 OS << BB->getName() << "("; 528 else 529 OS << "(no_name)("; 530 OS << BB << ")\n"; 531 } 532 } 533 #endif 534 } // namespace llvm 535