1 //===-- Local.h - Functions to perform local transformations -----*- C++ -*--=//
3 // This family of functions perform various local transformations to the
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Transforms/Utils/Local.h"
9 #include "llvm/iTerminators.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");
42 // Create our unconditional branch...
43 BranchInst *BI = new BranchInst(TI->getSuccessor(SuccNum));
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 // Now if we have a pass object, update analysis information. Currently we
54 // update DominatorSet and DominatorTree information if it's available.
57 // Should we update DominatorSet information?
58 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
59 // The blocks that dominate the new one are the blocks that dominate TIBB
60 // plus the new block itself.
61 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
62 DomSet.insert(NewBB); // A block always dominates itself.
63 DS->addBasicBlock(NewBB, DomSet);
66 // Should we update ImmdediateDominator information?
67 if (ImmediateDominators *ID =
68 P->getAnalysisToUpdate<ImmediateDominators>()) {
69 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
71 ID->addNewBlock(NewBB, TIBB);
74 // Should we update DominatorTree information?
75 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
76 DominatorTree::Node *TINode = DT->getNode(TIBB);
78 // The new block is not the immediate dominator for any other nodes, but
79 // TINode is the immediate dominator for the new node.
81 DT->createNewNode(NewBB, TINode);