ARMBasicBlockInfo.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===-- ARMBasicBlockInfo.h - Basic Block Information -----------*- 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. // Utility functions and data structure for computing block size.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H
  13. #define LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H
  14. #include "ARMBaseInstrInfo.h"
  15. #include "ARMMachineFunctionInfo.h"
  16. #include "llvm/Support/MathExtras.h"
  17. #include <algorithm>
  18. #include <cstdint>
  19. namespace llvm {
  20. struct BasicBlockInfo;
  21. using BBInfoVector = SmallVectorImpl<BasicBlockInfo>;
  22. /// UnknownPadding - Return the worst case padding that could result from
  23. /// unknown offset bits. This does not include alignment padding caused by
  24. /// known offset bits.
  25. ///
  26. /// @param Alignment alignment
  27. /// @param KnownBits Number of known low offset bits.
  28. inline unsigned UnknownPadding(Align Alignment, unsigned KnownBits) {
  29. if (KnownBits < Log2(Alignment))
  30. return Alignment.value() - (1ull << KnownBits);
  31. return 0;
  32. }
  33. /// BasicBlockInfo - Information about the offset and size of a single
  34. /// basic block.
  35. struct BasicBlockInfo {
  36. /// Offset - Distance from the beginning of the function to the beginning
  37. /// of this basic block.
  38. ///
  39. /// Offsets are computed assuming worst case padding before an aligned
  40. /// block. This means that subtracting basic block offsets always gives a
  41. /// conservative estimate of the real distance which may be smaller.
  42. ///
  43. /// Because worst case padding is used, the computed offset of an aligned
  44. /// block may not actually be aligned.
  45. unsigned Offset = 0;
  46. /// Size - Size of the basic block in bytes. If the block contains
  47. /// inline assembly, this is a worst case estimate.
  48. ///
  49. /// The size does not include any alignment padding whether from the
  50. /// beginning of the block, or from an aligned jump table at the end.
  51. unsigned Size = 0;
  52. /// KnownBits - The number of low bits in Offset that are known to be
  53. /// exact. The remaining bits of Offset are an upper bound.
  54. uint8_t KnownBits = 0;
  55. /// Unalign - When non-zero, the block contains instructions (inline asm)
  56. /// of unknown size. The real size may be smaller than Size bytes by a
  57. /// multiple of 1 << Unalign.
  58. uint8_t Unalign = 0;
  59. /// PostAlign - When > 1, the block terminator contains a .align
  60. /// directive, so the end of the block is aligned to PostAlign bytes.
  61. Align PostAlign;
  62. BasicBlockInfo() = default;
  63. /// Compute the number of known offset bits internally to this block.
  64. /// This number should be used to predict worst case padding when
  65. /// splitting the block.
  66. unsigned internalKnownBits() const {
  67. unsigned Bits = Unalign ? Unalign : KnownBits;
  68. // If the block size isn't a multiple of the known bits, assume the
  69. // worst case padding.
  70. if (Size & ((1u << Bits) - 1))
  71. Bits = countTrailingZeros(Size);
  72. return Bits;
  73. }
  74. /// Compute the offset immediately following this block. If Align is
  75. /// specified, return the offset the successor block will get if it has
  76. /// this alignment.
  77. unsigned postOffset(Align Alignment = Align(1)) const {
  78. unsigned PO = Offset + Size;
  79. const Align PA = std::max(PostAlign, Alignment);
  80. if (PA == Align(1))
  81. return PO;
  82. // Add alignment padding from the terminator.
  83. return PO + UnknownPadding(PA, internalKnownBits());
  84. }
  85. /// Compute the number of known low bits of postOffset. If this block
  86. /// contains inline asm, the number of known bits drops to the
  87. /// instruction alignment. An aligned terminator may increase the number
  88. /// of know bits.
  89. /// If LogAlign is given, also consider the alignment of the next block.
  90. unsigned postKnownBits(Align Align = llvm::Align(1)) const {
  91. return std::max(Log2(std::max(PostAlign, Align)), internalKnownBits());
  92. }
  93. };
  94. class ARMBasicBlockUtils {
  95. private:
  96. MachineFunction &MF;
  97. bool isThumb = false;
  98. const ARMBaseInstrInfo *TII = nullptr;
  99. SmallVector<BasicBlockInfo, 8> BBInfo;
  100. public:
  101. ARMBasicBlockUtils(MachineFunction &MF) : MF(MF) {
  102. TII =
  103. static_cast<const ARMBaseInstrInfo*>(MF.getSubtarget().getInstrInfo());
  104. isThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
  105. }
  106. void computeAllBlockSizes() {
  107. BBInfo.resize(MF.getNumBlockIDs());
  108. for (MachineBasicBlock &MBB : MF)
  109. computeBlockSize(&MBB);
  110. }
  111. void computeBlockSize(MachineBasicBlock *MBB);
  112. unsigned getOffsetOf(MachineInstr *MI) const;
  113. unsigned getOffsetOf(MachineBasicBlock *MBB) const {
  114. return BBInfo[MBB->getNumber()].Offset;
  115. }
  116. void adjustBBOffsetsAfter(MachineBasicBlock *MBB);
  117. void adjustBBSize(MachineBasicBlock *MBB, int Size) {
  118. BBInfo[MBB->getNumber()].Size += Size;
  119. }
  120. bool isBBInRange(MachineInstr *MI, MachineBasicBlock *DestBB,
  121. unsigned MaxDisp) const;
  122. void insert(unsigned BBNum, BasicBlockInfo BBI) {
  123. BBInfo.insert(BBInfo.begin() + BBNum, BBI);
  124. }
  125. void clear() { BBInfo.clear(); }
  126. BBInfoVector &getBBInfo() { return BBInfo; }
  127. };
  128. } // end namespace llvm
  129. #endif // LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H