1
//===- PredIteratorCache.h - pred_iterator Cache ----------------*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file defines the PredIteratorCache class.
12
//===----------------------------------------------------------------------===//
14
#ifndef LLVM_IR_PREDITERATORCACHE_H
15
#define LLVM_IR_PREDITERATORCACHE_H
17
#include "llvm/ADT/ArrayRef.h"
18
#include "llvm/ADT/DenseMap.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/IR/CFG.h"
21
#include "llvm/Support/Allocator.h"
25
/// PredIteratorCache - This class is an extremely trivial cache for
26
/// predecessor iterator queries. This is useful for code that repeatedly
27
/// wants the predecessor list for the same blocks.
28
class PredIteratorCache {
29
/// BlockToPredsMap - Pointer to null-terminated list.
30
DenseMap<BasicBlock *, BasicBlock **> BlockToPredsMap;
31
DenseMap<BasicBlock *, unsigned> BlockToPredCountMap;
33
/// Memory - This is the space that holds cached preds.
34
BumpPtrAllocator Memory;
37
/// GetPreds - Get a cached list for the null-terminated predecessor list of
38
/// the specified block. This can be used in a loop like this:
39
/// for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
42
/// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
43
BasicBlock **GetPreds(BasicBlock *BB) {
44
BasicBlock **&Entry = BlockToPredsMap[BB];
48
SmallVector<BasicBlock *, 32> PredCache(pred_begin(BB), pred_end(BB));
49
PredCache.push_back(nullptr); // null terminator.
51
BlockToPredCountMap[BB] = PredCache.size() - 1;
53
Entry = Memory.Allocate<BasicBlock *>(PredCache.size());
54
std::copy(PredCache.begin(), PredCache.end(), Entry);
58
unsigned GetNumPreds(BasicBlock *BB) {
60
return BlockToPredCountMap[BB];
64
size_t size(BasicBlock *BB) { return GetNumPreds(BB); }
65
ArrayRef<BasicBlock *> get(BasicBlock *BB) {
66
return makeArrayRef(GetPreds(BB), GetNumPreds(BB));
69
/// clear - Remove all information.
71
BlockToPredsMap.clear();
72
BlockToPredCountMap.clear();
77
} // end namespace llvm