blob: c72b140a6224fec085d184253846e1dd8bd88d7f [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Owen Anderson1f23e162008-04-16 04:21:16 +000014#define DEBUG_TYPE "postdomtree"
15
Chris Lattnera69fd902002-08-21 23:43:50 +000016#include "llvm/Analysis/PostDominators.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Chris Lattner221d6882002-02-12 21:07:25 +000018#include "llvm/Support/CFG.h"
Owen Anderson1f23e162008-04-16 04:21:16 +000019#include "llvm/Support/Debug.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/DepthFirstIterator.h"
21#include "llvm/ADT/SetOperations.h"
Owen Anderson9cb7f492007-10-03 21:25:45 +000022#include "llvm/Analysis/DominatorInternals.h"
Chris Lattnercd7c2872003-12-07 00:35:42 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner94108ab2001-07-06 16:58:22 +000025//===----------------------------------------------------------------------===//
Owen Anderson3dc67762007-04-15 08:47:27 +000026// PostDominatorTree Implementation
Nate Begeman442b32b2006-03-11 02:20:46 +000027//===----------------------------------------------------------------------===//
28
Devang Patel19974732007-05-03 01:11:54 +000029char PostDominatorTree::ID = 0;
30char PostDominanceFrontier::ID = 0;
Owen Anderson3dc67762007-04-15 08:47:27 +000031static RegisterPass<PostDominatorTree>
Devang Patel9f835122008-03-20 23:27:18 +000032F("postdomtree", "Post-Dominator Tree Construction", true, true);
Nate Begeman442b32b2006-03-11 02:20:46 +000033
Owen Anderson471ab542007-10-03 03:20:17 +000034bool PostDominatorTree::runOnFunction(Function &F) {
Owen Andersond20cc142007-10-23 20:58:37 +000035 DT->recalculate(F);
Owen Anderson1f23e162008-04-16 04:21:16 +000036 DEBUG(DT->dump());
Owen Anderson471ab542007-10-03 03:20:17 +000037 return false;
38}
39
Torok Edwinf6055802008-05-03 20:25:26 +000040PostDominatorTree::~PostDominatorTree()
41{
42 delete DT;
43}
44
Chris Lattnerccacd3c2006-01-08 08:22:18 +000045//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000046// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +000047//===----------------------------------------------------------------------===//
48
Chris Lattner5d8925c2006-08-27 22:30:17 +000049static RegisterPass<PostDominanceFrontier>
Devang Patel9f835122008-03-20 23:27:18 +000050H("postdomfrontier", "Post-Dominance Frontier Construction", true, true);
Chris Lattner93193f82002-01-31 00:42:27 +000051
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000052const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +000053PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Devang Patel26042422007-06-04 00:32:22 +000054 const DomTreeNode *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +000055 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +000056 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +000057 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +000058 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +000059
Chris Lattner706e61e2003-09-10 20:37:08 +000060 if (BB)
61 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel5a713cc2007-04-18 01:19:55 +000062 SI != SE; ++SI) {
Misha Brukman2f2d0652003-09-11 18:14:24 +000063 // Does Node immediately dominate this predecessor?
Devang Patel26042422007-06-04 00:32:22 +000064 DomTreeNode *SINode = DT[*SI];
Devang Patel5a713cc2007-04-18 01:19:55 +000065 if (SINode && SINode->getIDom() != Node)
Chris Lattner706e61e2003-09-10 20:37:08 +000066 S.insert(*SI);
Devang Patel5a713cc2007-04-18 01:19:55 +000067 }
Chris Lattner94108ab2001-07-06 16:58:22 +000068
69 // At this point, S is DFlocal. Now we union in DFup's of our children...
70 // Loop through and visit the nodes that Node immediately dominates (Node's
71 // children in the IDomTree)
72 //
Devang Patel26042422007-06-04 00:32:22 +000073 for (DomTreeNode::const_iterator
Chris Lattnerce6ef112002-07-26 18:40:14 +000074 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Devang Patel26042422007-06-04 00:32:22 +000075 DomTreeNode *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +000076 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +000077
78 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
79 for (; CDFI != CDFE; ++CDFI) {
Devang Patel9a511572007-06-07 17:47:21 +000080 if (!DT.properlyDominates(Node, DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +000081 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +000082 }
83 }
84
85 return S;
86}
Chris Lattnera69fd902002-08-21 23:43:50 +000087
Reid Spencer4f1bd9e2006-06-07 22:00:26 +000088// Ensure that this .cpp file gets linked when PostDominators.h is used.
89DEFINING_FILE_FOR(PostDominanceFrontier)