blob: 0ca738823129f06cd16735e94dc93bdda6bf19e5 [file] [log] [blame]
Chris Lattner4c9df7c2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner17152292001-07-02 05:46:38 +00009//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000010// This file implements the post-dominator construction algorithms.
Chris Lattner17152292001-07-02 05:46:38 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera69fd902002-08-21 23:43:50 +000014#include "llvm/Analysis/PostDominators.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000015#include "llvm/Instructions.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/SetOperations.h"
Owen Anderson04fa5692007-09-23 22:21:00 +000019#include "PostDominatorCalculation.h"
Chris Lattnercd7c2872003-12-07 00:35:42 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner94108ab2001-07-06 16:58:22 +000022//===----------------------------------------------------------------------===//
Owen Anderson3dc67762007-04-15 08:47:27 +000023// PostDominatorTree Implementation
Nate Begeman442b32b2006-03-11 02:20:46 +000024//===----------------------------------------------------------------------===//
25
Devang Patel19974732007-05-03 01:11:54 +000026char PostDominatorTree::ID = 0;
27char PostDominanceFrontier::ID = 0;
Owen Anderson3dc67762007-04-15 08:47:27 +000028static RegisterPass<PostDominatorTree>
29F("postdomtree", "Post-Dominator Tree Construction", true);
Nate Begeman442b32b2006-03-11 02:20:46 +000030
Owen Anderson471ab542007-10-03 03:20:17 +000031bool PostDominatorTree::runOnFunction(Function &F) {
32 reset(); // Reset from the last time we were run...
33
34 // Initialize the roots list
35 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
36 TerminatorInst *Insn = I->getTerminator();
37 if (Insn->getNumSuccessors() == 0) {
38 // Unreachable block is not a root node.
39 if (!isa<UnreachableInst>(Insn))
40 Roots.push_back(I);
41 }
42
43 // Prepopulate maps so that we don't get iterator invalidation issues later.
44 IDoms[I] = 0;
45 DomTreeNodes[I] = 0;
46 }
47
48 Vertex.push_back(0);
49
50 PDTcalculate(*this, F);
51 return false;
52}
53
Chris Lattnerccacd3c2006-01-08 08:22:18 +000054//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000055// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +000056//===----------------------------------------------------------------------===//
57
Chris Lattner5d8925c2006-08-27 22:30:17 +000058static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +000059H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +000060
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000061const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +000062PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Devang Patel26042422007-06-04 00:32:22 +000063 const DomTreeNode *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +000064 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +000065 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +000066 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +000067 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +000068
Chris Lattner706e61e2003-09-10 20:37:08 +000069 if (BB)
70 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel5a713cc2007-04-18 01:19:55 +000071 SI != SE; ++SI) {
Misha Brukman2f2d0652003-09-11 18:14:24 +000072 // Does Node immediately dominate this predecessor?
Devang Patel26042422007-06-04 00:32:22 +000073 DomTreeNode *SINode = DT[*SI];
Devang Patel5a713cc2007-04-18 01:19:55 +000074 if (SINode && SINode->getIDom() != Node)
Chris Lattner706e61e2003-09-10 20:37:08 +000075 S.insert(*SI);
Devang Patel5a713cc2007-04-18 01:19:55 +000076 }
Chris Lattner94108ab2001-07-06 16:58:22 +000077
78 // At this point, S is DFlocal. Now we union in DFup's of our children...
79 // Loop through and visit the nodes that Node immediately dominates (Node's
80 // children in the IDomTree)
81 //
Devang Patel26042422007-06-04 00:32:22 +000082 for (DomTreeNode::const_iterator
Chris Lattnerce6ef112002-07-26 18:40:14 +000083 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Devang Patel26042422007-06-04 00:32:22 +000084 DomTreeNode *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +000085 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +000086
87 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
88 for (; CDFI != CDFE; ++CDFI) {
Devang Patel9a511572007-06-07 17:47:21 +000089 if (!DT.properlyDominates(Node, DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +000090 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +000091 }
92 }
93
94 return S;
95}
Chris Lattnera69fd902002-08-21 23:43:50 +000096
Reid Spencer4f1bd9e2006-06-07 22:00:26 +000097// Ensure that this .cpp file gets linked when PostDominators.h is used.
98DEFINING_FILE_FOR(PostDominanceFrontier)