xref: /minix3/external/bsd/llvm/dist/clang/lib/Analysis/PostOrderCFGView.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- PostOrderCFGView.cpp - Post order view of CFG blocks -------*- C++ --*-//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements post order view of the blocks in a CFG.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Analysis/Analyses/PostOrderCFGView.h"
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc using namespace clang;
17f4a2713aSLionel Sambuc 
anchor()18f4a2713aSLionel Sambuc void PostOrderCFGView::anchor() { }
19f4a2713aSLionel Sambuc 
PostOrderCFGView(const CFG * cfg)20f4a2713aSLionel Sambuc PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
21f4a2713aSLionel Sambuc   Blocks.reserve(cfg->getNumBlockIDs());
22f4a2713aSLionel Sambuc   CFGBlockSet BSet(cfg);
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc   for (po_iterator I = po_iterator::begin(cfg, BSet),
25f4a2713aSLionel Sambuc                    E = po_iterator::end(cfg, BSet); I != E; ++I) {
26f4a2713aSLionel Sambuc     BlockOrder[*I] = Blocks.size() + 1;
27f4a2713aSLionel Sambuc     Blocks.push_back(*I);
28f4a2713aSLionel Sambuc   }
29f4a2713aSLionel Sambuc }
30f4a2713aSLionel Sambuc 
create(AnalysisDeclContext & ctx)31f4a2713aSLionel Sambuc PostOrderCFGView *PostOrderCFGView::create(AnalysisDeclContext &ctx) {
32f4a2713aSLionel Sambuc   const CFG *cfg = ctx.getCFG();
33f4a2713aSLionel Sambuc   if (!cfg)
34*0a6a1f1dSLionel Sambuc     return nullptr;
35f4a2713aSLionel Sambuc   return new PostOrderCFGView(cfg);
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
getTag()38f4a2713aSLionel Sambuc const void *PostOrderCFGView::getTag() { static int x; return &x; }
39f4a2713aSLionel Sambuc 
operator ()(const CFGBlock * b1,const CFGBlock * b2) const40f4a2713aSLionel Sambuc bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1,
41f4a2713aSLionel Sambuc                                                      const CFGBlock *b2) const {
42f4a2713aSLionel Sambuc   PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1);
43f4a2713aSLionel Sambuc   PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(b2);
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc   unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
46f4a2713aSLionel Sambuc   unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
47f4a2713aSLionel Sambuc   return b1V > b2V;
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc 
50