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