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