MCInstrAnalysis.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef LLVM_MC_MCINSTRANALYSIS_H
  14. #define LLVM_MC_MCINSTRANALYSIS_H
  15. #include "llvm/MC/MCInst.h"
  16. #include "llvm/MC/MCInstrDesc.h"
  17. #include "llvm/MC/MCInstrInfo.h"
  18. #include <cstdint>
  19. namespace llvm {
  20. class MCRegisterInfo;
  21. class Triple;
  22. class MCInstrAnalysis {
  23. protected:
  24. friend class Target;
  25. const MCInstrInfo *Info;
  26. public:
  27. MCInstrAnalysis(const MCInstrInfo *Info) : Info(Info) {}
  28. virtual ~MCInstrAnalysis() = default;
  29. virtual bool isBranch(const MCInst &Inst) const {
  30. return Info->get(Inst.getOpcode()).isBranch();
  31. }
  32. virtual bool isConditionalBranch(const MCInst &Inst) const {
  33. return Info->get(Inst.getOpcode()).isConditionalBranch();
  34. }
  35. virtual bool isUnconditionalBranch(const MCInst &Inst) const {
  36. return Info->get(Inst.getOpcode()).isUnconditionalBranch();
  37. }
  38. virtual bool isIndirectBranch(const MCInst &Inst) const {
  39. return Info->get(Inst.getOpcode()).isIndirectBranch();
  40. }
  41. virtual bool isCall(const MCInst &Inst) const {
  42. return Info->get(Inst.getOpcode()).isCall();
  43. }
  44. virtual bool isReturn(const MCInst &Inst) const {
  45. return Info->get(Inst.getOpcode()).isReturn();
  46. }
  47. virtual bool isTerminator(const MCInst &Inst) const {
  48. return Info->get(Inst.getOpcode()).isTerminator();
  49. }
  50. /// Returns true if at least one of the register writes performed by
  51. /// \param Inst implicitly clears the upper portion of all super-registers.
  52. ///
  53. /// Example: on X86-64, a write to EAX implicitly clears the upper half of
  54. /// RAX. Also (still on x86) an XMM write perfomed by an AVX 128-bit
  55. /// instruction implicitly clears the upper portion of the correspondent
  56. /// YMM register.
  57. ///
  58. /// This method also updates an APInt which is used as mask of register
  59. /// writes. There is one bit for every explicit/implicit write performed by
  60. /// the instruction. If a write implicitly clears its super-registers, then
  61. /// the corresponding bit is set (vic. the corresponding bit is cleared).
  62. ///
  63. /// The first bits in the APint are related to explicit writes. The remaining
  64. /// bits are related to implicit writes. The sequence of writes follows the
  65. /// machine operand sequence. For implicit writes, the sequence is defined by
  66. /// the MCInstrDesc.
  67. ///
  68. /// The assumption is that the bit-width of the APInt is correctly set by
  69. /// the caller. The default implementation conservatively assumes that none of
  70. /// the writes clears the upper portion of a super-register.
  71. virtual bool clearsSuperRegisters(const MCRegisterInfo &MRI,
  72. const MCInst &Inst,
  73. APInt &Writes) const;
  74. /// Returns true if MI is a dependency breaking zero-idiom for the given
  75. /// subtarget.
  76. ///
  77. /// Mask is used to identify input operands that have their dependency
  78. /// broken. Each bit of the mask is associated with a specific input operand.
  79. /// Bits associated with explicit input operands are laid out first in the
  80. /// mask; implicit operands come after explicit operands.
  81. ///
  82. /// Dependencies are broken only for operands that have their corresponding bit
  83. /// set. Operands that have their bit cleared, or that don't have a
  84. /// corresponding bit in the mask don't have their dependency broken. Note
  85. /// that Mask may not be big enough to describe all operands. The assumption
  86. /// for operands that don't have a correspondent bit in the mask is that those
  87. /// are still data dependent.
  88. ///
  89. /// The only exception to the rule is for when Mask has all zeroes.
  90. /// A zero mask means: dependencies are broken for all explicit register
  91. /// operands.
  92. virtual bool isZeroIdiom(const MCInst &MI, APInt &Mask,
  93. unsigned CPUID) const {
  94. return false;
  95. }
  96. /// Returns true if MI is a dependency breaking instruction for the
  97. /// subtarget associated with CPUID .
  98. ///
  99. /// The value computed by a dependency breaking instruction is not dependent
  100. /// on the inputs. An example of dependency breaking instruction on X86 is
  101. /// `XOR %eax, %eax`.
  102. ///
  103. /// If MI is a dependency breaking instruction for subtarget CPUID, then Mask
  104. /// can be inspected to identify independent operands.
  105. ///
  106. /// Essentially, each bit of the mask corresponds to an input operand.
  107. /// Explicit operands are laid out first in the mask; implicit operands follow
  108. /// explicit operands. Bits are set for operands that are independent.
  109. ///
  110. /// Note that the number of bits in Mask may not be equivalent to the sum of
  111. /// explicit and implicit operands in MI. Operands that don't have a
  112. /// corresponding bit in Mask are assumed "not independente".
  113. ///
  114. /// The only exception is for when Mask is all zeroes. That means: explicit
  115. /// input operands of MI are independent.
  116. virtual bool isDependencyBreaking(const MCInst &MI, APInt &Mask,
  117. unsigned CPUID) const {
  118. return isZeroIdiom(MI, Mask, CPUID);
  119. }
  120. /// Returns true if MI is a candidate for move elimination.
  121. ///
  122. /// Different subtargets may apply different constraints to optimizable
  123. /// register moves. For example, on most X86 subtargets, a candidate for move
  124. /// elimination cannot specify the same register for both source and
  125. /// destination.
  126. virtual bool isOptimizableRegisterMove(const MCInst &MI,
  127. unsigned CPUID) const {
  128. return false;
  129. }
  130. /// Given a branch instruction try to get the address the branch
  131. /// targets. Return true on success, and the address in Target.
  132. virtual bool
  133. evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
  134. uint64_t &Target) const;
  135. /// Given an instruction tries to get the address of a memory operand. Returns
  136. /// the address on success.
  137. virtual Optional<uint64_t> evaluateMemoryOperandAddress(const MCInst &Inst,
  138. uint64_t Addr,
  139. uint64_t Size) const;
  140. /// Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
  141. virtual std::vector<std::pair<uint64_t, uint64_t>>
  142. findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
  143. uint64_t GotPltSectionVA, const Triple &TargetTriple) const {
  144. return {};
  145. }
  146. };
  147. } // end namespace llvm
  148. #endif // LLVM_MC_MCINSTRANALYSIS_H