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
54/// Represents a parsed InstructionEncoding record or a record derived from it.
55class InstructionEncoding {
56 /// The Record this encoding originates from.
57 const Record *EncodingDef;
58
59 /// The instruction this encoding is for.
60 const CodeGenInstruction *Inst;
61
62 /// The name of this encoding (for debugging purposes).
63 std::string Name;
64
65 /// The namespace in which this encoding exists.
66 StringRef DecoderNamespace;
67
68 /// Known bits of this encoding. This is the value of the `Inst` field
69 /// with any variable references replaced with '?'.
70 KnownBits InstBits;
71
72 /// Mask of bits that should be considered unknown during decoding.
73 /// This is the value of the `SoftFail` field.
74 APInt SoftFailMask;
75
76 /// The name of the function to use for decoding. May be an empty string,
77 /// meaning the decoder is generated.
78 StringRef DecoderMethod;
79
80 /// Whether the custom decoding function always succeeds. If a custom decoder
81 /// function is specified, the value is taken from the target description,
82 /// otherwise it is inferred.
83 bool HasCompleteDecoder;
84
85 /// Information about the operands' contribution to this encoding.
86 SmallVector<OperandInfo, 16> Operands;
87
88public:
89 InstructionEncoding(const Record *EncodingDef,
90 const CodeGenInstruction *Inst);
91
92 /// Returns the Record this encoding originates from.
93 const Record *getRecord() const { return EncodingDef; }
94
95 /// Returns the instruction this encoding is for.
96 const CodeGenInstruction *getInstruction() const { return Inst; }
97
98 /// Returns the name of this encoding, for debugging purposes.
99 StringRef getName() const { return Name; }
100
101 /// Returns the namespace in which this encoding exists.
102 StringRef getDecoderNamespace() const { return DecoderNamespace; }
103
104 /// Returns the size of this encoding, in bits.
105 unsigned getBitWidth() const { return InstBits.getBitWidth(); }
106
107 /// Returns the known bits of this encoding.
108 const KnownBits &getInstBits() const { return InstBits; }
109
110 /// Returns a mask of bits that should be considered unknown during decoding.
111 const APInt &getSoftFailMask() const { return SoftFailMask; }
112
113 /// Returns the known bits of this encoding that must match for
114 /// successful decoding.
115 KnownBits getMandatoryBits() const {
116 KnownBits EncodingBits = InstBits;
117 // Mark all bits that are allowed to change according to SoftFail mask
118 // as unknown.
119 EncodingBits.Zero &= ~SoftFailMask;
120 EncodingBits.One &= ~SoftFailMask;
121 return EncodingBits;
122 }
123
124 /// Returns the name of the function to use for decoding, or an empty string
125 /// if the decoder is generated.
126 StringRef getDecoderMethod() const { return DecoderMethod; }
127
128 /// Returns whether the decoder (either generated or specified by the user)
129 /// always succeeds.
130 bool hasCompleteDecoder() const { return HasCompleteDecoder; }
131
132 /// Returns information about the operands' contribution to this encoding.
133 ArrayRef<OperandInfo> getOperands() const { return Operands; }
134
135 /// \returns the effective value of the DecoderMethod field. If DecoderMethod
136 /// is an explictly set value, return false for second.
137 static std::pair<std::string, bool>
138 findOperandDecoderMethod(const Record *Record);
139
140 static OperandInfo getOpInfo(const Record *TypeRecord);
141
142private:
143 void parseVarLenEncoding(const VarLenInst &VLI);
144 void parseFixedLenEncoding(const BitsInit &RecordInstBits);
145
146 void parseVarLenOperands(const VarLenInst &VLI);
147 void parseFixedLenOperands(const BitsInit &Bits);
148};
149
150} // namespace llvm
151
152#endif // LLVM_UTILS_TABLEGEN_COMMON_INSTRUCTIONENCODING_H
153