blob: e0cb0aa676a6f7fec71d8f6f7f284c6e26c10f70 [file] [log] [blame]
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +00001//===-- 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
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000017#include "llvm/Support/MathExtras.h"
18#include <algorithm>
19#include <cstdint>
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000020
21namespace 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.
29inline 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.
37struct 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.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000047 unsigned Offset = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000048
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.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000054 unsigned Size = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000055
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.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000058 uint8_t KnownBits = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000059
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.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000063 uint8_t Unalign = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000064
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.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000068 uint8_t PostAlign = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000069
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000070 BasicBlockInfo() = default;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000071
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.
75 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
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000109#endif // LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H