blob: 18e7195e1a9ca100cfb2b59822fae4f68a9af0ea [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
Sam Parker966f4e82019-06-17 08:49:09 +000016#include "ARMBaseInstrInfo.h"
17#include "ARMMachineFunctionInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000018#include "llvm/Support/MathExtras.h"
19#include <algorithm>
20#include <cstdint>
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000021
22namespace llvm {
23
Sam Parker966f4e82019-06-17 08:49:09 +000024using BBInfoVector = SmallVectorImpl<BasicBlockInfo>;
25
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000026/// UnknownPadding - Return the worst case padding that could result from
27/// unknown offset bits. This does not include alignment padding caused by
28/// known offset bits.
29///
Guillaume Chateletd4c46712019-09-18 15:49:49 +000030/// @param Align alignment
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000031/// @param KnownBits Number of known low offset bits.
Guillaume Chateletd4c46712019-09-18 15:49:49 +000032inline unsigned UnknownPadding(llvm::Align Align, unsigned KnownBits) {
33 if (KnownBits < Log2(Align))
Simon Pilgrimda894952019-09-19 10:47:12 +000034 return Align.value() - (1ull << KnownBits);
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000035 return 0;
36}
37
38/// BasicBlockInfo - Information about the offset and size of a single
39/// basic block.
40struct BasicBlockInfo {
41 /// Offset - Distance from the beginning of the function to the beginning
42 /// of this basic block.
43 ///
44 /// Offsets are computed assuming worst case padding before an aligned
45 /// block. This means that subtracting basic block offsets always gives a
46 /// conservative estimate of the real distance which may be smaller.
47 ///
48 /// Because worst case padding is used, the computed offset of an aligned
49 /// block may not actually be aligned.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000050 unsigned Offset = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000051
52 /// Size - Size of the basic block in bytes. If the block contains
53 /// inline assembly, this is a worst case estimate.
54 ///
55 /// The size does not include any alignment padding whether from the
56 /// beginning of the block, or from an aligned jump table at the end.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000057 unsigned Size = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000058
59 /// KnownBits - The number of low bits in Offset that are known to be
60 /// exact. The remaining bits of Offset are an upper bound.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000061 uint8_t KnownBits = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000062
63 /// Unalign - When non-zero, the block contains instructions (inline asm)
64 /// of unknown size. The real size may be smaller than Size bytes by a
65 /// multiple of 1 << Unalign.
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000066 uint8_t Unalign = 0;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000067
Guillaume Chateletd4c46712019-09-18 15:49:49 +000068 /// PostAlign - When > 1, the block terminator contains a .align
69 /// directive, so the end of the block is aligned to PostAlign bytes.
70 llvm::Align PostAlign;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000071
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000072 BasicBlockInfo() = default;
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000073
74 /// Compute the number of known offset bits internally to this block.
75 /// This number should be used to predict worst case padding when
76 /// splitting the block.
77 unsigned internalKnownBits() const {
78 unsigned Bits = Unalign ? Unalign : KnownBits;
79 // If the block size isn't a multiple of the known bits, assume the
80 // worst case padding.
81 if (Size & ((1u << Bits) - 1))
82 Bits = countTrailingZeros(Size);
83 return Bits;
84 }
85
Guillaume Chateletd4c46712019-09-18 15:49:49 +000086 /// Compute the offset immediately following this block. If Align is
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000087 /// specified, return the offset the successor block will get if it has
88 /// this alignment.
Guillaume Chateletd4c46712019-09-18 15:49:49 +000089 unsigned postOffset(llvm::Align Align = llvm::Align::None()) const {
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000090 unsigned PO = Offset + Size;
Guillaume Chateletd4c46712019-09-18 15:49:49 +000091 const llvm::Align PA = std::max(PostAlign, Align);
92 if (PA == llvm::Align::None())
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000093 return PO;
94 // Add alignment padding from the terminator.
Guillaume Chateletd4c46712019-09-18 15:49:49 +000095 return PO + UnknownPadding(PA, internalKnownBits());
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000096 }
97
98 /// Compute the number of known low bits of postOffset. If this block
99 /// contains inline asm, the number of known bits drops to the
100 /// instruction alignment. An aligned terminator may increase the number
101 /// of know bits.
102 /// If LogAlign is given, also consider the alignment of the next block.
Guillaume Chateletd4c46712019-09-18 15:49:49 +0000103 unsigned postKnownBits(llvm::Align Align = llvm::Align::None()) const {
104 return std::max(Log2(std::max(PostAlign, Align)), internalKnownBits());
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +0000105 }
106};
107
Sam Parker966f4e82019-06-17 08:49:09 +0000108class ARMBasicBlockUtils {
109
110private:
111 MachineFunction &MF;
112 bool isThumb = false;
113 const ARMBaseInstrInfo *TII = nullptr;
114 SmallVector<BasicBlockInfo, 8> BBInfo;
115
116public:
117 ARMBasicBlockUtils(MachineFunction &MF) : MF(MF) {
118 TII =
119 static_cast<const ARMBaseInstrInfo*>(MF.getSubtarget().getInstrInfo());
120 isThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
121 }
122
123 void computeAllBlockSizes() {
124 BBInfo.resize(MF.getNumBlockIDs());
125 for (MachineBasicBlock &MBB : MF)
126 computeBlockSize(&MBB);
127 }
128
129 void computeBlockSize(MachineBasicBlock *MBB);
130
131 unsigned getOffsetOf(MachineInstr *MI) const;
132
Sam Parker08b4a8d2019-07-11 09:56:15 +0000133 unsigned getOffsetOf(MachineBasicBlock *MBB) const {
134 return BBInfo[MBB->getNumber()].Offset;
135 }
136
Sam Parker966f4e82019-06-17 08:49:09 +0000137 void adjustBBOffsetsAfter(MachineBasicBlock *MBB);
138
139 void adjustBBSize(MachineBasicBlock *MBB, int Size) {
140 BBInfo[MBB->getNumber()].Size += Size;
141 }
142
143 bool isBBInRange(MachineInstr *MI, MachineBasicBlock *DestBB,
144 unsigned MaxDisp) const;
145
146 void insert(unsigned BBNum, BasicBlockInfo BBI) {
147 BBInfo.insert(BBInfo.begin() + BBNum, BBI);
148 }
149
150 void clear() { BBInfo.clear(); }
151
152 BBInfoVector &getBBInfo() { return BBInfo; }
153
154};
155
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +0000156} // end namespace llvm
157
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000158#endif // LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H