Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 1 | //===- 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 Skrobov | a208a73 | 2014-08-14 16:04:47 +0000 | [diff] [blame] | 10 | // This file implements post order views of the blocks in a CFG. |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
| 15 | |
| 16 | using namespace clang; |
| 17 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 18 | void PostOrderCFGView::anchor() { } |
| 19 | |
Artyom Skrobov | a208a73 | 2014-08-14 16:04:47 +0000 | [diff] [blame] | 20 | ReversePostOrderCFGView::ReversePostOrderCFGView(const CFG *cfg) { |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 21 | Blocks.reserve(cfg->getNumBlockIDs()); |
| 22 | CFGBlockSet BSet(cfg); |
Artyom Skrobov | a208a73 | 2014-08-14 16:04:47 +0000 | [diff] [blame] | 23 | |
| 24 | typedef llvm::po_iterator<const CFG*, CFGBlockSet, true> po_iterator; |
| 25 | |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 26 | for (po_iterator I = po_iterator::begin(cfg, BSet), |
| 27 | E = po_iterator::end(cfg, BSet); I != E; ++I) { |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 28 | Blocks.push_back(*I); |
Artyom Skrobov | a208a73 | 2014-08-14 16:04:47 +0000 | [diff] [blame] | 29 | BlockOrder[*I] = Blocks.size(); |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 33 | PostOrderCFGView *PostOrderCFGView::create(AnalysisDeclContext &ctx) { |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 34 | const CFG *cfg = ctx.getCFG(); |
| 35 | if (!cfg) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 36 | return nullptr; |
Ted Kremenek | 5abde7c | 2011-10-22 02:14:23 +0000 | [diff] [blame] | 37 | return new PostOrderCFGView(cfg); |
| 38 | } |
| 39 | |
| 40 | const void *PostOrderCFGView::getTag() { static int x; return &x; } |
| 41 | |
| 42 | bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1, |
| 43 | const CFGBlock *b2) const { |
| 44 | PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1); |
| 45 | PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(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 | |
Artyom Skrobov | a208a73 | 2014-08-14 16:04:47 +0000 | [diff] [blame] | 52 | PostOrderCFGView::PostOrderCFGView(const CFG *cfg) { |
| 53 | unsigned size = cfg->getNumBlockIDs(); |
| 54 | Blocks.reserve(size); |
| 55 | CFGBlockSet BSet(cfg); |
| 56 | |
| 57 | typedef llvm::po_iterator<const CFG*, CFGBlockSet, true, |
| 58 | llvm::GraphTraits<llvm::Inverse<const CFG*> > |
| 59 | > po_iterator; |
| 60 | |
| 61 | for (po_iterator I = po_iterator::begin(cfg, BSet), |
| 62 | E = po_iterator::end(cfg, BSet); I != E; ++I) { |
| 63 | Blocks.push_back(*I); |
| 64 | BlockOrder[*I] = Blocks.size(); |
| 65 | } |
| 66 | |
| 67 | // It may be that some blocks are inaccessible going from the CFG exit upwards |
| 68 | // (e.g. infinite loops); we still need to add them. |
| 69 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); |
| 70 | (Blocks.size() < size) && (I != E); ++I) { |
| 71 | const CFGBlock* block = *I; |
| 72 | // Add a chain going upwards. |
| 73 | while (!BlockOrder.count(block)) { |
| 74 | Blocks.push_back(block); |
| 75 | BlockOrder[block] = Blocks.size(); |
| 76 | CFGBlock::const_pred_iterator PI = block->pred_begin(), |
| 77 | PE = block->pred_end(); |
| 78 | for (; PI != PE; ++PI) { |
| 79 | const CFGBlock* pred = *PI; |
| 80 | if (pred && !BlockOrder.count(pred)) { |
| 81 | block = pred; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | // Chain ends when we couldn't find an unmapped pred. |
| 86 | if (PI == PE) break; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | ReversePostOrderCFGView * |
| 92 | ReversePostOrderCFGView::create(AnalysisDeclContext &ctx) { |
| 93 | const CFG *cfg = ctx.getCFG(); |
| 94 | if (!cfg) |
| 95 | return nullptr; |
| 96 | return new ReversePostOrderCFGView(cfg); |
| 97 | } |