1 //===-- MCInstrDescView.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Provide views around LLVM structures to represents an instruction instance,
12 /// as well as its implicit and explicit arguments in a uniform way.
13 /// Arguments that are explicit and independant (non tied) also have a Variable
14 /// associated to them so the instruction can be fully defined by reading its
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H
20 #define LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H
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"
33 // A variable represents the value associated to an Operand or a set of Operands
34 // if they are tied together.
36 // Returns the index of this Variable inside Instruction's Variable.
37 unsigned getIndex() const;
39 // Returns the index of the Operand linked to this Variable.
40 unsigned getPrimaryOperandIndex() const;
42 // Returns whether this Variable has more than one Operand linked to it.
43 bool hasTiedOperands() const;
45 // The indices of the operands tied to this Variable.
46 llvm::SmallVector
<unsigned, 2> TiedOperands
;
48 // The index of this Variable in Instruction.Variables and its associated
49 // Value in InstructionBuilder.VariableValues.
53 // MCOperandInfo can only represents Explicit operands. This object gives a
54 // uniform view of Implicit and Explicit Operands.
55 // - Index: can be used to refer to MCInstrDesc::operands for Explicit operands.
56 // - Tracker: is set for Register Operands and is used to keep track of possible
57 // registers and the registers reachable from them (aliasing registers).
58 // - Info: a shortcut for MCInstrDesc::operands()[Index].
59 // - TiedToIndex: the index of the Operand holding the value or -1.
60 // - ImplicitReg: a pointer to the register value when Operand is Implicit,
62 // - VariableIndex: the index of the Variable holding the value for this Operand
63 // or -1 if this operand is implicit.
65 bool isExplicit() const;
66 bool isImplicit() const;
67 bool isImplicitReg() const;
72 bool isVariable() const;
73 bool isMemory() const;
74 bool isImmediate() const;
75 unsigned getIndex() const;
76 unsigned getTiedToIndex() const;
77 unsigned getVariableIndex() const;
78 unsigned getImplicitReg() const;
79 const RegisterAliasingTracker
&getRegisterAliasing() const;
80 const llvm::MCOperandInfo
&getExplicitOperandInfo() const;
82 // Please use the accessors above and not the following fields.
85 const RegisterAliasingTracker
*Tracker
= nullptr; // Set for Register Op.
86 const llvm::MCOperandInfo
*Info
= nullptr; // Set for Explicit Op.
87 int TiedToIndex
= -1; // Set for Reg&Explicit Op.
88 const llvm::MCPhysReg
*ImplicitReg
= nullptr; // Set for Implicit Op.
89 int VariableIndex
= -1; // Set for Explicit Op.
92 // A view over an MCInstrDesc offering a convenient interface to compute
95 Instruction(const llvm::MCInstrDesc
&MCInstrDesc
,
96 const RegisterAliasingTrackerCache
&ATC
);
98 // Returns the Operand linked to this Variable.
99 // In case the Variable is tied, the primary (i.e. Def) Operand is returned.
100 const Operand
&getPrimaryOperand(const Variable
&Var
) const;
102 // Whether this instruction is self aliasing through its tied registers.
103 // Repeating this instruction is guaranteed to executes sequentially.
104 bool hasTiedRegisters() const;
106 // Whether this instruction is self aliasing through its implicit registers.
107 // Repeating this instruction is guaranteed to executes sequentially.
108 bool hasAliasingImplicitRegisters() const;
110 // Whether this instruction is self aliasing through some registers.
111 // Repeating this instruction may execute sequentially by picking aliasing
112 // Use and Def registers. It may also execute in parallel by picking non
113 // aliasing Use and Def registers.
114 bool hasAliasingRegisters() const;
116 // Whether this instruction's implicit registers alias with OtherInstr's
117 // implicit registers.
118 bool hasAliasingImplicitRegistersThrough(const Instruction
&OtherInstr
) const;
120 // Whether this instruction's registers alias with OtherInstr's registers.
121 bool hasAliasingRegistersThrough(const Instruction
&OtherInstr
) const;
123 // Returns whether this instruction has Memory Operands.
124 // Repeating this instruction executes sequentially with an instruction that
125 // reads or write the same memory region.
126 bool hasMemoryOperands() const;
128 // Convenient function to help with debugging.
129 void dump(const llvm::MCRegisterInfo
&RegInfo
,
130 llvm::raw_ostream
&Stream
) const;
132 const llvm::MCInstrDesc
*Description
; // Never nullptr.
133 llvm::SmallVector
<Operand
, 8> Operands
;
134 llvm::SmallVector
<Variable
, 4> Variables
;
135 llvm::BitVector ImplDefRegs
; // The set of aliased implicit def registers.
136 llvm::BitVector ImplUseRegs
; // The set of aliased implicit use registers.
137 llvm::BitVector AllDefRegs
; // The set of all aliased def registers.
138 llvm::BitVector AllUseRegs
; // The set of all aliased use registers.
141 // Represents the assignment of a Register to an Operand.
142 struct RegisterOperandAssignment
{
143 RegisterOperandAssignment(const Operand
*Operand
, llvm::MCPhysReg Reg
)
144 : Op(Operand
), Reg(Reg
) {}
146 const Operand
*Op
; // Pointer to an Explicit Register Operand.
149 bool operator==(const RegisterOperandAssignment
&other
) const;
152 // Represents a set of Operands that would alias through the use of some
154 // There are two reasons why operands would alias:
155 // - The registers assigned to each of the operands are the same or alias each
156 // other (e.g. AX/AL)
157 // - The operands are tied.
158 struct AliasingRegisterOperands
{
159 llvm::SmallVector
<RegisterOperandAssignment
, 1> Defs
; // Unlikely size() > 1.
160 llvm::SmallVector
<RegisterOperandAssignment
, 2> Uses
;
162 // True is Defs and Use contain an Implicit Operand.
163 bool hasImplicitAliasing() const;
165 bool operator==(const AliasingRegisterOperands
&other
) const;
168 // Returns all possible configurations leading Def registers of DefInstruction
169 // to alias with Use registers of UseInstruction.
170 struct AliasingConfigurations
{
171 AliasingConfigurations(const Instruction
&DefInstruction
,
172 const Instruction
&UseInstruction
);
174 bool empty() const; // True if no aliasing configuration is found.
175 bool hasImplicitAliasing() const;
176 void setExplicitAliasing() const;
178 const Instruction
&DefInstruction
;
179 const Instruction
&UseInstruction
;
180 llvm::SmallVector
<AliasingRegisterOperands
, 32> Configurations
;
183 // Writes MCInst to OS.
184 // This is not assembly but the internal LLVM's name for instructions and
186 void DumpMCInst(const llvm::MCRegisterInfo
&MCRegisterInfo
,
187 const llvm::MCInstrInfo
&MCInstrInfo
,
188 const llvm::MCInst
&MCInst
, llvm::raw_ostream
&OS
);
190 } // namespace exegesis
192 #endif // LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H