blob: 370cd9552997cbb19ab855e3f8f9690be0a11626 [file] [log] [blame]
Chris Lattnerd43023a2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner081aabc2001-07-02 05:46:38 +00009//
Chris Lattnerd43023a2002-08-02 16:43:03 +000010// This file implements the post-dominator construction algorithms.
Chris Lattner081aabc2001-07-02 05:46:38 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc86203a2002-08-21 23:43:50 +000014#include "llvm/Analysis/PostDominators.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000015#include "llvm/Instructions.h"
Chris Lattner60a65912002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/SetOperations.h"
Owen Anderson8313e752007-10-03 21:25:45 +000019#include "llvm/Analysis/DominatorInternals.h"
Chris Lattnerf9f7c2d2003-12-07 00:35:42 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattnerc385beb2001-07-06 16:58:22 +000022//===----------------------------------------------------------------------===//
Owen Andersonf35a1db2007-04-15 08:47:27 +000023// PostDominatorTree Implementation
Nate Begemand5811b92006-03-11 02:20:46 +000024//===----------------------------------------------------------------------===//
25
Devang Patel8c78a0b2007-05-03 01:11:54 +000026char PostDominatorTree::ID = 0;
27char PostDominanceFrontier::ID = 0;
Owen Andersonf35a1db2007-04-15 08:47:27 +000028static RegisterPass<PostDominatorTree>
29F("postdomtree", "Post-Dominator Tree Construction", true);
Nate Begemand5811b92006-03-11 02:20:46 +000030
Owen Andersonb60f2542007-10-03 03:20:17 +000031bool PostDominatorTree::runOnFunction(Function &F) {
Owen Anderson9c614112007-10-23 20:58:37 +000032 DT->recalculate(F);
Owen Andersonb60f2542007-10-03 03:20:17 +000033 return false;
34}
35
Chris Lattner6c9cbdb2006-01-08 08:22:18 +000036//===----------------------------------------------------------------------===//
Chris Lattnerd43023a2002-08-02 16:43:03 +000037// PostDominanceFrontier Implementation
Chris Lattner081aabc2001-07-02 05:46:38 +000038//===----------------------------------------------------------------------===//
39
Chris Lattner3c9b2422006-08-27 22:30:17 +000040static RegisterPass<PostDominanceFrontier>
Chris Lattner31aa7e72002-07-30 16:27:52 +000041H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattnerccf571a2002-01-31 00:42:27 +000042
Chris Lattner78dd56f2002-04-28 16:21:30 +000043const DominanceFrontier::DomSetType &
Misha Brukman01808ca2005-04-21 21:13:18 +000044PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Devang Patelbdd1aae2007-06-04 00:32:22 +000045 const DomTreeNode *Node) {
Chris Lattnerc385beb2001-07-06 16:58:22 +000046 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerbb9d03b2003-09-11 16:26:13 +000047 BasicBlock *BB = Node->getBlock();
Chris Lattnerc385beb2001-07-06 16:58:22 +000048 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner81575052003-09-10 20:37:08 +000049 if (getRoots().empty()) return S;
Chris Lattnerc385beb2001-07-06 16:58:22 +000050
Chris Lattner81575052003-09-10 20:37:08 +000051 if (BB)
52 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel883ff072007-04-18 01:19:55 +000053 SI != SE; ++SI) {
Misha Brukman32998322003-09-11 18:14:24 +000054 // Does Node immediately dominate this predecessor?
Devang Patelbdd1aae2007-06-04 00:32:22 +000055 DomTreeNode *SINode = DT[*SI];
Devang Patel883ff072007-04-18 01:19:55 +000056 if (SINode && SINode->getIDom() != Node)
Chris Lattner81575052003-09-10 20:37:08 +000057 S.insert(*SI);
Devang Patel883ff072007-04-18 01:19:55 +000058 }
Chris Lattnerc385beb2001-07-06 16:58:22 +000059
60 // At this point, S is DFlocal. Now we union in DFup's of our children...
61 // Loop through and visit the nodes that Node immediately dominates (Node's
62 // children in the IDomTree)
63 //
Devang Patelbdd1aae2007-06-04 00:32:22 +000064 for (DomTreeNode::const_iterator
Chris Lattner64eea742002-07-26 18:40:14 +000065 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Devang Patelbdd1aae2007-06-04 00:32:22 +000066 DomTreeNode *IDominee = *NI;
Chris Lattner64eea742002-07-26 18:40:14 +000067 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattnerc385beb2001-07-06 16:58:22 +000068
69 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
70 for (; CDFI != CDFE; ++CDFI) {
Devang Patelaf41e4a2007-06-07 17:47:21 +000071 if (!DT.properlyDominates(Node, DT[*CDFI]))
Misha Brukman77451162005-04-22 04:01:18 +000072 S.insert(*CDFI);
Chris Lattnerc385beb2001-07-06 16:58:22 +000073 }
74 }
75
76 return S;
77}
Chris Lattnerc86203a2002-08-21 23:43:50 +000078
Reid Spencerbe535662006-06-07 22:00:26 +000079// Ensure that this .cpp file gets linked when PostDominators.h is used.
80DEFINING_FILE_FOR(PostDominanceFrontier)