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