LLVM 19.0.0git
ARMBasicBlockInfo.cpp
Go to the documentation of this file.
1//===--- ARMBasicBlockInfo.cpp - Utilities for block sizes ---------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "ARMBasicBlockInfo.h"
10#include "ARM.h"
11#include "ARMBaseInstrInfo.h"
18#include "llvm/Support/Debug.h"
19
20#define DEBUG_TYPE "arm-bb-utils"
21
22using namespace llvm;
23
24namespace llvm {
25
26// mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
27// below may shrink MI.
28static bool
30 switch(MI->getOpcode()) {
31 // optimizeThumb2Instructions.
32 case ARM::t2LEApcrel:
33 case ARM::t2LDRpci:
34 // optimizeThumb2Branches.
35 case ARM::t2B:
36 case ARM::t2Bcc:
37 case ARM::tBcc:
38 // optimizeThumb2JumpTables.
39 case ARM::t2BR_JT:
40 case ARM::tBR_JTr:
41 return true;
42 }
43 return false;
44}
45
47 LLVM_DEBUG(dbgs() << "computeBlockSize: " << MBB->getName() << "\n");
48 BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
49 BBI.Size = 0;
50 BBI.Unalign = 0;
51 BBI.PostAlign = Align(1);
52
53 for (MachineInstr &I : *MBB) {
54 BBI.Size += TII->getInstSizeInBytes(I);
55 // For inline asm, getInstSizeInBytes returns a conservative estimate.
56 // The actual size may be smaller, but still a multiple of the instr size.
57 if (I.isInlineAsm())
58 BBI.Unalign = isThumb ? 1 : 2;
59 // Also consider instructions that may be shrunk later.
60 else if (isThumb && mayOptimizeThumb2Instruction(&I))
61 BBI.Unalign = 1;
62 }
63
64 // tBR_JTr contains a .align 2 directive.
65 if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) {
66 BBI.PostAlign = Align(4);
68 }
69}
70
71/// getOffsetOf - Return the current offset of the specified machine instruction
72/// from the start of the function. This offset changes as stuff is moved
73/// around inside the function.
75 const MachineBasicBlock *MBB = MI->getParent();
76
77 // The offset is composed of two things: the sum of the sizes of all MBB's
78 // before this instruction's block, and the offset from the start of the block
79 // it is in.
80 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
81
82 // Sum instructions before MI in MBB.
83 for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != MI; ++I) {
84 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
85 Offset += TII->getInstSizeInBytes(*I);
86 }
87 return Offset;
88}
89
90/// isBBInRange - Returns true if the distance between specific MI and
91/// specific BB can fit in MI's displacement field.
93 MachineBasicBlock *DestBB,
94 unsigned MaxDisp) const {
95 unsigned PCAdj = isThumb ? 4 : 8;
96 unsigned BrOffset = getOffsetOf(MI) + PCAdj;
97 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
98
99 LLVM_DEBUG(dbgs() << "Branch of destination " << printMBBReference(*DestBB)
100 << " from " << printMBBReference(*MI->getParent())
101 << " max delta=" << MaxDisp << " from " << getOffsetOf(MI)
102 << " to " << DestOffset << " offset "
103 << int(DestOffset - BrOffset) << "\t" << *MI);
104
105 if (BrOffset <= DestOffset) {
106 // Branch before the Dest.
107 if (DestOffset-BrOffset <= MaxDisp)
108 return true;
109 } else {
110 if (BrOffset-DestOffset <= MaxDisp)
111 return true;
112 }
113 return false;
114}
115
117 assert(BB->getParent() == &MF &&
118 "Basic block is not a child of the current function.\n");
119
120 unsigned BBNum = BB->getNumber();
121 LLVM_DEBUG(dbgs() << "Adjust block:\n"
122 << " - name: " << BB->getName() << "\n"
123 << " - number: " << BB->getNumber() << "\n"
124 << " - function: " << MF.getName() << "\n"
125 << " - blocks: " << MF.getNumBlockIDs() << "\n");
126
127 for(unsigned i = BBNum + 1, e = MF.getNumBlockIDs(); i < e; ++i) {
128 // Get the offset and known bits at the end of the layout predecessor.
129 // Include the alignment of the current block.
130 const Align Align = MF.getBlockNumbered(i)->getAlignment();
131 const unsigned Offset = BBInfo[i - 1].postOffset(Align);
132 const unsigned KnownBits = BBInfo[i - 1].postKnownBits(Align);
133
134 // This is where block i begins. Stop if the offset is already correct,
135 // and we have updated 2 blocks. This is the maximum number of blocks
136 // changed before calling this function.
137 if (i > BBNum + 2 &&
138 BBInfo[i].Offset == Offset &&
139 BBInfo[i].KnownBits == KnownBits)
140 break;
141
142 BBInfo[i].Offset = Offset;
143 BBInfo[i].KnownBits = KnownBits;
144 }
145}
146
147} // end namespace llvm
MachineBasicBlock & MBB
#define LLVM_DEBUG(X)
Definition: Debug.h:101
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
GetInstSize - Returns the size of the specified MachineInstr.
bool isBBInRange(MachineInstr *MI, MachineBasicBlock *DestBB, unsigned MaxDisp) const
isBBInRange - Returns true if the distance between specific MI and specific BB can fit in MI's displa...
void adjustBBOffsetsAfter(MachineBasicBlock *MBB)
void computeBlockSize(MachineBasicBlock *MBB)
unsigned getOffsetOf(MachineInstr *MI) const
getOffsetOf - Return the current offset of the specified machine instruction from the start of the fu...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Align getAlignment() const
Return alignment of the basic block.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void ensureAlignment(Align A)
ensureAlignment - Make sure the function is at least A bytes aligned.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
Definition: MachineInstr.h:69
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:564
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
static bool mayOptimizeThumb2Instruction(const MachineInstr *MI)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
BasicBlockInfo - Information about the offset and size of a single basic block.
unsigned Size
Size - Size of the basic block in bytes.
Align PostAlign
PostAlign - When > 1, the block terminator contains a .align directive, so the end of the block is al...
uint8_t Unalign
Unalign - When non-zero, the block contains instructions (inline asm) of unknown size.