2014-03-04 12:09:19 +00:00
|
|
|
//===- PredIteratorCache.h - pred_iterator Cache ----------------*- C++ -*-===//
|
2008-12-09 06:27:07 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the PredIteratorCache class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_IR_PREDITERATORCACHE_H
|
|
|
|
#define LLVM_IR_PREDITERATORCACHE_H
|
|
|
|
|
2008-12-09 06:27:07 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-03-04 11:45:46 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2008-12-09 06:27:07 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// PredIteratorCache - This class is an extremely trivial cache for
|
|
|
|
/// predecessor iterator queries. This is useful for code that repeatedly
|
|
|
|
/// wants the predecessor list for the same blocks.
|
|
|
|
class PredIteratorCache {
|
|
|
|
/// BlockToPredsMap - Pointer to null-terminated list.
|
|
|
|
DenseMap<BasicBlock*, BasicBlock**> BlockToPredsMap;
|
2009-04-22 08:46:33 +00:00
|
|
|
DenseMap<BasicBlock*, unsigned> BlockToPredCountMap;
|
2008-12-09 06:27:07 +00:00
|
|
|
|
|
|
|
/// Memory - This is the space that holds cached preds.
|
|
|
|
BumpPtrAllocator Memory;
|
|
|
|
public:
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-12-09 06:27:07 +00:00
|
|
|
/// GetPreds - Get a cached list for the null-terminated predecessor list of
|
|
|
|
/// the specified block. This can be used in a loop like this:
|
|
|
|
/// for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI)
|
|
|
|
/// use(*PI);
|
|
|
|
/// instead of:
|
|
|
|
/// for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
|
|
|
BasicBlock **GetPreds(BasicBlock *BB) {
|
|
|
|
BasicBlock **&Entry = BlockToPredsMap[BB];
|
|
|
|
if (Entry) return Entry;
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-12-09 06:27:07 +00:00
|
|
|
SmallVector<BasicBlock*, 32> PredCache(pred_begin(BB), pred_end(BB));
|
2014-04-15 04:59:12 +00:00
|
|
|
PredCache.push_back(nullptr); // null terminator.
|
2009-04-22 08:46:33 +00:00
|
|
|
|
|
|
|
BlockToPredCountMap[BB] = PredCache.size()-1;
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-12-09 06:27:07 +00:00
|
|
|
Entry = Memory.Allocate<BasicBlock*>(PredCache.size());
|
|
|
|
std::copy(PredCache.begin(), PredCache.end(), Entry);
|
|
|
|
return Entry;
|
|
|
|
}
|
2009-04-22 08:46:33 +00:00
|
|
|
|
|
|
|
unsigned GetNumPreds(BasicBlock *BB) {
|
|
|
|
GetPreds(BB);
|
|
|
|
return BlockToPredCountMap[BB];
|
|
|
|
}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-12-09 06:27:07 +00:00
|
|
|
/// clear - Remove all information.
|
|
|
|
void clear() {
|
|
|
|
BlockToPredsMap.clear();
|
2009-04-22 08:46:33 +00:00
|
|
|
BlockToPredCountMap.clear();
|
2008-12-09 06:27:07 +00:00
|
|
|
Memory.Reset();
|
|
|
|
}
|
|
|
|
};
|
2008-12-09 06:32:32 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2009-01-04 01:28:00 +00:00
|
|
|
#endif
|