LLVM  10.0.0svn
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 "ARM.h"
10 #include "ARMBaseInstrInfo.h"
11 #include "ARMBasicBlockInfo.h"
12 #include "ARMMachineFunctionInfo.h"
17 #include <vector>
18 
19 #define DEBUG_TYPE "arm-bb-utils"
20 
21 using namespace llvm;
22 
23 namespace llvm {
24 
25 // mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
26 // below may shrink MI.
27 static bool
29  switch(MI->getOpcode()) {
30  // optimizeThumb2Instructions.
31  case ARM::t2LEApcrel:
32  case ARM::t2LDRpci:
33  // optimizeThumb2Branches.
34  case ARM::t2B:
35  case ARM::t2Bcc:
36  case ARM::tBcc:
37  // optimizeThumb2JumpTables.
38  case ARM::t2BR_JT:
39  case ARM::tBR_JTr:
40  return true;
41  }
42  return false;
43 }
44 
46  LLVM_DEBUG(dbgs() << "computeBlockSize: " << MBB->getName() << "\n");
47  BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
48  BBI.Size = 0;
49  BBI.Unalign = 0;
50  BBI.PostAlign = llvm::Align::None();
51 
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.
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 = llvm::Align(4);
66  MBB->getParent()->ensureAlignment(llvm::Align(4));
67  }
68 }
69 
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.
74  const MachineBasicBlock *MBB = MI->getParent();
75 
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
78  // it is in.
79  unsigned Offset = BBInfo[MBB->getNumber()].Offset;
80 
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);
85  }
86  return Offset;
87 }
88 
89 /// isBBInRange - Returns true if the distance between specific MI and
90 /// specific BB can fit in MI's displacement field.
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;
97 
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);
103 
104  if (BrOffset <= DestOffset) {
105  // Branch before the Dest.
106  if (DestOffset-BrOffset <= MaxDisp)
107  return true;
108  } else {
109  if (BrOffset-DestOffset <= MaxDisp)
110  return true;
111  }
112  return false;
113 }
114 
116  assert(BB->getParent() == &MF &&
117  "Basic block is not a child of the current function.\n");
118 
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");
125 
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.
130  const unsigned Offset = BBInfo[i - 1].postOffset(Align);
131  const unsigned KnownBits = BBInfo[i - 1].postKnownBits(Align);
132 
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.
136  if (i > BBNum + 2 &&
137  BBInfo[i].Offset == Offset &&
138  BBInfo[i].KnownBits == KnownBits)
139  break;
140 
141  BBInfo[i].Offset = Offset;
142  BBInfo[i].KnownBits = KnownBits;
143  }
144 }
145 
146 } // end namespace llvm
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID&#39;s allocated.
void adjustBBOffsetsAfter(MachineBasicBlock *MBB)
BasicBlockInfo - Information about the offset and size of a single basic block.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they&#39;re not in a MachineFuncti...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
static bool mayOptimizeThumb2Instruction(const MachineInstr *MI)
unsigned getOffsetOf(MachineInstr *MI) const
getOffsetOf - Return the current offset of the specified machine instruction from the start of the fu...
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
GetInstSize - Returns the size of the specified MachineInstr.
static constexpr const Align None()
Returns a default constructed Align which corresponds to no alignment.
Definition: Alignment.h:82
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
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&#39;s displa...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
llvm::Align getAlignment() const
Return alignment of the basic block.
void computeBlockSize(MachineBasicBlock *MBB)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:256
Representation of each machine instruction.
Definition: MachineInstr.h:64
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
#define LLVM_DEBUG(X)
Definition: Debug.h:122