blob: 5a3c8182a140009a1efe2cbdd89b030b3d78d9e4 [file] [log] [blame]
Ted Kremenek5abde7c2011-10-22 02:14:23 +00001//===- 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//
Artyom Skrobov27720762014-09-23 08:34:41 +000010// This file implements post order view of the blocks in a CFG.
Ted Kremenek5abde7c2011-10-22 02:14:23 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/Analyses/PostOrderCFGView.h"
15
16using namespace clang;
17
David Blaikie68e081d2011-12-20 02:48:34 +000018void PostOrderCFGView::anchor() { }
19
Artyom Skrobov27720762014-09-23 08:34:41 +000020PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
Ted Kremenek5abde7c2011-10-22 02:14:23 +000021 Blocks.reserve(cfg->getNumBlockIDs());
22 CFGBlockSet BSet(cfg);
Artyom Skrobov27720762014-09-23 08:34:41 +000023
Ted Kremenek5abde7c2011-10-22 02:14:23 +000024 for (po_iterator I = po_iterator::begin(cfg, BSet),
25 E = po_iterator::end(cfg, BSet); I != E; ++I) {
Artyom Skrobov27720762014-09-23 08:34:41 +000026 BlockOrder[*I] = Blocks.size() + 1;
Ted Kremenek5abde7c2011-10-22 02:14:23 +000027 Blocks.push_back(*I);
28 }
29}
30
Ted Kremenek81ce1c82011-10-24 01:32:45 +000031PostOrderCFGView *PostOrderCFGView::create(AnalysisDeclContext &ctx) {
Ted Kremenek5abde7c2011-10-22 02:14:23 +000032 const CFG *cfg = ctx.getCFG();
33 if (!cfg)
Craig Topper25542942014-05-20 04:30:07 +000034 return nullptr;
Ted Kremenek5abde7c2011-10-22 02:14:23 +000035 return new PostOrderCFGView(cfg);
36}
37
38const void *PostOrderCFGView::getTag() { static int x; return &x; }
39
40bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1,
41 const CFGBlock *b2) const {
42 PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1);
43 PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(b2);
44
45 unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
46 unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
47 return b1V > b2V;
48}
49