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