Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 1 | //===-- ARMBasicBlockInfo.h - Basic Block Information -----------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Utility functions and data structure for computing block size. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H |
| 14 | #define LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H |
| 15 | |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MathExtras.h" |
| 17 | #include <algorithm> |
| 18 | #include <cstdint> |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
| 21 | |
| 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 LogAlign log2(alignment) |
| 27 | /// @param KnownBits Number of known low offset bits. |
| 28 | inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) { |
| 29 | if (KnownBits < LogAlign) |
| 30 | return (1u << LogAlign) - (1u << KnownBits); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | /// BasicBlockInfo - Information about the offset and size of a single |
| 35 | /// basic block. |
| 36 | struct BasicBlockInfo { |
| 37 | /// Offset - Distance from the beginning of the function to the beginning |
| 38 | /// of this basic block. |
| 39 | /// |
| 40 | /// Offsets are computed assuming worst case padding before an aligned |
| 41 | /// block. This means that subtracting basic block offsets always gives a |
| 42 | /// conservative estimate of the real distance which may be smaller. |
| 43 | /// |
| 44 | /// Because worst case padding is used, the computed offset of an aligned |
| 45 | /// block may not actually be aligned. |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 46 | unsigned Offset = 0; |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 47 | |
| 48 | /// Size - Size of the basic block in bytes. If the block contains |
| 49 | /// inline assembly, this is a worst case estimate. |
| 50 | /// |
| 51 | /// The size does not include any alignment padding whether from the |
| 52 | /// beginning of the block, or from an aligned jump table at the end. |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 53 | unsigned Size = 0; |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 54 | |
| 55 | /// KnownBits - The number of low bits in Offset that are known to be |
| 56 | /// exact. The remaining bits of Offset are an upper bound. |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 57 | uint8_t KnownBits = 0; |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 58 | |
| 59 | /// Unalign - When non-zero, the block contains instructions (inline asm) |
| 60 | /// of unknown size. The real size may be smaller than Size bytes by a |
| 61 | /// multiple of 1 << Unalign. |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 62 | uint8_t Unalign = 0; |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 63 | |
| 64 | /// PostAlign - When non-zero, the block terminator contains a .align |
| 65 | /// directive, so the end of the block is aligned to 1 << PostAlign |
| 66 | /// bytes. |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 67 | uint8_t PostAlign = 0; |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 68 | |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 69 | BasicBlockInfo() = default; |
Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 70 | |
| 71 | /// Compute the number of known offset bits internally to this block. |
| 72 | /// This number should be used to predict worst case padding when |
| 73 | /// splitting the block. |
| 74 | unsigned internalKnownBits() const { |
| 75 | unsigned Bits = Unalign ? Unalign : KnownBits; |
| 76 | // If the block size isn't a multiple of the known bits, assume the |
| 77 | // worst case padding. |
| 78 | if (Size & ((1u << Bits) - 1)) |
| 79 | Bits = countTrailingZeros(Size); |
| 80 | return Bits; |
| 81 | } |
| 82 | |
| 83 | /// Compute the offset immediately following this block. If LogAlign is |
| 84 | /// specified, return the offset the successor block will get if it has |
| 85 | /// this alignment. |
| 86 | unsigned postOffset(unsigned LogAlign = 0) const { |
| 87 | unsigned PO = Offset + Size; |
| 88 | unsigned LA = std::max(unsigned(PostAlign), LogAlign); |
| 89 | if (!LA) |
| 90 | return PO; |
| 91 | // Add alignment padding from the terminator. |
| 92 | return PO + UnknownPadding(LA, internalKnownBits()); |
| 93 | } |
| 94 | |
| 95 | /// Compute the number of known low bits of postOffset. If this block |
| 96 | /// contains inline asm, the number of known bits drops to the |
| 97 | /// instruction alignment. An aligned terminator may increase the number |
| 98 | /// of know bits. |
| 99 | /// If LogAlign is given, also consider the alignment of the next block. |
| 100 | unsigned postKnownBits(unsigned LogAlign = 0) const { |
| 101 | return std::max(std::max(unsigned(PostAlign), LogAlign), |
| 102 | internalKnownBits()); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | } // end namespace llvm |
| 107 | |
Eugene Zelenko | e6cf437 | 2017-01-26 23:40:06 +0000 | [diff] [blame] | 108 | #endif // LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H |