LLVM API Documentation

PredIteratorCache.h

Go to the documentation of this file.
00001 //===- llvm/Support/PredIteratorCache.h - pred_iterator Cache ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the PredIteratorCache class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/Allocator.h"
00015 #include "llvm/Support/CFG.h"
00016 #include "llvm/ADT/DenseMap.h"
00017 #include "llvm/ADT/SmallVector.h"
00018 
00019 #ifndef LLVM_SUPPORT_PREDITERATORCACHE_H
00020 #define LLVM_SUPPORT_PREDITERATORCACHE_H
00021 
00022 namespace llvm {
00023 
00024   /// PredIteratorCache - This class is an extremely trivial cache for
00025   /// predecessor iterator queries.  This is useful for code that repeatedly
00026   /// wants the predecessor list for the same blocks.
00027   class PredIteratorCache {
00028     /// BlockToPredsMap - Pointer to null-terminated list.
00029     DenseMap<BasicBlock*, BasicBlock**> BlockToPredsMap;
00030 
00031     /// Memory - This is the space that holds cached preds.
00032     BumpPtrAllocator Memory;
00033   public:
00034     
00035     /// GetPreds - Get a cached list for the null-terminated predecessor list of
00036     /// the specified block.  This can be used in a loop like this:
00037     ///   for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
00038     ///      use(*PI);
00039     /// instead of:
00040     /// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
00041     BasicBlock **GetPreds(BasicBlock *BB) {
00042       BasicBlock **&Entry = BlockToPredsMap[BB];
00043       if (Entry) return Entry;
00044       
00045       SmallVector<BasicBlock*, 32> PredCache(pred_begin(BB), pred_end(BB));
00046       PredCache.push_back(0); // null terminator.
00047       
00048       Entry = Memory.Allocate<BasicBlock*>(PredCache.size());
00049       std::copy(PredCache.begin(), PredCache.end(), Entry);
00050       return Entry;
00051     }
00052     
00053     /// clear - Remove all information.
00054     void clear() {
00055       BlockToPredsMap.clear();
00056       Memory.Reset();
00057     }
00058   };
00059 } // end namespace llvm
00060 
00061 #endif