1 //===--- ARMBasicBlockInfo.cpp - Utilities for block sizes ---------------===//
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 //===----------------------------------------------------------------------===//
10 #include "ARMBaseInstrInfo.h"
11 #include "ARMBasicBlockInfo.h"
12 #include "ARMMachineFunctionInfo.h"
13 #include "llvm/CodeGen/MachineBasicBlock.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/TargetSubtargetInfo.h"
19 #define DEBUG_TYPE "arm-bb-utils"
25 // mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
26 // below may shrink MI.
28 mayOptimizeThumb2Instruction(const MachineInstr
*MI
) {
29 switch(MI
->getOpcode()) {
30 // optimizeThumb2Instructions.
33 // optimizeThumb2Branches.
37 // optimizeThumb2JumpTables.
45 void ARMBasicBlockUtils::computeBlockSize(MachineBasicBlock
*MBB
) {
46 LLVM_DEBUG(dbgs() << "computeBlockSize: " << MBB
->getName() << "\n");
47 BasicBlockInfo
&BBI
= BBInfo
[MBB
->getNumber()];
52 for (MachineInstr
&I
: *MBB
) {
53 BBI
.Size
+= TII
->getInstSizeInBytes(I
);
54 // For inline asm, getInstSizeInBytes returns a conservative estimate.
55 // The actual size may be smaller, but still a multiple of the instr size.
57 BBI
.Unalign
= isThumb
? 1 : 2;
58 // Also consider instructions that may be shrunk later.
59 else if (isThumb
&& mayOptimizeThumb2Instruction(&I
))
63 // tBR_JTr contains a .align 2 directive.
64 if (!MBB
->empty() && MBB
->back().getOpcode() == ARM::tBR_JTr
) {
66 MBB
->getParent()->ensureAlignment(2);
70 /// getOffsetOf - Return the current offset of the specified machine instruction
71 /// from the start of the function. This offset changes as stuff is moved
72 /// around inside the function.
73 unsigned ARMBasicBlockUtils::getOffsetOf(MachineInstr
*MI
) const {
74 const MachineBasicBlock
*MBB
= MI
->getParent();
76 // The offset is composed of two things: the sum of the sizes of all MBB's
77 // before this instruction's block, and the offset from the start of the block
79 unsigned Offset
= BBInfo
[MBB
->getNumber()].Offset
;
81 // Sum instructions before MI in MBB.
82 for (MachineBasicBlock::const_iterator I
= MBB
->begin(); &*I
!= MI
; ++I
) {
83 assert(I
!= MBB
->end() && "Didn't find MI in its own basic block?");
84 Offset
+= TII
->getInstSizeInBytes(*I
);
89 /// isBBInRange - Returns true if the distance between specific MI and
90 /// specific BB can fit in MI's displacement field.
91 bool ARMBasicBlockUtils::isBBInRange(MachineInstr
*MI
,
92 MachineBasicBlock
*DestBB
,
93 unsigned MaxDisp
) const {
94 unsigned PCAdj
= isThumb
? 4 : 8;
95 unsigned BrOffset
= getOffsetOf(MI
) + PCAdj
;
96 unsigned DestOffset
= BBInfo
[DestBB
->getNumber()].Offset
;
98 LLVM_DEBUG(dbgs() << "Branch of destination " << printMBBReference(*DestBB
)
99 << " from " << printMBBReference(*MI
->getParent())
100 << " max delta=" << MaxDisp
<< " from " << getOffsetOf(MI
)
101 << " to " << DestOffset
<< " offset "
102 << int(DestOffset
- BrOffset
) << "\t" << *MI
);
104 if (BrOffset
<= DestOffset
) {
105 // Branch before the Dest.
106 if (DestOffset
-BrOffset
<= MaxDisp
)
109 if (BrOffset
-DestOffset
<= MaxDisp
)
115 void ARMBasicBlockUtils::adjustBBOffsetsAfter(MachineBasicBlock
*BB
) {
116 assert(BB
->getParent() == &MF
&&
117 "Basic block is not a child of the current function.\n");
119 unsigned BBNum
= BB
->getNumber();
120 LLVM_DEBUG(dbgs() << "Adjust block:\n"
121 << " - name: " << BB
->getName() << "\n"
122 << " - number: " << BB
->getNumber() << "\n"
123 << " - function: " << MF
.getName() << "\n"
124 << " - blocks: " << MF
.getNumBlockIDs() << "\n");
126 for(unsigned i
= BBNum
+ 1, e
= MF
.getNumBlockIDs(); i
< e
; ++i
) {
127 // Get the offset and known bits at the end of the layout predecessor.
128 // Include the alignment of the current block.
129 unsigned LogAlign
= MF
.getBlockNumbered(i
)->getAlignment();
130 unsigned Offset
= BBInfo
[i
- 1].postOffset(LogAlign
);
131 unsigned KnownBits
= BBInfo
[i
- 1].postKnownBits(LogAlign
);
133 // This is where block i begins. Stop if the offset is already correct,
134 // and we have updated 2 blocks. This is the maximum number of blocks
135 // changed before calling this function.
137 BBInfo
[i
].Offset
== Offset
&&
138 BBInfo
[i
].KnownBits
== KnownBits
)
141 BBInfo
[i
].Offset
= Offset
;
142 BBInfo
[i
].KnownBits
= KnownBits
;
146 } // end namespace llvm