1//===- PostOrderCFGView.cpp - Post order view of CFG blocks ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements post order view of the blocks in a CFG.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Analysis/Analyses/PostOrderCFGView.h"
14#include "clang/Analysis/AnalysisDeclContext.h"
15#include "clang/Analysis/CFG.h"
16
17using namespace clang;
18
19void PostOrderCFGView::anchor() {}
20
21PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
22 Blocks.reserve(n: cfg->getNumBlockIDs());
23 CFGBlockSet BSet(cfg);
24
25 for (po_iterator I = po_iterator::begin(G: cfg, S&: BSet),
26 E = po_iterator::end(G: cfg, S&: BSet); I != E; ++I) {
27 BlockOrder[*I] = Blocks.size() + 1;
28 Blocks.push_back(x: *I);
29 }
30}
31
32std::unique_ptr<PostOrderCFGView>
33PostOrderCFGView::create(AnalysisDeclContext &ctx) {
34 const CFG *cfg = ctx.getCFG();
35 if (!cfg)
36 return nullptr;
37 return std::make_unique<PostOrderCFGView>(args&: cfg);
38}
39
40const void *PostOrderCFGView::getTag() { static int x; return &x; }
41
42bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1,
43 const CFGBlock *b2) const {
44 PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(Val: b1);
45 PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(Val: b2);
46
47 unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
48 unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
49 return b1V > b2V;
50}
51