Path: blob/main/contrib/llvm-project/llvm/lib/Target/ARM/ARMBasicBlockInfo.h
35268 views
//===-- ARMBasicBlockInfo.h - Basic Block Information -----------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Utility functions and data structure for computing block size.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H13#define LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H1415#include "ARMBaseInstrInfo.h"16#include "ARMMachineFunctionInfo.h"17#include "llvm/Support/MathExtras.h"18#include <algorithm>19#include <cstdint>2021namespace llvm {2223struct BasicBlockInfo;24using BBInfoVector = SmallVectorImpl<BasicBlockInfo>;2526/// UnknownPadding - Return the worst case padding that could result from27/// unknown offset bits. This does not include alignment padding caused by28/// known offset bits.29///30/// @param Alignment alignment31/// @param KnownBits Number of known low offset bits.32inline unsigned UnknownPadding(Align Alignment, unsigned KnownBits) {33if (KnownBits < Log2(Alignment))34return Alignment.value() - (1ull << KnownBits);35return 0;36}3738/// BasicBlockInfo - Information about the offset and size of a single39/// basic block.40struct BasicBlockInfo {41/// Offset - Distance from the beginning of the function to the beginning42/// of this basic block.43///44/// Offsets are computed assuming worst case padding before an aligned45/// block. This means that subtracting basic block offsets always gives a46/// conservative estimate of the real distance which may be smaller.47///48/// Because worst case padding is used, the computed offset of an aligned49/// block may not actually be aligned.50unsigned Offset = 0;5152/// Size - Size of the basic block in bytes. If the block contains53/// inline assembly, this is a worst case estimate.54///55/// The size does not include any alignment padding whether from the56/// beginning of the block, or from an aligned jump table at the end.57unsigned Size = 0;5859/// KnownBits - The number of low bits in Offset that are known to be60/// exact. The remaining bits of Offset are an upper bound.61uint8_t KnownBits = 0;6263/// Unalign - When non-zero, the block contains instructions (inline asm)64/// of unknown size. The real size may be smaller than Size bytes by a65/// multiple of 1 << Unalign.66uint8_t Unalign = 0;6768/// PostAlign - When > 1, the block terminator contains a .align69/// directive, so the end of the block is aligned to PostAlign bytes.70Align PostAlign;7172BasicBlockInfo() = default;7374/// Compute the number of known offset bits internally to this block.75/// This number should be used to predict worst case padding when76/// splitting the block.77unsigned internalKnownBits() const {78unsigned Bits = Unalign ? Unalign : KnownBits;79// If the block size isn't a multiple of the known bits, assume the80// worst case padding.81if (Size & ((1u << Bits) - 1))82Bits = llvm::countr_zero(Size);83return Bits;84}8586/// Compute the offset immediately following this block. If Align is87/// specified, return the offset the successor block will get if it has88/// this alignment.89unsigned postOffset(Align Alignment = Align(1)) const {90unsigned PO = Offset + Size;91const Align PA = std::max(PostAlign, Alignment);92if (PA == Align(1))93return PO;94// Add alignment padding from the terminator.95return PO + UnknownPadding(PA, internalKnownBits());96}9798/// Compute the number of known low bits of postOffset. If this block99/// contains inline asm, the number of known bits drops to the100/// instruction alignment. An aligned terminator may increase the number101/// of know bits.102/// If LogAlign is given, also consider the alignment of the next block.103unsigned postKnownBits(Align Align = llvm::Align(1)) const {104return std::max(Log2(std::max(PostAlign, Align)), internalKnownBits());105}106};107108class ARMBasicBlockUtils {109110private:111MachineFunction &MF;112bool isThumb = false;113const ARMBaseInstrInfo *TII = nullptr;114SmallVector<BasicBlockInfo, 8> BBInfo;115116public:117ARMBasicBlockUtils(MachineFunction &MF) : MF(MF) {118TII =119static_cast<const ARMBaseInstrInfo*>(MF.getSubtarget().getInstrInfo());120isThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();121}122123void computeAllBlockSizes() {124BBInfo.resize(MF.getNumBlockIDs());125for (MachineBasicBlock &MBB : MF)126computeBlockSize(&MBB);127}128129void computeBlockSize(MachineBasicBlock *MBB);130131unsigned getOffsetOf(MachineInstr *MI) const;132133unsigned getOffsetOf(MachineBasicBlock *MBB) const {134return BBInfo[MBB->getNumber()].Offset;135}136137void adjustBBOffsetsAfter(MachineBasicBlock *MBB);138139void adjustBBSize(MachineBasicBlock *MBB, int Size) {140BBInfo[MBB->getNumber()].Size += Size;141}142143bool isBBInRange(MachineInstr *MI, MachineBasicBlock *DestBB,144unsigned MaxDisp) const;145146void insert(unsigned BBNum, BasicBlockInfo BBI) {147BBInfo.insert(BBInfo.begin() + BBNum, BBI);148}149150void clear() { BBInfo.clear(); }151152BBInfoVector &getBBInfo() { return BBInfo; }153154};155156} // end namespace llvm157158#endif // LLVM_LIB_TARGET_ARM_ARMBASICBLOCKINFO_H159160161