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