xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Analysis/PostOrderCFGView.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===- PostOrderCFGView.cpp - Post order view of CFG blocks ---------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements post order view of the blocks in a CFG.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg 
137330f729Sjoerg #include "clang/Analysis/Analyses/PostOrderCFGView.h"
147330f729Sjoerg #include "clang/Analysis/AnalysisDeclContext.h"
157330f729Sjoerg #include "clang/Analysis/CFG.h"
167330f729Sjoerg 
177330f729Sjoerg using namespace clang;
187330f729Sjoerg 
anchor()197330f729Sjoerg void PostOrderCFGView::anchor() {}
207330f729Sjoerg 
PostOrderCFGView(const CFG * cfg)217330f729Sjoerg PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
227330f729Sjoerg   Blocks.reserve(cfg->getNumBlockIDs());
237330f729Sjoerg   CFGBlockSet BSet(cfg);
247330f729Sjoerg 
257330f729Sjoerg   for (po_iterator I = po_iterator::begin(cfg, BSet),
267330f729Sjoerg                    E = po_iterator::end(cfg, BSet); I != E; ++I) {
277330f729Sjoerg     BlockOrder[*I] = Blocks.size() + 1;
287330f729Sjoerg     Blocks.push_back(*I);
297330f729Sjoerg   }
307330f729Sjoerg }
317330f729Sjoerg 
32*e038c9c4Sjoerg std::unique_ptr<PostOrderCFGView>
create(AnalysisDeclContext & ctx)33*e038c9c4Sjoerg PostOrderCFGView::create(AnalysisDeclContext &ctx) {
347330f729Sjoerg   const CFG *cfg = ctx.getCFG();
357330f729Sjoerg   if (!cfg)
367330f729Sjoerg     return nullptr;
37*e038c9c4Sjoerg   return std::make_unique<PostOrderCFGView>(cfg);
387330f729Sjoerg }
397330f729Sjoerg 
getTag()407330f729Sjoerg const void *PostOrderCFGView::getTag() { static int x; return &x; }
417330f729Sjoerg 
operator ()(const CFGBlock * b1,const CFGBlock * b2) const427330f729Sjoerg bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1,
437330f729Sjoerg                                                      const CFGBlock *b2) const {
447330f729Sjoerg   PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1);
457330f729Sjoerg   PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(b2);
467330f729Sjoerg 
477330f729Sjoerg   unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
487330f729Sjoerg   unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
497330f729Sjoerg   return b1V > b2V;
507330f729Sjoerg }
51