Index: llvm/trunk/include/llvm/IR/PredIteratorCache.h =================================================================== --- llvm/trunk/include/llvm/IR/PredIteratorCache.h +++ llvm/trunk/include/llvm/IR/PredIteratorCache.h @@ -27,8 +27,8 @@ /// wants the predecessor list for the same blocks. class PredIteratorCache { /// BlockToPredsMap - Pointer to null-terminated list. - DenseMap BlockToPredsMap; - DenseMap BlockToPredCountMap; + mutable DenseMap BlockToPredsMap; + mutable DenseMap BlockToPredCountMap; /// Memory - This is the space that holds cached preds. BumpPtrAllocator Memory; @@ -55,13 +55,15 @@ return Entry; } - unsigned GetNumPreds(BasicBlock *BB) { - GetPreds(BB); - return BlockToPredCountMap[BB]; + unsigned GetNumPreds(BasicBlock *BB) const { + auto Result = BlockToPredCountMap.find(BB); + if (Result != BlockToPredCountMap.end()) + return Result->second; + return BlockToPredCountMap[BB] = std::distance(pred_begin(BB), pred_end(BB)); } public: - size_t size(BasicBlock *BB) { return GetNumPreds(BB); } + size_t size(BasicBlock *BB) const { return GetNumPreds(BB); } ArrayRef get(BasicBlock *BB) { return makeArrayRef(GetPreds(BB), GetNumPreds(BB)); }