Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/PredIteratorCache.h
Line
Count
Source
1
//===- PredIteratorCache.h - pred_iterator Cache ----------------*- C++ -*-===//
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 defines the PredIteratorCache class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_IR_PREDITERATORCACHE_H
14
#define LLVM_IR_PREDITERATORCACHE_H
15
16
#include "llvm/ADT/ArrayRef.h"
17
#include "llvm/ADT/DenseMap.h"
18
#include "llvm/ADT/SmallVector.h"
19
#include "llvm/IR/CFG.h"
20
#include "llvm/Support/Allocator.h"
21
22
namespace llvm {
23
24
/// PredIteratorCache - This class is an extremely trivial cache for
25
/// predecessor iterator queries.  This is useful for code that repeatedly
26
/// wants the predecessor list for the same blocks.
27
class PredIteratorCache {
28
  /// BlockToPredsMap - Pointer to null-terminated list.
29
  mutable DenseMap<BasicBlock *, BasicBlock **> BlockToPredsMap;
30
  mutable DenseMap<BasicBlock *, unsigned> BlockToPredCountMap;
31
32
  /// Memory - This is the space that holds cached preds.
33
  BumpPtrAllocator Memory;
34
35
private:
36
  /// GetPreds - Get a cached list for the null-terminated predecessor list of
37
  /// the specified block.  This can be used in a loop like this:
38
  ///   for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
39
  ///      use(*PI);
40
  /// instead of:
41
  /// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
42
12.7M
  BasicBlock **GetPreds(BasicBlock *BB) {
43
12.7M
    BasicBlock **&Entry = BlockToPredsMap[BB];
44
12.7M
    if (Entry)
45
10.7M
      return Entry;
46
2.03M
47
2.03M
    SmallVector<BasicBlock *, 32> PredCache(pred_begin(BB), pred_end(BB));
48
2.03M
    PredCache.push_back(nullptr); // null terminator.
49
2.03M
50
2.03M
    BlockToPredCountMap[BB] = PredCache.size() - 1;
51
2.03M
52
2.03M
    Entry = Memory.Allocate<BasicBlock *>(PredCache.size());
53
2.03M
    std::copy(PredCache.begin(), PredCache.end(), Entry);
54
2.03M
    return Entry;
55
2.03M
  }
56
57
13.6M
  unsigned GetNumPreds(BasicBlock *BB) const {
58
13.6M
    auto Result = BlockToPredCountMap.find(BB);
59
13.6M
    if (Result != BlockToPredCountMap.end())
60
13.0M
      return Result->second;
61
632k
    return BlockToPredCountMap[BB] = std::distance(pred_begin(BB), pred_end(BB));
62
632k
  }
63
64
public:
65
898k
  size_t size(BasicBlock *BB) const { return GetNumPreds(BB); }
66
12.7M
  ArrayRef<BasicBlock *> get(BasicBlock *BB) {
67
12.7M
    return makeArrayRef(GetPreds(BB), GetNumPreds(BB));
68
12.7M
  }
69
70
  /// clear - Remove all information.
71
39.3k
  void clear() {
72
39.3k
    BlockToPredsMap.clear();
73
39.3k
    BlockToPredCountMap.clear();
74
39.3k
    Memory.Reset();
75
39.3k
  }
76
};
77
78
} // end namespace llvm
79
80
#endif