LLVM  12.0.0git
CodeEmitter.h
Go to the documentation of this file.
1 //===--------------------- CodeEmitter.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 ///
10 /// A utility class used to compute instruction encodings. It buffers encodings
11 /// for later usage. It exposes a simple API to compute and get the encodings as
12 /// StringRef.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_MCA_CODEEMITTER_H
17 #define LLVM_MCA_CODEEMITTER_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCInst.h"
26 
27 #include <string>
28 
29 namespace llvm {
30 namespace mca {
31 
32 /// A utility class used to compute instruction encodings for a code region.
33 ///
34 /// It provides a simple API to compute and return instruction encodings as
35 /// strings. Encodings are cached internally for later usage.
36 class CodeEmitter {
37  const MCSubtargetInfo &STI;
38  const MCAsmBackend &MAB;
39  const MCCodeEmitter &MCE;
40 
41  SmallString<256> Code;
42  raw_svector_ostream VecOS;
43  ArrayRef<MCInst> Sequence;
44 
45  // An EncodingInfo pair stores <base, length> information. Base (i.e. first)
46  // is an index to the `Code`. Length (i.e. second) is the encoding size.
47  using EncodingInfo = std::pair<unsigned, unsigned>;
48 
49  // A cache of encodings.
51 
52  EncodingInfo getOrCreateEncodingInfo(unsigned MCID);
53 
54 public:
56  const MCCodeEmitter &CE, ArrayRef<MCInst> S)
57  : STI(ST), MAB(AB), MCE(CE), VecOS(Code), Sequence(S),
58  Encodings(S.size()) {}
59 
60  StringRef getEncoding(unsigned MCID) {
61  EncodingInfo EI = getOrCreateEncodingInfo(MCID);
62  return StringRef(&Code[EI.first], EI.second);
63  }
64 };
65 
66 } // namespace mca
67 } // namespace llvm
68 
69 #endif // LLVM_MCA_CODEEMITTER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:550
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
auto size(R &&Range, std::enable_if_t< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1473
Generic base class for all target subtargets.
A utility class used to compute instruction encodings for a code region.
Definition: CodeEmitter.h:36
StringRef getEncoding(unsigned MCID)
Definition: CodeEmitter.h:60
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
CodeEmitter(const MCSubtargetInfo &ST, const MCAsmBackend &AB, const MCCodeEmitter &CE, ArrayRef< MCInst > S)
Definition: CodeEmitter.h:55