|
13 | 13 | #include "llvm/Transforms/Utils/ControlFlowUtils.h" |
14 | 14 | #include "llvm/ADT/SetVector.h" |
15 | 15 | #include "llvm/Analysis/DomTreeUpdater.h" |
| 16 | +#include "llvm/Analysis/LoopInfo.h" |
16 | 17 | #include "llvm/IR/Constants.h" |
17 | 18 | #include "llvm/IR/Instructions.h" |
18 | 19 | #include "llvm/IR/ValueHandle.h" |
@@ -281,7 +282,9 @@ std::pair<BasicBlock *, bool> ControlFlowHub::finalize( |
281 | 282 |
|
282 | 283 | for (auto [BB, Succ0, Succ1] : Branches) { |
283 | 284 | #ifndef NDEBUG |
284 | | - assert(Incoming.insert(BB).second && "Duplicate entry for incoming block."); |
| 285 | + assert( |
| 286 | + (Incoming.insert(BB).second || isa<CallBrInst>(BB->getTerminator())) && |
| 287 | + "Duplicate entry for incoming block."); |
285 | 288 | #endif |
286 | 289 | if (Succ0) |
287 | 290 | Outgoing.insert(Succ0); |
@@ -341,3 +344,64 @@ std::pair<BasicBlock *, bool> ControlFlowHub::finalize( |
341 | 344 |
|
342 | 345 | return {FirstGuardBlock, true}; |
343 | 346 | } |
| 347 | + |
| 348 | +/// Helper function to update the cycle or loop information after inserting a |
| 349 | +/// new block between a callbr instruction and one of its target blocks. Adds |
| 350 | +/// the new block to the innermost cycle or loop that the callbr instruction and |
| 351 | +/// the original target block share. |
| 352 | +/// \p LCI cycle or loop information to update |
| 353 | +/// \p CallBrBlock block containing the callbr instruction |
| 354 | +/// \p CallBrTarget new target block of the callbr instruction |
| 355 | +/// \p Succ original target block of the callbr instruction |
| 356 | +template <typename TI, typename T> |
| 357 | +static bool updateCycleLoopInfo(TI *LCI, BasicBlock *CallBrBlock, |
| 358 | + BasicBlock *CallBrTarget, BasicBlock *Succ) { |
| 359 | + static_assert(std::is_same_v<TI, CycleInfo> || std::is_same_v<TI, LoopInfo>, |
| 360 | + "type must be CycleInfo or LoopInfo"); |
| 361 | + if (!LCI) |
| 362 | + return false; |
| 363 | + |
| 364 | + T *LC; |
| 365 | + if constexpr (std::is_same_v<TI, CycleInfo>) |
| 366 | + LC = LCI->getSmallestCommonCycle(CallBrBlock, Succ); |
| 367 | + else |
| 368 | + LC = LCI->getSmallestCommonLoop(CallBrBlock, Succ); |
| 369 | + if (!LC) |
| 370 | + return false; |
| 371 | + |
| 372 | + if constexpr (std::is_same_v<TI, CycleInfo>) |
| 373 | + LCI->addBlockToCycle(CallBrTarget, LC); |
| 374 | + else |
| 375 | + LC->addBasicBlockToLoop(CallBrTarget, *LCI); |
| 376 | + |
| 377 | + return true; |
| 378 | +} |
| 379 | + |
| 380 | +BasicBlock *ControlFlowHub::createCallBrTarget(CallBrInst *CallBr, |
| 381 | + BasicBlock *Succ, |
| 382 | + unsigned SuccIdx, CycleInfo *CI, |
| 383 | + DomTreeUpdater *DTU, |
| 384 | + LoopInfo *LI, bool *UpdatedLI) { |
| 385 | + BasicBlock *CallBrBlock = CallBr->getParent(); |
| 386 | + BasicBlock *CallBrTarget = |
| 387 | + BasicBlock::Create(CallBrBlock->getContext(), |
| 388 | + CallBrBlock->getName() + ".target." + Succ->getName(), |
| 389 | + CallBrBlock->getParent()); |
| 390 | + // Rewire control flow from callbr to the new target block. |
| 391 | + Succ->replacePhiUsesWith(CallBrBlock, CallBrTarget); |
| 392 | + CallBr->setSuccessor(SuccIdx, CallBrTarget); |
| 393 | + // Jump from the new target block to the original successor. |
| 394 | + BranchInst::Create(Succ, CallBrTarget); |
| 395 | + bool Updated = |
| 396 | + updateCycleLoopInfo<LoopInfo, Loop>(LI, CallBrBlock, CallBrTarget, Succ); |
| 397 | + if (UpdatedLI) |
| 398 | + *UpdatedLI = Updated; |
| 399 | + updateCycleLoopInfo<CycleInfo, Cycle>(CI, CallBrBlock, CallBrTarget, Succ); |
| 400 | + if (DTU) { |
| 401 | + DTU->applyUpdates({{DominatorTree::Insert, CallBrBlock, CallBrTarget}}); |
| 402 | + if (DTU->getDomTree().dominates(CallBrBlock, Succ)) |
| 403 | + DTU->applyUpdates({{DominatorTree::Delete, CallBrBlock, Succ}, |
| 404 | + {DominatorTree::Insert, CallBrTarget, Succ}}); |
| 405 | + } |
| 406 | + return CallBrTarget; |
| 407 | +} |
0 commit comments