SourceMgr.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===--------------------- SourceMgr.h --------------------------*- 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. /// \file
  9. /// This file implements class SourceMgr. Class SourceMgr abstracts the input
  10. /// code sequence (a sequence of MCInst), and assings unique identifiers to
  11. /// every instruction in the sequence.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_MCA_SOURCEMGR_H
  15. #define LLVM_MCA_SOURCEMGR_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/MCA/Instruction.h"
  18. namespace llvm {
  19. namespace mca {
  20. // MSVC >= 19.15, < 19.20 need to see the definition of class Instruction to
  21. // prevent compiler error C2139 about intrinsic type trait '__is_assignable'.
  22. typedef std::pair<unsigned, const Instruction &> SourceRef;
  23. class SourceMgr {
  24. using UniqueInst = std::unique_ptr<Instruction>;
  25. ArrayRef<UniqueInst> Sequence;
  26. unsigned Current;
  27. const unsigned Iterations;
  28. static const unsigned DefaultIterations = 100;
  29. public:
  30. SourceMgr(ArrayRef<UniqueInst> S, unsigned Iter)
  31. : Sequence(S), Current(0), Iterations(Iter ? Iter : DefaultIterations) {}
  32. unsigned getNumIterations() const { return Iterations; }
  33. unsigned size() const { return Sequence.size(); }
  34. bool hasNext() const { return Current < (Iterations * Sequence.size()); }
  35. void updateNext() { ++Current; }
  36. SourceRef peekNext() const {
  37. assert(hasNext() && "Already at end of sequence!");
  38. return SourceRef(Current, *Sequence[Current % Sequence.size()]);
  39. }
  40. using const_iterator = ArrayRef<UniqueInst>::const_iterator;
  41. const_iterator begin() const { return Sequence.begin(); }
  42. const_iterator end() const { return Sequence.end(); }
  43. };
  44. } // namespace mca
  45. } // namespace llvm
  46. #endif // LLVM_MCA_SOURCEMGR_H