Removed trailing whitespace.
[oota-llvm.git] / include / llvm / Support / PredIteratorCache.h
1 //===- llvm/Support/PredIteratorCache.h - pred_iterator Cache ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PredIteratorCache class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/CFG.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallVector.h"
18
19 #ifndef LLVM_SUPPORT_PREDITERATORCACHE_H
20 #define LLVM_SUPPORT_PREDITERATORCACHE_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     DenseMap<BasicBlock*, BasicBlock**> BlockToPredsMap;
30
31     /// Memory - This is the space that holds cached preds.
32     BumpPtrAllocator Memory;
33   public:
34
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)
38     ///      use(*PI);
39     /// instead of:
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;
44
45       SmallVector<BasicBlock*, 32> PredCache(pred_begin(BB), pred_end(BB));
46       PredCache.push_back(0); // null terminator.
47
48       Entry = Memory.Allocate<BasicBlock*>(PredCache.size());
49       std::copy(PredCache.begin(), PredCache.end(), Entry);
50       return Entry;
51     }
52
53     /// clear - Remove all information.
54     void clear() {
55       BlockToPredsMap.clear();
56       Memory.Reset();
57     }
58   };
59 } // end namespace llvm
60
61 #endif