Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/ARM/ARMBasicBlockInfo.cpp
Line
Count
Source
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"
13
#include "llvm/CodeGen/MachineBasicBlock.h"
14
#include "llvm/CodeGen/MachineFunction.h"
15
#include "llvm/CodeGen/MachineInstr.h"
16
#include "llvm/CodeGen/TargetSubtargetInfo.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
28
816k
mayOptimizeThumb2Instruction(const MachineInstr *MI) {
29
816k
  switch(MI->getOpcode()) {
30
816k
    // optimizeThumb2Instructions.
31
816k
    case ARM::t2LEApcrel:
32
56.2k
    case ARM::t2LDRpci:
33
56.2k
    // optimizeThumb2Branches.
34
56.2k
    case ARM::t2B:
35
56.2k
    case ARM::t2Bcc:
36
56.2k
    case ARM::tBcc:
37
56.2k
    // optimizeThumb2JumpTables.
38
56.2k
    case ARM::t2BR_JT:
39
56.2k
    case ARM::tBR_JTr:
40
56.2k
      return true;
41
760k
  }
42
760k
  return false;
43
760k
}
44
45
107k
void ARMBasicBlockUtils::computeBlockSize(MachineBasicBlock *MBB) {
46
107k
  LLVM_DEBUG(dbgs() << "computeBlockSize: " << MBB->getName() << "\n");
47
107k
  BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
48
107k
  BBI.Size = 0;
49
107k
  BBI.Unalign = 0;
50
107k
  BBI.PostAlign = 0;
51
107k
52
930k
  for (MachineInstr &I : *MBB) {
53
930k
    BBI.Size += TII->getInstSizeInBytes(I);
54
930k
    // For inline asm, getInstSizeInBytes returns a conservative estimate.
55
930k
    // The actual size may be smaller, but still a multiple of the instr size.
56
930k
    if (I.isInlineAsm())
57
21.2k
      BBI.Unalign = isThumb ? 
120.9k
:
2256
;
58
908k
    // Also consider instructions that may be shrunk later.
59
908k
    else if (isThumb && 
mayOptimizeThumb2Instruction(&I)816k
)
60
56.2k
      BBI.Unalign = 1;
61
930k
  }
62
107k
63
107k
  // tBR_JTr contains a .align 2 directive.
64
107k
  if (!MBB->empty() && 
MBB->back().getOpcode() == ARM::tBR_JTr107k
) {
65
24
    BBI.PostAlign = 2;
66
24
    MBB->getParent()->ensureAlignment(2);
67
24
  }
68
107k
}
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.
73
152k
unsigned ARMBasicBlockUtils::getOffsetOf(MachineInstr *MI) const {
74
152k
  const MachineBasicBlock *MBB = MI->getParent();
75
152k
76
152k
  // The offset is composed of two things: the sum of the sizes of all MBB's
77
152k
  // before this instruction's block, and the offset from the start of the block
78
152k
  // it is in.
79
152k
  unsigned Offset = BBInfo[MBB->getNumber()].Offset;
80
152k
81
152k
  // Sum instructions before MI in MBB.
82
1.80M
  for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != MI; 
++I1.64M
) {
83
1.64M
    assert(I != MBB->end() && "Didn't find MI in its own basic block?");
84
1.64M
    Offset += TII->getInstSizeInBytes(*I);
85
1.64M
  }
86
152k
  return Offset;
87
152k
}
88
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
110k
                                     unsigned MaxDisp) const {
94
110k
  unsigned PCAdj      = isThumb ? 
4109k
:
81.04k
;
95
110k
  unsigned BrOffset   = getOffsetOf(MI) + PCAdj;
96
110k
  unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
97
110k
98
110k
  LLVM_DEBUG(dbgs() << "Branch of destination " << printMBBReference(*DestBB)
99
110k
                    << " from " << printMBBReference(*MI->getParent())
100
110k
                    << " max delta=" << MaxDisp << " from " << getOffsetOf(MI)
101
110k
                    << " to " << DestOffset << " offset "
102
110k
                    << int(DestOffset - BrOffset) << "\t" << *MI);
103
110k
104
110k
  if (BrOffset <= DestOffset) {
105
87.8k
    // Branch before the Dest.
106
87.8k
    if (DestOffset-BrOffset <= MaxDisp)
107
84.7k
      return true;
108
22.9k
  } else {
109
22.9k
    if (BrOffset-DestOffset <= MaxDisp)
110
21.7k
      return true;
111
4.29k
  }
112
4.29k
  return false;
113
4.29k
}
114
115
89.9k
void ARMBasicBlockUtils::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
116
89.9k
  assert(BB->getParent() == &MF &&
117
89.9k
         "Basic block is not a child of the current function.\n");
118
89.9k
119
89.9k
  unsigned BBNum = BB->getNumber();
120
89.9k
  LLVM_DEBUG(dbgs() << "Adjust block:\n"
121
89.9k
             << " - name: " << BB->getName() << "\n"
122
89.9k
             << " - number: " << BB->getNumber() << "\n"
123
89.9k
             << " - function: " << MF.getName() << "\n"
124
89.9k
             << "   - blocks: " << MF.getNumBlockIDs() << "\n");
125
89.9k
126
1.15M
  for(unsigned i = BBNum + 1, e = MF.getNumBlockIDs(); i < e; 
++i1.06M
) {
127
1.06M
    // Get the offset and known bits at the end of the layout predecessor.
128
1.06M
    // Include the alignment of the current block.
129
1.06M
    unsigned LogAlign = MF.getBlockNumbered(i)->getAlignment();
130
1.06M
    unsigned Offset = BBInfo[i - 1].postOffset(LogAlign);
131
1.06M
    unsigned KnownBits = BBInfo[i - 1].postKnownBits(LogAlign);
132
1.06M
133
1.06M
    // This is where block i begins.  Stop if the offset is already correct,
134
1.06M
    // and we have updated 2 blocks.  This is the maximum number of blocks
135
1.06M
    // changed before calling this function.
136
1.06M
    if (i > BBNum + 2 &&
137
1.06M
        
BBInfo[i].Offset == Offset923k
&&
138
1.06M
        
BBInfo[i].KnownBits == KnownBits1
)
139
1
      break;
140
1.06M
141
1.06M
    BBInfo[i].Offset = Offset;
142
1.06M
    BBInfo[i].KnownBits = KnownBits;
143
1.06M
  }
144
89.9k
}
145
146
} // end namespace llvm