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"
21 /// PredIteratorCache - This class is an extremely trivial cache for
22 /// predecessor iterator queries. This is useful for code that repeatedly
23 /// wants the predecessor list for the same blocks.
24 class PredIteratorCache {
25 /// BlockToPredsMap - Pointer to null-terminated list.
26 DenseMap<BasicBlock*, BasicBlock**> BlockToPredsMap;
28 /// Memory - This is the space that holds cached preds.
29 BumpPtrAllocator Memory;
32 /// GetPreds - Get a cached list for the null-terminated predecessor list of
33 /// the specified block. This can be used in a loop like this:
34 /// for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
37 /// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
38 BasicBlock **GetPreds(BasicBlock *BB) {
39 BasicBlock **&Entry = BlockToPredsMap[BB];
40 if (Entry) return Entry;
42 SmallVector<BasicBlock*, 32> PredCache(pred_begin(BB), pred_end(BB));
43 PredCache.push_back(0); // null terminator.
45 Entry = Memory.Allocate<BasicBlock*>(PredCache.size());
46 std::copy(PredCache.begin(), PredCache.end(), Entry);
50 /// clear - Remove all information.
52 BlockToPredsMap.clear();
56 } // end namespace llvm