LLVM API Documentation

PredIteratorCache.h
Go to the documentation of this file.
00001 //===- 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 #ifndef LLVM_IR_PREDITERATORCACHE_H
00015 #define LLVM_IR_PREDITERATORCACHE_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/IR/CFG.h"
00020 #include "llvm/Support/Allocator.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     DenseMap<BasicBlock*, unsigned> BlockToPredCountMap;
00031 
00032     /// Memory - This is the space that holds cached preds.
00033     BumpPtrAllocator Memory;
00034   public:
00035 
00036     /// GetPreds - Get a cached list for the null-terminated predecessor list of
00037     /// the specified block.  This can be used in a loop like this:
00038     ///   for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
00039     ///      use(*PI);
00040     /// instead of:
00041     /// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
00042     BasicBlock **GetPreds(BasicBlock *BB) {
00043       BasicBlock **&Entry = BlockToPredsMap[BB];
00044       if (Entry) return Entry;
00045 
00046       SmallVector<BasicBlock*, 32> PredCache(pred_begin(BB), pred_end(BB));
00047       PredCache.push_back(nullptr); // null terminator.
00048       
00049       BlockToPredCountMap[BB] = PredCache.size()-1;
00050 
00051       Entry = Memory.Allocate<BasicBlock*>(PredCache.size());
00052       std::copy(PredCache.begin(), PredCache.end(), Entry);
00053       return Entry;
00054     }
00055     
00056     unsigned GetNumPreds(BasicBlock *BB) {
00057       GetPreds(BB);
00058       return BlockToPredCountMap[BB];
00059     }
00060 
00061     /// clear - Remove all information.
00062     void clear() {
00063       BlockToPredsMap.clear();
00064       BlockToPredCountMap.clear();
00065       Memory.Reset();
00066     }
00067   };
00068 } // end namespace llvm
00069 
00070 #endif