1 //===-- MCInstrDescView.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Provide views around LLVM structures to represents an instruction instance,
11 /// as well as its implicit and explicit arguments in a uniform way.
12 /// Arguments that are explicit and independant (non tied) also have a Variable
13 /// associated to them so the instruction can be fully defined by reading its
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H
19 #define LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H
22 #include <unordered_map>
24 #include "RegisterAliasing.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
34 // A variable represents the value associated to an Operand or a set of Operands
35 // if they are tied together.
37 // Returns the index of this Variable inside Instruction's Variable.
38 unsigned getIndex() const;
40 // Returns the index of the Operand linked to this Variable.
41 unsigned getPrimaryOperandIndex() const;
43 // Returns whether this Variable has more than one Operand linked to it.
44 bool hasTiedOperands() const;
46 // The indices of the operands tied to this Variable.
47 llvm::SmallVector
<unsigned, 2> TiedOperands
;
49 // The index of this Variable in Instruction.Variables and its associated
50 // Value in InstructionBuilder.VariableValues.
54 // MCOperandInfo can only represents Explicit operands. This object gives a
55 // uniform view of Implicit and Explicit Operands.
56 // - Index: can be used to refer to MCInstrDesc::operands for Explicit operands.
57 // - Tracker: is set for Register Operands and is used to keep track of possible
58 // registers and the registers reachable from them (aliasing registers).
59 // - Info: a shortcut for MCInstrDesc::operands()[Index].
60 // - TiedToIndex: the index of the Operand holding the value or -1.
61 // - ImplicitReg: a pointer to the register value when Operand is Implicit,
63 // - VariableIndex: the index of the Variable holding the value for this Operand
64 // or -1 if this operand is implicit.
66 bool isExplicit() const;
67 bool isImplicit() const;
68 bool isImplicitReg() const;
73 bool isVariable() const;
74 bool isMemory() const;
75 bool isImmediate() const;
76 unsigned getIndex() const;
77 unsigned getTiedToIndex() const;
78 unsigned getVariableIndex() const;
79 unsigned getImplicitReg() const;
80 const RegisterAliasingTracker
&getRegisterAliasing() const;
81 const llvm::MCOperandInfo
&getExplicitOperandInfo() const;
83 // Please use the accessors above and not the following fields.
86 const RegisterAliasingTracker
*Tracker
= nullptr; // Set for Register Op.
87 const llvm::MCOperandInfo
*Info
= nullptr; // Set for Explicit Op.
88 int TiedToIndex
= -1; // Set for Reg&Explicit Op.
89 const llvm::MCPhysReg
*ImplicitReg
= nullptr; // Set for Implicit Op.
90 int VariableIndex
= -1; // Set for Explicit Op.
93 // A view over an MCInstrDesc offering a convenient interface to compute
96 Instruction(const llvm::MCInstrInfo
&InstrInfo
,
97 const RegisterAliasingTrackerCache
&RATC
, unsigned Opcode
);
99 // Returns the Operand linked to this Variable.
100 // In case the Variable is tied, the primary (i.e. Def) Operand is returned.
101 const Operand
&getPrimaryOperand(const Variable
&Var
) const;
103 // Whether this instruction is self aliasing through its tied registers.
104 // Repeating this instruction is guaranteed to executes sequentially.
105 bool hasTiedRegisters() const;
107 // Whether this instruction is self aliasing through its implicit registers.
108 // Repeating this instruction is guaranteed to executes sequentially.
109 bool hasAliasingImplicitRegisters() const;
111 // Whether this instruction is self aliasing through some registers.
112 // Repeating this instruction may execute sequentially by picking aliasing
113 // Use and Def registers. It may also execute in parallel by picking non
114 // aliasing Use and Def registers.
115 bool hasAliasingRegisters() const;
117 // Whether this instruction's implicit registers alias with OtherInstr's
118 // implicit registers.
119 bool hasAliasingImplicitRegistersThrough(const Instruction
&OtherInstr
) const;
121 // Whether this instruction's registers alias with OtherInstr's registers.
122 bool hasAliasingRegistersThrough(const Instruction
&OtherInstr
) const;
124 // Returns whether this instruction has Memory Operands.
125 // Repeating this instruction executes sequentially with an instruction that
126 // reads or write the same memory region.
127 bool hasMemoryOperands() const;
129 // Returns whether this instruction as at least one use or one def.
130 // Repeating this instruction may execute sequentially by adding an
131 // instruction that aliases one of these.
132 bool hasOneUseOrOneDef() const;
134 // Convenient function to help with debugging.
135 void dump(const llvm::MCRegisterInfo
&RegInfo
,
136 llvm::raw_ostream
&Stream
) const;
138 const llvm::MCInstrDesc
*Description
; // Never nullptr.
139 llvm::StringRef Name
; // The name of this instruction.
140 llvm::SmallVector
<Operand
, 8> Operands
;
141 llvm::SmallVector
<Variable
, 4> Variables
;
142 llvm::BitVector ImplDefRegs
; // The set of aliased implicit def registers.
143 llvm::BitVector ImplUseRegs
; // The set of aliased implicit use registers.
144 llvm::BitVector AllDefRegs
; // The set of all aliased def registers.
145 llvm::BitVector AllUseRegs
; // The set of all aliased use registers.
148 // Instructions are expensive to instantiate. This class provides a cache of
149 // Instructions with lazy construction.
150 struct InstructionsCache
{
151 InstructionsCache(const llvm::MCInstrInfo
&InstrInfo
,
152 const RegisterAliasingTrackerCache
&RATC
);
154 // Returns the Instruction object corresponding to this Opcode.
155 const Instruction
&getInstr(unsigned Opcode
) const;
158 const llvm::MCInstrInfo
&InstrInfo
;
159 const RegisterAliasingTrackerCache
&RATC
;
160 mutable std::unordered_map
<unsigned, std::unique_ptr
<Instruction
>>
164 // Represents the assignment of a Register to an Operand.
165 struct RegisterOperandAssignment
{
166 RegisterOperandAssignment(const Operand
*Operand
, llvm::MCPhysReg Reg
)
167 : Op(Operand
), Reg(Reg
) {}
169 const Operand
*Op
; // Pointer to an Explicit Register Operand.
172 bool operator==(const RegisterOperandAssignment
&other
) const;
175 // Represents a set of Operands that would alias through the use of some
177 // There are two reasons why operands would alias:
178 // - The registers assigned to each of the operands are the same or alias each
179 // other (e.g. AX/AL)
180 // - The operands are tied.
181 struct AliasingRegisterOperands
{
182 llvm::SmallVector
<RegisterOperandAssignment
, 1> Defs
; // Unlikely size() > 1.
183 llvm::SmallVector
<RegisterOperandAssignment
, 2> Uses
;
185 // True is Defs and Use contain an Implicit Operand.
186 bool hasImplicitAliasing() const;
188 bool operator==(const AliasingRegisterOperands
&other
) const;
191 // Returns all possible configurations leading Def registers of DefInstruction
192 // to alias with Use registers of UseInstruction.
193 struct AliasingConfigurations
{
194 AliasingConfigurations(const Instruction
&DefInstruction
,
195 const Instruction
&UseInstruction
);
197 bool empty() const; // True if no aliasing configuration is found.
198 bool hasImplicitAliasing() const;
200 llvm::SmallVector
<AliasingRegisterOperands
, 32> Configurations
;
203 // Writes MCInst to OS.
204 // This is not assembly but the internal LLVM's name for instructions and
206 void DumpMCInst(const llvm::MCRegisterInfo
&MCRegisterInfo
,
207 const llvm::MCInstrInfo
&MCInstrInfo
,
208 const llvm::MCInst
&MCInst
, llvm::raw_ostream
&OS
);
210 } // namespace exegesis
213 #endif // LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H