SourceMgr.h 2.1 KB

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