PredIteratorCache.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PredIteratorCache.h - pred_iterator Cache ----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the PredIteratorCache class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_PREDITERATORCACHE_H
  18. #define LLVM_IR_PREDITERATORCACHE_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/IR/CFG.h"
  23. #include "llvm/Support/Allocator.h"
  24. namespace llvm {
  25. /// PredIteratorCache - This class is an extremely trivial cache for
  26. /// predecessor iterator queries. This is useful for code that repeatedly
  27. /// wants the predecessor list for the same blocks.
  28. class PredIteratorCache {
  29. /// BlockToPredsMap - Pointer to null-terminated list.
  30. mutable DenseMap<BasicBlock *, BasicBlock **> BlockToPredsMap;
  31. mutable DenseMap<BasicBlock *, unsigned> BlockToPredCountMap;
  32. /// Memory - This is the space that holds cached preds.
  33. BumpPtrAllocator Memory;
  34. private:
  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)
  44. return Entry;
  45. SmallVector<BasicBlock *, 32> PredCache(predecessors(BB));
  46. PredCache.push_back(nullptr); // null terminator.
  47. BlockToPredCountMap[BB] = PredCache.size() - 1;
  48. Entry = Memory.Allocate<BasicBlock *>(PredCache.size());
  49. std::copy(PredCache.begin(), PredCache.end(), Entry);
  50. return Entry;
  51. }
  52. unsigned GetNumPreds(BasicBlock *BB) const {
  53. auto Result = BlockToPredCountMap.find(BB);
  54. if (Result != BlockToPredCountMap.end())
  55. return Result->second;
  56. return BlockToPredCountMap[BB] = pred_size(BB);
  57. }
  58. public:
  59. size_t size(BasicBlock *BB) const { return GetNumPreds(BB); }
  60. ArrayRef<BasicBlock *> get(BasicBlock *BB) {
  61. return ArrayRef(GetPreds(BB), GetNumPreds(BB));
  62. }
  63. /// clear - Remove all information.
  64. void clear() {
  65. BlockToPredsMap.clear();
  66. BlockToPredCountMap.clear();
  67. Memory.Reset();
  68. }
  69. };
  70. } // end namespace llvm
  71. #endif
  72. #ifdef __GNUC__
  73. #pragma GCC diagnostic pop
  74. #endif