SourceMgr.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------------------- SourceMgr.h --------------------------*- 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. /// \file
  14. /// This file contains abstract class SourceMgr and the default implementation,
  15. /// CircularSourceMgr.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_MCA_SOURCEMGR_H
  19. #define LLVM_MCA_SOURCEMGR_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/MCA/Instruction.h"
  22. namespace llvm {
  23. namespace mca {
  24. // MSVC >= 19.15, < 19.20 need to see the definition of class Instruction to
  25. // prevent compiler error C2139 about intrinsic type trait '__is_assignable'.
  26. typedef std::pair<unsigned, const Instruction &> SourceRef;
  27. /// Abstracting the input code sequence (a sequence of MCInst) and assigning
  28. /// unique identifiers to every instruction in the sequence.
  29. struct SourceMgr {
  30. using UniqueInst = std::unique_ptr<Instruction>;
  31. /// Provides a fixed range of \a UniqueInst to iterate.
  32. virtual ArrayRef<UniqueInst> getInstructions() const = 0;
  33. /// (Fixed) Number of \a UniqueInst. Returns the size of
  34. /// \a getInstructions by default.
  35. virtual size_t size() const { return getInstructions().size(); }
  36. /// Whether there is any \a SourceRef to inspect / peek next.
  37. /// Note that returning false from this doesn't mean the instruction
  38. /// stream has ended.
  39. virtual bool hasNext() const = 0;
  40. /// Whether the instruction stream has eneded.
  41. virtual bool isEnd() const = 0;
  42. /// The next \a SourceRef.
  43. virtual SourceRef peekNext() const = 0;
  44. /// Advance to the next \a SourceRef.
  45. virtual void updateNext() = 0;
  46. virtual ~SourceMgr() {}
  47. };
  48. /// The default implementation of \a SourceMgr. It always takes a fixed number
  49. /// of instructions and provides an option to loop the given sequence for a
  50. /// certain iterations.
  51. class CircularSourceMgr : public SourceMgr {
  52. ArrayRef<UniqueInst> Sequence;
  53. unsigned Current;
  54. const unsigned Iterations;
  55. static const unsigned DefaultIterations = 100;
  56. public:
  57. CircularSourceMgr(ArrayRef<UniqueInst> S, unsigned Iter)
  58. : Sequence(S), Current(0U), Iterations(Iter ? Iter : DefaultIterations) {}
  59. ArrayRef<UniqueInst> getInstructions() const override { return Sequence; }
  60. unsigned getNumIterations() const { return Iterations; }
  61. bool hasNext() const override {
  62. return Current < (Iterations * Sequence.size());
  63. }
  64. bool isEnd() const override { return !hasNext(); }
  65. SourceRef peekNext() const override {
  66. assert(hasNext() && "Already at end of sequence!");
  67. return SourceRef(Current, *Sequence[Current % Sequence.size()]);
  68. }
  69. void updateNext() override { ++Current; }
  70. };
  71. } // namespace mca
  72. } // namespace llvm
  73. #endif // LLVM_MCA_SOURCEMGR_H
  74. #ifdef __GNUC__
  75. #pragma GCC diagnostic pop
  76. #endif