blob: 4eb4106378a2303d6fbc4f3749e9070d2b7e9e8a [file] [log] [blame]
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +00001//===-- ARMBasicBlockInfo.h - Basic Block Information -----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Meijer5c0ef832016-07-22 08:39:12 +00006//
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 Zelenkoe6cf4372017-01-26 23:40:06 +000016#include "llvm/Support/MathExtras.h"
17#include <algorithm>
18#include <cstdint>
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000019
20namespace 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.
28inline 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.
36struct 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 Zelenkoe6cf4372017-01-26 23:40:06 +000046 unsigned Offset = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000047
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 Zelenkoe6cf4372017-01-26 23:40:06 +000053 unsigned Size = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000054
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 Zelenkoe6cf4372017-01-26 23:40:06 +000057 uint8_t KnownBits = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000058
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 Zelenkoe6cf4372017-01-26 23:40:06 +000062 uint8_t Unalign = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000063
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 Zelenkoe6cf4372017-01-26 23:40:06 +000067 uint8_t PostAlign = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000068
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000069 BasicBlockInfo() = default;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000070
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 Zelenkoe6cf4372017-01-26 23:40:06 +0000108#endif // LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H