Trace.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This class represents a single trace of LLVM basic blocks. A trace is a
  10. // single entry, multiple exit, region of code that is often hot. Trace-based
  11. // optimizations treat traces almost like they are a large, strange, basic
  12. // block: because the trace path is assumed to be hot, optimizations for the
  13. // fall-through path are made at the expense of the non-fall-through paths.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_ANALYSIS_TRACE_H
  17. #define LLVM_ANALYSIS_TRACE_H
  18. #include <cassert>
  19. #include <vector>
  20. namespace llvm {
  21. class BasicBlock;
  22. class Function;
  23. class Module;
  24. class raw_ostream;
  25. class Trace {
  26. using BasicBlockListType = std::vector<BasicBlock *>;
  27. BasicBlockListType BasicBlocks;
  28. public:
  29. /// Trace ctor - Make a new trace from a vector of basic blocks,
  30. /// residing in the function which is the parent of the first
  31. /// basic block in the vector.
  32. Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
  33. /// getEntryBasicBlock - Return the entry basic block (first block)
  34. /// of the trace.
  35. BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
  36. /// operator[]/getBlock - Return basic block N in the trace.
  37. BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
  38. BasicBlock *getBlock(unsigned i) const { return BasicBlocks[i]; }
  39. /// getFunction - Return this trace's parent function.
  40. Function *getFunction () const;
  41. /// getModule - Return this Module that contains this trace's parent
  42. /// function.
  43. Module *getModule () const;
  44. /// getBlockIndex - Return the index of the specified basic block in the
  45. /// trace, or -1 if it is not in the trace.
  46. int getBlockIndex(const BasicBlock *X) const {
  47. for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
  48. if (BasicBlocks[i] == X)
  49. return i;
  50. return -1;
  51. }
  52. /// contains - Returns true if this trace contains the given basic
  53. /// block.
  54. bool contains(const BasicBlock *X) const {
  55. return getBlockIndex(X) != -1;
  56. }
  57. /// Returns true if B1 occurs before B2 in the trace, or if it is the same
  58. /// block as B2.. Both blocks must be in the trace.
  59. bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
  60. int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
  61. assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
  62. return B1Idx <= B2Idx;
  63. }
  64. // BasicBlock iterators...
  65. using iterator = BasicBlockListType::iterator;
  66. using const_iterator = BasicBlockListType::const_iterator;
  67. using reverse_iterator = std::reverse_iterator<iterator>;
  68. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  69. iterator begin() { return BasicBlocks.begin(); }
  70. const_iterator begin() const { return BasicBlocks.begin(); }
  71. iterator end () { return BasicBlocks.end(); }
  72. const_iterator end () const { return BasicBlocks.end(); }
  73. reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
  74. const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
  75. reverse_iterator rend () { return BasicBlocks.rend(); }
  76. const_reverse_iterator rend () const { return BasicBlocks.rend(); }
  77. unsigned size() const { return BasicBlocks.size(); }
  78. bool empty() const { return BasicBlocks.empty(); }
  79. iterator erase(iterator q) { return BasicBlocks.erase (q); }
  80. iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
  81. /// print - Write trace to output stream.
  82. void print(raw_ostream &O) const;
  83. /// dump - Debugger convenience method; writes trace to standard error
  84. /// output stream.
  85. void dump() const;
  86. };
  87. } // end namespace llvm
  88. #endif // LLVM_ANALYSIS_TRACE_H