Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:16

0001 //===--------------------- SourceMgr.h --------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 /// This file contains abstract class SourceMgr and the default implementation,
0010 /// CircularSourceMgr.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_MCA_SOURCEMGR_H
0015 #define LLVM_MCA_SOURCEMGR_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/MCA/Instruction.h"
0019 
0020 namespace llvm {
0021 namespace mca {
0022 
0023 // MSVC >= 19.15, < 19.20 need to see the definition of class Instruction to
0024 // prevent compiler error C2139 about intrinsic type trait '__is_assignable'.
0025 typedef std::pair<unsigned, const Instruction &> SourceRef;
0026 
0027 /// Abstracting the input code sequence (a sequence of MCInst) and assigning
0028 /// unique identifiers to every instruction in the sequence.
0029 struct SourceMgr {
0030   using UniqueInst = std::unique_ptr<Instruction>;
0031 
0032   /// Provides a fixed range of \a UniqueInst to iterate.
0033   virtual ArrayRef<UniqueInst> getInstructions() const = 0;
0034 
0035   /// (Fixed) Number of \a UniqueInst. Returns the size of
0036   /// \a getInstructions by default.
0037   virtual size_t size() const { return getInstructions().size(); }
0038 
0039   /// Whether there is any \a SourceRef to inspect / peek next.
0040   /// Note that returning false from this doesn't mean the instruction
0041   /// stream has ended.
0042   virtual bool hasNext() const = 0;
0043 
0044   /// Whether the instruction stream has eneded.
0045   virtual bool isEnd() const = 0;
0046 
0047   /// The next \a SourceRef.
0048   virtual SourceRef peekNext() const = 0;
0049 
0050   /// Advance to the next \a SourceRef.
0051   virtual void updateNext() = 0;
0052 
0053   virtual ~SourceMgr() {}
0054 };
0055 
0056 /// The default implementation of \a SourceMgr. It always takes a fixed number
0057 /// of instructions and provides an option to loop the given sequence for a
0058 /// certain iterations.
0059 class CircularSourceMgr : public SourceMgr {
0060   ArrayRef<UniqueInst> Sequence;
0061   unsigned Current;
0062   const unsigned Iterations;
0063   static const unsigned DefaultIterations = 100;
0064 
0065 public:
0066   CircularSourceMgr(ArrayRef<UniqueInst> S, unsigned Iter)
0067       : Sequence(S), Current(0U), Iterations(Iter ? Iter : DefaultIterations) {}
0068 
0069   ArrayRef<UniqueInst> getInstructions() const override { return Sequence; }
0070 
0071   unsigned getNumIterations() const { return Iterations; }
0072   bool hasNext() const override {
0073     return Current < (Iterations * Sequence.size());
0074   }
0075   bool isEnd() const override { return !hasNext(); }
0076 
0077   SourceRef peekNext() const override {
0078     assert(hasNext() && "Already at end of sequence!");
0079     return SourceRef(Current, *Sequence[Current % Sequence.size()]);
0080   }
0081 
0082   void updateNext() override { ++Current; }
0083 };
0084 
0085 } // namespace mca
0086 } // namespace llvm
0087 
0088 #endif // LLVM_MCA_SOURCEMGR_H