1 //===- llvm/Support/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 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/CFG.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallVector.h"
19 #ifndef LLVM_SUPPORT_PREDITERATORCACHE_H
20 #define LLVM_SUPPORT_PREDITERATORCACHE_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 DenseMap<BasicBlock*, BasicBlock**> BlockToPredsMap;
31 /// Memory - This is the space that holds cached preds.
32 BumpPtrAllocator Memory;
35 /// GetPreds - Get a cached list for the null-terminated predecessor list of
36 /// the specified block. This can be used in a loop like this:
37 /// for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
40 /// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
41 BasicBlock **GetPreds(BasicBlock *BB) {
42 BasicBlock **&Entry = BlockToPredsMap[BB];
43 if (Entry) return Entry;
45 SmallVector<BasicBlock*, 32> PredCache(pred_begin(BB), pred_end(BB));
46 PredCache.push_back(0); // null terminator.
48 Entry = Memory.Allocate<BasicBlock*>(PredCache.size());
49 std::copy(PredCache.begin(), PredCache.end(), Entry);
53 /// clear - Remove all information.
55 BlockToPredsMap.clear();
59 } // end namespace llvm