blob: 828fc6568380c4c717a942a5a9782d8808edc16d [file] [log] [blame]
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +00001//===--- ARMComputeBlockSize.cpp - Compute machine block sizes ------------===//
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#include "ARM.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000010#include "ARMBaseInstrInfo.h"
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000011#include "ARMBasicBlockInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000012#include "ARMMachineFunctionInfo.h"
13#include "llvm/CodeGen/MachineBasicBlock.h"
14#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/CodeGen/MachineInstr.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000016#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000017#include <vector>
18
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000019using namespace llvm;
20
21namespace llvm {
22
23// mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
24// below may shrink MI.
25static bool
26mayOptimizeThumb2Instruction(const MachineInstr *MI) {
27 switch(MI->getOpcode()) {
28 // optimizeThumb2Instructions.
29 case ARM::t2LEApcrel:
30 case ARM::t2LDRpci:
31 // optimizeThumb2Branches.
32 case ARM::t2B:
33 case ARM::t2Bcc:
34 case ARM::tBcc:
35 // optimizeThumb2JumpTables.
36 case ARM::t2BR_JT:
Ana Pazos41573802018-03-23 17:53:27 +000037 case ARM::tBR_JTr:
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000038 return true;
39 }
40 return false;
41}
42
43void 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();
48 BBI.Size = 0;
49 BBI.Unalign = 0;
50 BBI.PostAlign = 0;
51
52 for (MachineInstr &I : *MBB) {
Sjoerd Meijer89217f82016-07-28 16:32:22 +000053 BBI.Size += TII->getInstSizeInBytes(I);
54 // For inline asm, getInstSizeInBytes returns a conservative estimate.
Sjoerd Meijer5c0ef832016-07-22 08:39:12 +000055 // The actual size may be smaller, but still a multiple of the instr size.
56 if (I.isInlineAsm())
57 BBI.Unalign = isThumb ? 1 : 2;
58 // Also consider instructions that may be shrunk later.
59 else if (isThumb && mayOptimizeThumb2Instruction(&I))
60 BBI.Unalign = 1;
61 }
62
63 // tBR_JTr contains a .align 2 directive.
64 if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) {
65 BBI.PostAlign = 2;
66 MBB->getParent()->ensureAlignment(2);
67 }
68}
69
70std::vector<BasicBlockInfo> computeAllBlockSizes(MachineFunction *MF) {
71 std::vector<BasicBlockInfo> BBInfo;
72 BBInfo.resize(MF->getNumBlockIDs());
73
74 for (MachineBasicBlock &MBB : *MF)
75 computeBlockSize(MF, &MBB, BBInfo[MBB.getNumber()]);
76
77 return BBInfo;
78}
79
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000080} // end namespace llvm