1//===----------------------------------------------------------------------===//
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
9#ifndef LLVM_UTILS_TABLEGEN_COMMON_INSTRUCTIONENCODING_H
10#define LLVM_UTILS_TABLEGEN_COMMON_INSTRUCTIONENCODING_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/KnownBits.h"
16#include <optional>
17#include <string>
18#include <vector>
19
20namespace llvm {
21
22class BitsInit;
23class CodeGenInstruction;
24class CodeGenTarget;
25class Record;
26class RecordVal;
27class VarLenInst;
28
29// Represents a span of bits in the instruction encoding that's based on a span
30// of bits in an operand's encoding.
31//
32// Width is the width of the span.
33// Base is the starting position of that span in the instruction encoding.
34// Offset if the starting position of that span in the operand's encoding.
35// That is, bits {Base + Width - 1, Base} in the instruction encoding form
36// bits {Offset + Width - 1, Offset} in the operands encoding.
37struct EncodingField {
38 unsigned Base, Width, Offset;
39 EncodingField(unsigned B, unsigned W, unsigned O)
40 : Base(B), Width(W), Offset(O) {}
41};
42
43struct OperandInfo {
44 StringRef Name;
45 bool HasNoEncoding = false;
46 std::vector<EncodingField> Fields;
47 std::string Decoder;
48 bool HasCompleteDecoder;
49 std::optional<uint64_t> InitValue;
50
51 OperandInfo(std::string D, bool HCD) : Decoder(D), HasCompleteDecoder(HCD) {}
52
53 void addField(unsigned Base, unsigned Width, unsigned Offset) {
54 Fields.emplace_back(args&: Base, args&: Width, args&: Offset);
55 }
56
57 ArrayRef<EncodingField> fields() const { return Fields; }
58};
59
60/// Represents a parsed InstructionEncoding record or a record derived from it.
61class InstructionEncoding {
62 /// The Record this encoding originates from.
63 const Record *EncodingDef;
64
65 /// The instruction this encoding is for.
66 const CodeGenInstruction *Inst;
67
68 /// The name of this encoding (for debugging purposes).
69 std::string Name;
70
71 /// The namespace in which this encoding exists.
72 StringRef DecoderNamespace;
73
74 /// Known bits of this encoding. This is the value of the `Inst` field
75 /// with any variable references replaced with '?'.
76 KnownBits InstBits;
77
78 /// Mask of bits that should be considered unknown during decoding.
79 /// This is the value of the `SoftFail` field.
80 APInt SoftFailMask;
81
82 /// The name of the function to use for decoding. May be an empty string,
83 /// meaning the decoder is generated.
84 StringRef DecoderMethod;
85
86 /// Whether the custom decoding function always succeeds. If a custom decoder
87 /// function is specified, the value is taken from the target description,
88 /// otherwise it is inferred.
89 bool HasCompleteDecoder;
90
91 /// Information about the operands' contribution to this encoding.
92 SmallVector<OperandInfo, 16> Operands;
93
94public:
95 InstructionEncoding(const Record *EncodingDef,
96 const CodeGenInstruction *Inst);
97
98 /// Returns the Record this encoding originates from.
99 const Record *getRecord() const { return EncodingDef; }
100
101 /// Returns the instruction this encoding is for.
102 const CodeGenInstruction *getInstruction() const { return Inst; }
103
104 /// Returns the name of this encoding, for debugging purposes.
105 StringRef getName() const { return Name; }
106
107 /// Returns the namespace in which this encoding exists.
108 StringRef getDecoderNamespace() const { return DecoderNamespace; }
109
110 /// Returns the size of this encoding, in bits.
111 unsigned getBitWidth() const { return InstBits.getBitWidth(); }
112
113 /// Returns the known bits of this encoding.
114 const KnownBits &getInstBits() const { return InstBits; }
115
116 /// Returns a mask of bits that should be considered unknown during decoding.
117 const APInt &getSoftFailMask() const { return SoftFailMask; }
118
119 /// Returns the known bits of this encoding that must match for
120 /// successful decoding.
121 KnownBits getMandatoryBits() const {
122 KnownBits EncodingBits = InstBits;
123 // Mark all bits that are allowed to change according to SoftFail mask
124 // as unknown.
125 EncodingBits.Zero &= ~SoftFailMask;
126 EncodingBits.One &= ~SoftFailMask;
127 return EncodingBits;
128 }
129
130 /// Returns the name of the function to use for decoding, or an empty string
131 /// if the decoder is generated.
132 StringRef getDecoderMethod() const { return DecoderMethod; }
133
134 /// Returns whether the decoder (either generated or specified by the user)
135 /// always succeeds.
136 bool hasCompleteDecoder() const { return HasCompleteDecoder; }
137
138 /// Returns information about the operands' contribution to this encoding.
139 ArrayRef<OperandInfo> getOperands() const { return Operands; }
140
141 /// \returns the effective value of the DecoderMethod field. If DecoderMethod
142 /// is an explictly set value, return false for second.
143 static std::pair<std::string, bool>
144 findOperandDecoderMethod(const Record *Record);
145
146 static OperandInfo getOpInfo(const Record *TypeRecord);
147
148private:
149 void parseVarLenEncoding(const VarLenInst &VLI);
150 void parseFixedLenEncoding(const BitsInit &RecordInstBits);
151
152 void parseVarLenOperands(const VarLenInst &VLI);
153 void parseFixedLenOperands(const BitsInit &Bits);
154};
155
156} // namespace llvm
157
158#endif // LLVM_UTILS_TABLEGEN_COMMON_INSTRUCTIONENCODING_H
159