clang 23.0.0git
PostOrderCFGView.cpp
Go to the documentation of this file.
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
15#include "clang/Analysis/CFG.h"
16
17using namespace clang;
18
19void PostOrderCFGView::anchor() {}
20
22 Blocks.reserve(cfg->getNumBlockIDs());
23
24 // The CFG orders the blocks of loop bodies before those of loop successors
25 // (both numerically, and in the successor order of the loop condition
26 // block). So, RPO necessarily reverses that order, placing the loop successor
27 // *before* the loop body. For many analyses, particularly those that converge
28 // to a fixpoint, this results in potentially significant extra work because
29 // loop successors will necessarily need to be reconsidered once the algorithm
30 // has reached a fixpoint on the loop body.
31 //
32 // This definition of CFG graph traits reverses the order of children, so that
33 // loop bodies will come first in an RPO.
34 struct CFGLoopBodyFirstTraits {
35 using NodeRef = const ::clang::CFGBlock *;
36 using ChildIteratorType = ::clang::CFGBlock::const_succ_reverse_iterator;
37
38 static ChildIteratorType child_begin(NodeRef N) { return N->succ_rbegin(); }
39 static ChildIteratorType child_end(NodeRef N) { return N->succ_rend(); }
40 };
41
42 struct POTraversal
43 : llvm::PostOrderTraversalBase<POTraversal, CFGLoopBodyFirstTraits> {
44 CFGBlockSet BSet;
45
46 POTraversal(const CFG *cfg) : BSet(cfg) { this->init(&cfg->getEntry()); }
47 bool insertEdge(std::optional<const CFGBlock *>, const CFGBlock *To) {
48 if (!To)
49 return false;
50 return BSet.insert(To).second;
51 }
52 };
53
54 for (const CFGBlock *Block : POTraversal(cfg)) {
55 BlockOrder[Block] = Blocks.size() + 1;
56 Blocks.push_back(Block);
57 }
58}
59
60std::unique_ptr<PostOrderCFGView>
62 const CFG *cfg = ctx.getCFG();
63 if (!cfg)
64 return nullptr;
65 return std::make_unique<PostOrderCFGView>(cfg);
66}
67
68const void *PostOrderCFGView::getTag() { static int x; return &x; }
69
71 const CFGBlock *b2) const {
72 PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1);
73 PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(b2);
74
75 unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
76 unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
77 return b1V > b2V;
78}
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a single basic block in a source-level CFG.
Definition CFG.h:632
AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator
Definition CFG.h:995
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1250
CFGBlock & getEntry()
Definition CFG.h:1364
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Definition CFG.h:1443
Implements a set of CFGBlocks using a BitVector.
std::pair< std::nullopt_t, bool > insert(const CFGBlock *Block)
Set the bit associated with a particular CFGBlock.
PostOrderCFGView(const CFG *cfg)
static std::unique_ptr< PostOrderCFGView > create(AnalysisDeclContext &analysisContext)
static const void * getTag()
The JSON file list parser is used to communicate input to InstallAPI.
bool operator()(const CFGBlock *b1, const CFGBlock *b2) const