LLVM  10.0.0
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/MCFixup.h"
24 #include "llvm/MC/MCInst.h"
26 #include "llvm/MCA/Instruction.h"
27 #include "llvm/MCA/Support.h"
29 
30 #include <string>
31 
32 namespace llvm {
33 namespace mca {
34 
35 /// A utility class used to compute instruction encodings for a code region.
36 ///
37 /// It provides a simple API to compute and return instruction encodings as
38 /// strings. Encodings are cached internally for later usage.
39 class CodeEmitter {
40  const MCSubtargetInfo &STI;
41  const MCAsmBackend &MAB;
42  const MCCodeEmitter &MCE;
43 
44  SmallString<256> Code;
45  raw_svector_ostream VecOS;
46  ArrayRef<MCInst> Sequence;
47 
48  // An EncodingInfo pair stores <base, length> information. Base (i.e. first)
49  // is an index to the `Code`. Length (i.e. second) is the encoding size.
50  using EncodingInfo = std::pair<unsigned, unsigned>;
51 
52  // A cache of encodings.
54 
55  EncodingInfo getOrCreateEncodingInfo(unsigned MCID);
56 
57 public:
59  const MCCodeEmitter &CE, ArrayRef<MCInst> S)
60  : STI(ST), MAB(AB), MCE(CE), VecOS(Code), Sequence(S),
61  Encodings(S.size()) {}
62 
63  StringRef getEncoding(unsigned MCID) {
64  EncodingInfo EI = getOrCreateEncodingInfo(MCID);
65  return StringRef(&Code[EI.first], EI.second);
66  }
67 };
68 
69 } // namespace mca
70 } // namespace llvm
71 
72 #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:543
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
Helper functions used by various pipeline components.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1163
Generic base class for all target subtargets.
This file defines abstractions used by the Pipeline to model register reads, register writes and inst...
A utility class used to compute instruction encodings for a code region.
Definition: CodeEmitter.h:39
StringRef getEncoding(unsigned MCID)
Definition: CodeEmitter.h:63
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
CodeEmitter(const MCSubtargetInfo &ST, const MCAsmBackend &AB, const MCCodeEmitter &CE, ArrayRef< MCInst > S)
Definition: CodeEmitter.h:58