1 //===- PredIteratorCache.h - pred_iterator Cache ----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the PredIteratorCache class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_PREDITERATORCACHE_H
14 #define LLVM_IR_PREDITERATORCACHE_H
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"
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
;
32 /// Memory - This is the space that holds cached preds.
33 BumpPtrAllocator Memory
;
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)
41 /// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
42 BasicBlock
**GetPreds(BasicBlock
*BB
) {
43 BasicBlock
**&Entry
= BlockToPredsMap
[BB
];
47 SmallVector
<BasicBlock
*, 32> PredCache(pred_begin(BB
), pred_end(BB
));
48 PredCache
.push_back(nullptr); // null terminator.
50 BlockToPredCountMap
[BB
] = PredCache
.size() - 1;
52 Entry
= Memory
.Allocate
<BasicBlock
*>(PredCache
.size());
53 std::copy(PredCache
.begin(), PredCache
.end(), Entry
);
57 unsigned GetNumPreds(BasicBlock
*BB
) const {
58 auto Result
= BlockToPredCountMap
.find(BB
);
59 if (Result
!= BlockToPredCountMap
.end())
60 return Result
->second
;
61 return BlockToPredCountMap
[BB
] = std::distance(pred_begin(BB
), pred_end(BB
));
65 size_t size(BasicBlock
*BB
) const { return GetNumPreds(BB
); }
66 ArrayRef
<BasicBlock
*> get(BasicBlock
*BB
) {
67 return makeArrayRef(GetPreds(BB
), GetNumPreds(BB
));
70 /// clear - Remove all information.
72 BlockToPredsMap
.clear();
73 BlockToPredCountMap
.clear();
78 } // end namespace llvm