LLVM 18.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"
22#include <cstdint>
23#include <vector>
24
25namespace llvm {
26
27class MCRegisterInfo;
28class Triple;
29
31protected:
32 friend class Target;
33
35
36public:
38 virtual ~MCInstrAnalysis() = default;
39
40 virtual bool isBranch(const MCInst &Inst) const {
41 return Info->get(Inst.getOpcode()).isBranch();
42 }
43
44 virtual bool isConditionalBranch(const MCInst &Inst) const {
45 return Info->get(Inst.getOpcode()).isConditionalBranch();
46 }
47
48 virtual bool isUnconditionalBranch(const MCInst &Inst) const {
49 return Info->get(Inst.getOpcode()).isUnconditionalBranch();
50 }
51
52 virtual bool isIndirectBranch(const MCInst &Inst) const {
53 return Info->get(Inst.getOpcode()).isIndirectBranch();
54 }
55
56 virtual bool isCall(const MCInst &Inst) const {
57 return Info->get(Inst.getOpcode()).isCall();
58 }
59
60 virtual bool isReturn(const MCInst &Inst) const {
61 return Info->get(Inst.getOpcode()).isReturn();
62 }
63
64 virtual bool isTerminator(const MCInst &Inst) const {
65 return Info->get(Inst.getOpcode()).isTerminator();
66 }
67
68 virtual bool mayAffectControlFlow(const MCInst &Inst,
69 const MCRegisterInfo &MCRI) const {
70 if (isBranch(Inst) || isCall(Inst) || isReturn(Inst) ||
71 isIndirectBranch(Inst))
72 return true;
73 unsigned PC = MCRI.getProgramCounter();
74 if (PC == 0)
75 return false;
76 return Info->get(Inst.getOpcode()).hasDefOfPhysReg(Inst, PC, MCRI);
77 }
78
79 /// Returns true if at least one of the register writes performed by
80 /// \param Inst implicitly clears the upper portion of all super-registers.
81 ///
82 /// Example: on X86-64, a write to EAX implicitly clears the upper half of
83 /// RAX. Also (still on x86) an XMM write perfomed by an AVX 128-bit
84 /// instruction implicitly clears the upper portion of the correspondent
85 /// YMM register.
86 ///
87 /// This method also updates an APInt which is used as mask of register
88 /// writes. There is one bit for every explicit/implicit write performed by
89 /// the instruction. If a write implicitly clears its super-registers, then
90 /// the corresponding bit is set (vic. the corresponding bit is cleared).
91 ///
92 /// The first bits in the APint are related to explicit writes. The remaining
93 /// bits are related to implicit writes. The sequence of writes follows the
94 /// machine operand sequence. For implicit writes, the sequence is defined by
95 /// the MCInstrDesc.
96 ///
97 /// The assumption is that the bit-width of the APInt is correctly set by
98 /// the caller. The default implementation conservatively assumes that none of
99 /// the writes clears the upper portion of a super-register.
100 virtual bool clearsSuperRegisters(const MCRegisterInfo &MRI,
101 const MCInst &Inst,
102 APInt &Writes) const;
103
104 /// Returns true if MI is a dependency breaking zero-idiom for the given
105 /// subtarget.
106 ///
107 /// Mask is used to identify input operands that have their dependency
108 /// broken. Each bit of the mask is associated with a specific input operand.
109 /// Bits associated with explicit input operands are laid out first in the
110 /// mask; implicit operands come after explicit operands.
111 ///
112 /// Dependencies are broken only for operands that have their corresponding bit
113 /// set. Operands that have their bit cleared, or that don't have a
114 /// corresponding bit in the mask don't have their dependency broken. Note
115 /// that Mask may not be big enough to describe all operands. The assumption
116 /// for operands that don't have a correspondent bit in the mask is that those
117 /// are still data dependent.
118 ///
119 /// The only exception to the rule is for when Mask has all zeroes.
120 /// A zero mask means: dependencies are broken for all explicit register
121 /// operands.
122 virtual bool isZeroIdiom(const MCInst &MI, APInt &Mask,
123 unsigned CPUID) const {
124 return false;
125 }
126
127 /// Returns true if MI is a dependency breaking instruction for the
128 /// subtarget associated with CPUID .
129 ///
130 /// The value computed by a dependency breaking instruction is not dependent
131 /// on the inputs. An example of dependency breaking instruction on X86 is
132 /// `XOR %eax, %eax`.
133 ///
134 /// If MI is a dependency breaking instruction for subtarget CPUID, then Mask
135 /// can be inspected to identify independent operands.
136 ///
137 /// Essentially, each bit of the mask corresponds to an input operand.
138 /// Explicit operands are laid out first in the mask; implicit operands follow
139 /// explicit operands. Bits are set for operands that are independent.
140 ///
141 /// Note that the number of bits in Mask may not be equivalent to the sum of
142 /// explicit and implicit operands in MI. Operands that don't have a
143 /// corresponding bit in Mask are assumed "not independente".
144 ///
145 /// The only exception is for when Mask is all zeroes. That means: explicit
146 /// input operands of MI are independent.
147 virtual bool isDependencyBreaking(const MCInst &MI, APInt &Mask,
148 unsigned CPUID) const {
149 return isZeroIdiom(MI, Mask, CPUID);
150 }
151
152 /// Returns true if MI is a candidate for move elimination.
153 ///
154 /// Different subtargets may apply different constraints to optimizable
155 /// register moves. For example, on most X86 subtargets, a candidate for move
156 /// elimination cannot specify the same register for both source and
157 /// destination.
159 unsigned CPUID) const {
160 return false;
161 }
162
163 /// Given a branch instruction try to get the address the branch
164 /// targets. Return true on success, and the address in Target.
165 virtual bool
167 uint64_t &Target) const;
168
169 /// Given an instruction tries to get the address of a memory operand. Returns
170 /// the address on success.
171 virtual std::optional<uint64_t>
173 uint64_t Addr, uint64_t Size) const;
174
175 /// Given an instruction with a memory operand that could require relocation,
176 /// returns the offset within the instruction of that relocation.
177 virtual std::optional<uint64_t>
179
180 /// Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
181 virtual std::vector<std::pair<uint64_t, uint64_t>>
182 findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
183 const Triple &TargetTriple) const {
184 return {};
185 }
186};
187
188} // end namespace llvm
189
190#endif // LLVM_MC_MCINSTRANALYSIS_H
unsigned const MachineRegisterInfo * MRI
uint64_t Addr
uint64_t Size
SmallVector< uint32_t, 0 > Writes
Definition: ELF_riscv.cpp:486
IRTranslator LLVM IR MI
Class for arbitrary precision integers.
Definition: APInt.h:76
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:184
unsigned getOpcode() const
Definition: MCInst.h:198
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 std::optional< uint64_t > getMemoryOperandRelocationOffset(const MCInst &Inst, uint64_t Size) const
Given an instruction with a memory operand that could require relocation, returns the offset within t...
virtual bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, uint64_t &Target) const
Given a branch instruction try to get the address the branch targets.
virtual std::vector< std::pair< uint64_t, uint64_t > > findPltEntries(uint64_t PltSectionVA, ArrayRef< uint8_t > PltContents, const Triple &TargetTriple) const
Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
virtual bool isTerminator(const MCInst &Inst) const
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 std::optional< uint64_t > evaluateMemoryOperandAddress(const MCInst &Inst, const MCSubtargetInfo *STI, uint64_t Addr, uint64_t Size) const
Given an instruction tries to get the address of a memory operand.
virtual bool clearsSuperRegisters(const MCRegisterInfo &MRI, const MCInst &Inst, APInt &Writes) const
Returns true if at least one of the register writes performed by.
const MCInstrInfo * Info
MCInstrAnalysis(const MCInstrInfo *Info)
virtual ~MCInstrAnalysis()=default
virtual bool isIndirectBranch(const MCInst &Inst) const
bool isIndirectBranch() const
Return true if this is an indirect branch, such as a branch through a register.
Definition: MCInstrDesc.h:311
bool hasDefOfPhysReg(const MCInst &MI, unsigned Reg, const MCRegisterInfo &RI) const
Return true if this instruction defines the specified physical register, either explicitly or implici...
Definition: MCInstrDesc.cpp:40
bool isBranch() const
Returns true if this is a conditional, unconditional, or indirect branch.
Definition: MCInstrDesc.h:307
bool isUnconditionalBranch() const
Return true if this is a branch which always transfers control flow to some other block.
Definition: MCInstrDesc.h:325
bool isCall() const
Return true if the instruction is a call.
Definition: MCInstrDesc.h:288
bool isTerminator() const
Returns true if this instruction part of the terminator for a basic block.
Definition: MCInstrDesc.h:301
bool isReturn() const
Return true if the instruction is a return.
Definition: MCInstrDesc.h:276
bool isConditionalBranch() const
Return true if this is a branch which may fall through to the next instruction or may transfer contro...
Definition: MCInstrDesc.h:317
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition: MCInstrInfo.h:63
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.
Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18