1 //===-- CriticalEdge.cpp - Functions to detect and split critical edges ---===//
3 // These functions are here to detect and split critical edges in the CFG.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Transforms/Utils/Local.h"
8 #include "llvm/iTerminators.h"
9 #include "llvm/iPHINode.h"
10 #include "llvm/Analysis/Dominators.h"
11 #include "llvm/Support/CFG.h"
13 // isCriticalEdge - Return true if the specified edge is a critical edge.
14 // Critical edges are edges from a block with multiple successors to a block
15 // with multiple predecessors.
17 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
18 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
19 if (TI->getNumSuccessors() <= 1) return false;
21 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
22 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
24 // If there is more than one predecessor, this is a critical edge...
25 assert(I != E && "No preds, but we have an edge to the block?");
26 ++I; // Skip one edge due to the incoming arc from TI.
30 // SplitCriticalEdge - Insert a new node node to split the critical edge. This
31 // will update DominatorSet, ImmediateDominator and DominatorTree information if
32 // it is available, thus calling this pass will not invalidate either of them.
34 void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
35 assert(isCriticalEdge(TI, SuccNum) &&
36 "Cannot break a critical edge, if it isn't a critical edge");
37 BasicBlock *TIBB = TI->getParent();
39 // Create a new basic block, linking it into the CFG.
40 BasicBlock *NewBB = new BasicBlock(TIBB->getName()+"_crit_edge");
41 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
42 // Create our unconditional branch...
43 BranchInst *BI = new BranchInst(DestBB);
44 NewBB->getInstList().push_back(BI);
46 // Branch to the new block, breaking the edge...
47 TI->setSuccessor(SuccNum, NewBB);
49 // Insert the block into the function... right after the block TI lives in.
50 Function &F = *TIBB->getParent();
51 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
53 // If there are any PHI nodes in DestBB, we need to update them so that they
54 // merge incoming values from NewBB instead of from TIBB.
56 for (BasicBlock::iterator I = DestBB->begin();
57 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
58 // We no longer enter through TIBB, now we come in through NewBB.
59 PN->replaceUsesOfWith(TIBB, NewBB);
62 // Now if we have a pass object, update analysis information. Currently we
63 // update DominatorSet and DominatorTree information if it's available.
66 // Should we update DominatorSet information?
67 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
68 // The blocks that dominate the new one are the blocks that dominate TIBB
69 // plus the new block itself.
70 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
71 DomSet.insert(NewBB); // A block always dominates itself.
72 DS->addBasicBlock(NewBB, DomSet);
75 // Should we update ImmdediateDominator information?
76 if (ImmediateDominators *ID =
77 P->getAnalysisToUpdate<ImmediateDominators>()) {
78 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
80 ID->addNewBlock(NewBB, TIBB);
83 // Should we update DominatorTree information?
84 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
85 DominatorTree::Node *TINode = DT->getNode(TIBB);
87 // The new block is not the immediate dominator for any other nodes, but
88 // TINode is the immediate dominator for the new node.
90 DT->createNewNode(NewBB, TINode);