Trace.h 4.3 KB

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