LLVM 22.0.0git
MCInstrAnalysis.h
Go to the documentation of this file.
1//===- llvm/MC/MCInstrAnalysis.h - InstrDesc target hooks -------*- 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//
9// This file defines the MCInstrAnalysis class which the MCTargetDescs can
10// derive from to give additional information to MC.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MC_MCINSTRANALYSIS_H
15#define LLVM_MC_MCINSTRANALYSIS_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstrDesc.h"
20#include "llvm/MC/MCInstrInfo.h"
23#include <cstdint>
24#include <vector>
25
26namespace llvm {
27
28class MCRegisterInfo;
29class Triple;
30
32protected:
33 friend class Target;
34
36
37public:
39 virtual ~MCInstrAnalysis() = default;
40
41 /// Clear the internal state. See updateState for more information.
42 virtual void resetState() {}
43
44 /// Update internal state with \p Inst at \p Addr.
45 ///
46 /// For some types of analyses, inspecting a single instruction is not
47 /// sufficient. Some examples are auipc/jalr pairs on RISC-V or adrp/ldr pairs
48 /// on AArch64. To support inspecting multiple instructions, targets may keep
49 /// track of an internal state while analysing instructions. Clients should
50 /// call updateState for every instruction which allows later calls to one of
51 /// the analysis functions to take previous instructions into account.
52 /// Whenever state becomes irrelevant (e.g., when starting to disassemble a
53 /// new function), clients should call resetState to clear it.
54 virtual void updateState(const MCInst &Inst, uint64_t Addr) {}
55
56 virtual bool isBranch(const MCInst &Inst) const {
57 return Info->get(Inst.getOpcode()).isBranch();
58 }
59
60 virtual bool isConditionalBranch(const MCInst &Inst) const {
61 return Info->get(Inst.getOpcode()).isConditionalBranch();
62 }
63
64 virtual bool isUnconditionalBranch(const MCInst &Inst) const {
65 return Info->get(Inst.getOpcode()).isUnconditionalBranch();
66 }
67
68 virtual bool isIndirectBranch(const MCInst &Inst) const {
69 return Info->get(Inst.getOpcode()).isIndirectBranch();
70 }
71
72 virtual bool isCall(const MCInst &Inst) const {
73 return Info->get(Inst.getOpcode()).isCall();
74 }
75
76 virtual bool isReturn(const MCInst &Inst) const {
77 return Info->get(Inst.getOpcode()).isReturn();
78 }
79
80 virtual bool isTerminator(const MCInst &Inst) const {
81 return Info->get(Inst.getOpcode()).isTerminator();
82 }
83
84 virtual bool isBarrier(const MCInst &Inst) const {
85 return Info->get(Inst.getOpcode()).isBarrier();
86 }
87
88 virtual bool mayAffectControlFlow(const MCInst &Inst,
89 const MCRegisterInfo &MCRI) const {
90 if (isBranch(Inst) || isCall(Inst) || isReturn(Inst) ||
91 isIndirectBranch(Inst))
92 return true;
93 MCRegister PC = MCRI.getProgramCounter();
94 if (!PC)
95 return false;
96 return Info->get(Inst.getOpcode()).hasDefOfPhysReg(Inst, PC, MCRI);
97 }
98
99 /// Returns true if at least one of the register writes performed by
100 /// \param Inst implicitly clears the upper portion of all super-registers.
101 ///
102 /// Example: on X86-64, a write to EAX implicitly clears the upper half of
103 /// RAX. Also (still on x86) an XMM write perfomed by an AVX 128-bit
104 /// instruction implicitly clears the upper portion of the correspondent
105 /// YMM register.
106 ///
107 /// This method also updates an APInt which is used as mask of register
108 /// writes. There is one bit for every explicit/implicit write performed by
109 /// the instruction. If a write implicitly clears its super-registers, then
110 /// the corresponding bit is set (vic. the corresponding bit is cleared).
111 ///
112 /// The first bits in the APint are related to explicit writes. The remaining
113 /// bits are related to implicit writes. The sequence of writes follows the
114 /// machine operand sequence. For implicit writes, the sequence is defined by
115 /// the MCInstrDesc.
116 ///
117 /// The assumption is that the bit-width of the APInt is correctly set by
118 /// the caller. The default implementation conservatively assumes that none of
119 /// the writes clears the upper portion of a super-register.
120 virtual bool clearsSuperRegisters(const MCRegisterInfo &MRI,
121 const MCInst &Inst,
122 APInt &Writes) const;
123
124 /// Returns true if MI is a dependency breaking zero-idiom for the given
125 /// subtarget.
126 ///
127 /// Mask is used to identify input operands that have their dependency
128 /// broken. Each bit of the mask is associated with a specific input operand.
129 /// Bits associated with explicit input operands are laid out first in the
130 /// mask; implicit operands come after explicit operands.
131 ///
132 /// Dependencies are broken only for operands that have their corresponding bit
133 /// set. Operands that have their bit cleared, or that don't have a
134 /// corresponding bit in the mask don't have their dependency broken. Note
135 /// that Mask may not be big enough to describe all operands. The assumption
136 /// for operands that don't have a correspondent bit in the mask is that those
137 /// are still data dependent.
138 ///
139 /// The only exception to the rule is for when Mask has all zeroes.
140 /// A zero mask means: dependencies are broken for all explicit register
141 /// operands.
142 virtual bool isZeroIdiom(const MCInst &MI, APInt &Mask,
143 unsigned CPUID) const {
144 return false;
145 }
146
147 /// Returns true if MI is a dependency breaking instruction for the
148 /// subtarget associated with CPUID .
149 ///
150 /// The value computed by a dependency breaking instruction is not dependent
151 /// on the inputs. An example of dependency breaking instruction on X86 is
152 /// `XOR %eax, %eax`.
153 ///
154 /// If MI is a dependency breaking instruction for subtarget CPUID, then Mask
155 /// can be inspected to identify independent operands.
156 ///
157 /// Essentially, each bit of the mask corresponds to an input operand.
158 /// Explicit operands are laid out first in the mask; implicit operands follow
159 /// explicit operands. Bits are set for operands that are independent.
160 ///
161 /// Note that the number of bits in Mask may not be equivalent to the sum of
162 /// explicit and implicit operands in MI. Operands that don't have a
163 /// corresponding bit in Mask are assumed "not independente".
164 ///
165 /// The only exception is for when Mask is all zeroes. That means: explicit
166 /// input operands of MI are independent.
167 virtual bool isDependencyBreaking(const MCInst &MI, APInt &Mask,
168 unsigned CPUID) const {
169 return isZeroIdiom(MI, Mask, CPUID);
170 }
171
172 /// Returns true if MI is a candidate for move elimination.
173 ///
174 /// Different subtargets may apply different constraints to optimizable
175 /// register moves. For example, on most X86 subtargets, a candidate for move
176 /// elimination cannot specify the same register for both source and
177 /// destination.
179 unsigned CPUID) const {
180 return false;
181 }
182
183 /// Given a branch instruction try to get the address the branch
184 /// targets. Return true on success, and the address in Target.
185 virtual bool
186 evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
187 uint64_t &Target) const;
188
189 /// Given an instruction tries to get the address of a memory operand. Returns
190 /// the address on success.
191 virtual std::optional<uint64_t>
192 evaluateMemoryOperandAddress(const MCInst &Inst, const MCSubtargetInfo *STI,
193 uint64_t Addr, uint64_t Size) const;
194
195 /// Given an instruction with a memory operand that could require relocation,
196 /// returns the offset within the instruction of that relocation.
197 virtual std::optional<uint64_t>
198 getMemoryOperandRelocationOffset(const MCInst &Inst, uint64_t Size) const;
199
200 /// Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
201 virtual std::vector<std::pair<uint64_t, uint64_t>>
202 findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
203 const MCSubtargetInfo &STI) const {
204 return {};
205 }
206};
207
208} // end namespace llvm
209
210#endif // LLVM_MC_MCINSTRANALYSIS_H
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Addr
uint64_t Size
SmallVector< uint32_t, 0 > Writes
Definition: ELF_riscv.cpp:497
IRTranslator LLVM IR MI
static bool isBranch(unsigned Opcode)
Class for arbitrary precision integers.
Definition: APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
unsigned getOpcode() const
Definition: MCInst.h:202
virtual std::vector< std::pair< uint64_t, uint64_t > > findPltEntries(uint64_t PltSectionVA, ArrayRef< uint8_t > PltContents, const MCSubtargetInfo &STI) const
Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
virtual bool isCall(const MCInst &Inst) const
virtual bool isBranch(const MCInst &Inst) const
virtual bool isOptimizableRegisterMove(const MCInst &MI, unsigned CPUID) const
Returns true if MI is a candidate for move elimination.
virtual bool isDependencyBreaking(const MCInst &MI, APInt &Mask, unsigned CPUID) const
Returns true if MI is a dependency breaking instruction for the subtarget associated with CPUID .
virtual bool isUnconditionalBranch(const MCInst &Inst) const
virtual bool isZeroIdiom(const MCInst &MI, APInt &Mask, unsigned CPUID) const
Returns true if MI is a dependency breaking zero-idiom for the given subtarget.
virtual bool isTerminator(const MCInst &Inst) const
virtual bool isBarrier(const MCInst &Inst) const
virtual void resetState()
Clear the internal state. See updateState for more information.
virtual bool isConditionalBranch(const MCInst &Inst) const
virtual bool mayAffectControlFlow(const MCInst &Inst, const MCRegisterInfo &MCRI) const
virtual bool isReturn(const MCInst &Inst) const
virtual void updateState(const MCInst &Inst, uint64_t Addr)
Update internal state with Inst at Addr.
const MCInstrInfo * Info
MCInstrAnalysis(const MCInstrInfo *Info)
virtual ~MCInstrAnalysis()=default
virtual bool isIndirectBranch(const MCInst &Inst) const
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:27
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
MCRegister getProgramCounter() const
Return the register which is the program counter.
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18