1 //===--- ARMComputeBlockSize.cpp - Compute machine 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"
23 // mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
24 // below may shrink MI.
26 mayOptimizeThumb2Instruction(const MachineInstr
*MI
) {
27 switch(MI
->getOpcode()) {
28 // optimizeThumb2Instructions.
31 // optimizeThumb2Branches.
35 // optimizeThumb2JumpTables.
43 void computeBlockSize(MachineFunction
*MF
, MachineBasicBlock
*MBB
,
44 BasicBlockInfo
&BBI
) {
45 const ARMBaseInstrInfo
*TII
=
46 static_cast<const ARMBaseInstrInfo
*>(MF
->getSubtarget().getInstrInfo());
47 bool isThumb
= MF
->getInfo
<ARMFunctionInfo
>()->isThumbFunction();
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 std::vector
<BasicBlockInfo
> computeAllBlockSizes(MachineFunction
*MF
) {
71 std::vector
<BasicBlockInfo
> BBInfo
;
72 BBInfo
.resize(MF
->getNumBlockIDs());
74 for (MachineBasicBlock
&MBB
: *MF
)
75 computeBlockSize(MF
, &MBB
, BBInfo
[MBB
.getNumber()]);
80 } // end namespace llvm