Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/PowerPC/PPCBranchSelector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
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
// This file contains a pass that scans a machine function to determine which
10
// conditional branches need more than 16 bits of displacement to reach their
11
// target basic block.  It does this in two passes; a calculation of basic block
12
// positions pass, and a branch pseudo op to machine branch opcode pass.  This
13
// pass should be run last, just before the assembly printer.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "MCTargetDesc/PPCPredicates.h"
18
#include "PPC.h"
19
#include "PPCInstrBuilder.h"
20
#include "PPCInstrInfo.h"
21
#include "PPCSubtarget.h"
22
#include "llvm/ADT/Statistic.h"
23
#include "llvm/CodeGen/MachineFunctionPass.h"
24
#include "llvm/CodeGen/MachineRegisterInfo.h"
25
#include "llvm/CodeGen/TargetSubtargetInfo.h"
26
#include "llvm/Support/MathExtras.h"
27
#include "llvm/Target/TargetMachine.h"
28
#include <algorithm>
29
using namespace llvm;
30
31
#define DEBUG_TYPE "ppc-branch-select"
32
33
STATISTIC(NumExpanded, "Number of branches expanded to long format");
34
35
namespace {
36
  struct PPCBSel : public MachineFunctionPass {
37
    static char ID;
38
1.80k
    PPCBSel() : MachineFunctionPass(ID) {
39
1.80k
      initializePPCBSelPass(*PassRegistry::getPassRegistry());
40
1.80k
    }
41
42
    // The sizes of the basic blocks in the function (the first
43
    // element of the pair); the second element of the pair is the amount of the
44
    // size that is due to potential padding.
45
    std::vector<std::pair<unsigned, unsigned>> BlockSizes;
46
47
    // The first block number which has imprecise instruction address.
48
    int FirstImpreciseBlock = -1;
49
50
    unsigned GetAlignmentAdjustment(MachineBasicBlock &MBB, unsigned Offset);
51
    unsigned ComputeBlockSizes(MachineFunction &Fn);
52
    void modifyAdjustment(MachineFunction &Fn);
53
    int computeBranchSize(MachineFunction &Fn,
54
                          const MachineBasicBlock *Src,
55
                          const MachineBasicBlock *Dest,
56
                          unsigned BrOffset);
57
58
    bool runOnMachineFunction(MachineFunction &Fn) override;
59
60
1.76k
    MachineFunctionProperties getRequiredProperties() const override {
61
1.76k
      return MachineFunctionProperties().set(
62
1.76k
          MachineFunctionProperties::Property::NoVRegs);
63
1.76k
    }
64
65
13.0k
    StringRef getPassName() const override { return "PowerPC Branch Selector"; }
66
  };
67
  char PPCBSel::ID = 0;
68
}
69
70
INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
71
                false, false)
72
73
/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
74
/// Pass
75
///
76
1.80k
FunctionPass *llvm::createPPCBranchSelectionPass() {
77
1.80k
  return new PPCBSel();
78
1.80k
}
79
80
/// In order to make MBB aligned, we need to add an adjustment value to the
81
/// original Offset.
82
unsigned PPCBSel::GetAlignmentAdjustment(MachineBasicBlock &MBB,
83
5.50k
                                         unsigned Offset) {
84
5.50k
  unsigned Align = MBB.getAlignment();
85
5.50k
  if (!Align)
86
5.18k
    return 0;
87
323
88
323
  unsigned AlignAmt = 1 << Align;
89
323
  unsigned ParentAlign = MBB.getParent()->getAlignment();
90
323
91
323
  if (Align <= ParentAlign)
92
226
    return OffsetToAlignment(Offset, AlignAmt);
93
97
94
97
  // The alignment of this MBB is larger than the function's alignment, so we
95
97
  // can't tell whether or not it will insert nops. Assume that it will.
96
97
  if (FirstImpreciseBlock < 0)
97
77
    FirstImpreciseBlock = MBB.getNumber();
98
97
  return AlignAmt + OffsetToAlignment(Offset, AlignAmt);
99
97
}
100
101
/// We need to be careful about the offset of the first block in the function
102
/// because it might not have the function's alignment. This happens because,
103
/// under the ELFv2 ABI, for functions which require a TOC pointer, we add a
104
/// two-instruction sequence to the start of the function.
105
/// Note: This needs to be synchronized with the check in
106
/// PPCLinuxAsmPrinter::EmitFunctionBodyStart.
107
11.2k
static inline unsigned GetInitialOffset(MachineFunction &Fn) {
108
11.2k
  unsigned InitialOffset = 0;
109
11.2k
  if (Fn.getSubtarget<PPCSubtarget>().isELFv2ABI() &&
110
11.2k
      
!Fn.getRegInfo().use_empty(PPC::X2)5.31k
)
111
1.54k
    InitialOffset = 8;
112
11.2k
  return InitialOffset;
113
11.2k
}
114
115
/// Measure each MBB and compute a size for the entire function.
116
11.2k
unsigned PPCBSel::ComputeBlockSizes(MachineFunction &Fn) {
117
11.2k
  const PPCInstrInfo *TII =
118
11.2k
      static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
119
11.2k
  unsigned FuncSize = GetInitialOffset(Fn);
120
11.2k
121
28.0k
  for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
122
16.7k
       ++MFI) {
123
16.7k
    MachineBasicBlock *MBB = &*MFI;
124
16.7k
125
16.7k
    // The end of the previous block may have extra nops if this block has an
126
16.7k
    // alignment requirement.
127
16.7k
    if (MBB->getNumber() > 0) {
128
5.50k
      unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize);
129
5.50k
130
5.50k
      auto &BS = BlockSizes[MBB->getNumber()-1];
131
5.50k
      BS.first += AlignExtra;
132
5.50k
      BS.second = AlignExtra;
133
5.50k
134
5.50k
      FuncSize += AlignExtra;
135
5.50k
    }
136
16.7k
137
16.7k
    unsigned BlockSize = 0;
138
103k
    for (MachineInstr &MI : *MBB) {
139
103k
      BlockSize += TII->getInstSizeInBytes(MI);
140
103k
      if (MI.isInlineAsm() && 
(FirstImpreciseBlock < 0)327
)
141
234
        FirstImpreciseBlock = MBB->getNumber();
142
103k
    }
143
16.7k
144
16.7k
    BlockSizes[MBB->getNumber()].first = BlockSize;
145
16.7k
    FuncSize += BlockSize;
146
16.7k
  }
147
11.2k
148
11.2k
  return FuncSize;
149
11.2k
}
150
151
/// Modify the basic block align adjustment.
152
1
void PPCBSel::modifyAdjustment(MachineFunction &Fn) {
153
1
  unsigned Offset = GetInitialOffset(Fn);
154
6
  for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
155
5
       ++MFI) {
156
5
    MachineBasicBlock *MBB = &*MFI;
157
5
158
5
    if (MBB->getNumber() > 0) {
159
4
      auto &BS = BlockSizes[MBB->getNumber()-1];
160
4
      BS.first -= BS.second;
161
4
      Offset -= BS.second;
162
4
163
4
      unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset);
164
4
165
4
      BS.first += AlignExtra;
166
4
      BS.second = AlignExtra;
167
4
168
4
      Offset += AlignExtra;
169
4
    }
170
5
171
5
    Offset += BlockSizes[MBB->getNumber()].first;
172
5
  }
173
1
}
174
175
/// Determine the offset from the branch in Src block to the Dest block.
176
/// BrOffset is the offset of the branch instruction inside Src block.
177
int PPCBSel::computeBranchSize(MachineFunction &Fn,
178
                               const MachineBasicBlock *Src,
179
                               const MachineBasicBlock *Dest,
180
3
                               unsigned BrOffset) {
181
3
  int BranchSize;
182
3
  unsigned MaxAlign = 2;
183
3
  bool NeedExtraAdjustment = false;
184
3
  if (Dest->getNumber() <= Src->getNumber()) {
185
2
    // If this is a backwards branch, the delta is the offset from the
186
2
    // start of this block to this branch, plus the sizes of all blocks
187
2
    // from this block to the dest.
188
2
    BranchSize = BrOffset;
189
2
    MaxAlign = std::max(MaxAlign, Src->getAlignment());
190
2
191
2
    int DestBlock = Dest->getNumber();
192
2
    BranchSize += BlockSizes[DestBlock].first;
193
2
    for (unsigned i = DestBlock+1, e = Src->getNumber(); i < e; 
++i0
) {
194
0
      BranchSize += BlockSizes[i].first;
195
0
      MaxAlign = std::max(MaxAlign,
196
0
                          Fn.getBlockNumbered(i)->getAlignment());
197
0
    }
198
2
199
2
    NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
200
2
                          (DestBlock >= FirstImpreciseBlock);
201
2
  } else {
202
1
    // Otherwise, add the size of the blocks between this block and the
203
1
    // dest to the number of bytes left in this block.
204
1
    unsigned StartBlock = Src->getNumber();
205
1
    BranchSize = BlockSizes[StartBlock].first - BrOffset;
206
1
207
1
    MaxAlign = std::max(MaxAlign, Dest->getAlignment());
208
4
    for (unsigned i = StartBlock+1, e = Dest->getNumber(); i != e; 
++i3
) {
209
3
      BranchSize += BlockSizes[i].first;
210
3
      MaxAlign = std::max(MaxAlign,
211
3
                          Fn.getBlockNumbered(i)->getAlignment());
212
3
    }
213
1
214
1
    NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
215
1
                          (Src->getNumber() >= FirstImpreciseBlock);
216
1
  }
217
3
218
3
  // We tend to over estimate code size due to large alignment and
219
3
  // inline assembly. Usually it causes larger computed branch offset.
220
3
  // But sometimes it may also causes smaller computed branch offset
221
3
  // than actual branch offset. If the offset is close to the limit of
222
3
  // encoding, it may cause problem at run time.
223
3
  // Following is a simplified example.
224
3
  //
225
3
  //              actual        estimated
226
3
  //              address        address
227
3
  //    ...
228
3
  //   bne Far      100            10c
229
3
  //   .p2align 4
230
3
  //   Near:        110            110
231
3
  //    ...
232
3
  //   Far:        8108           8108
233
3
  //
234
3
  //   Actual offset:    0x8108 - 0x100 = 0x8008
235
3
  //   Computed offset:  0x8108 - 0x10c = 0x7ffc
236
3
  //
237
3
  // This example also shows when we can get the largest gap between
238
3
  // estimated offset and actual offset. If there is an aligned block
239
3
  // ABB between branch and target, assume its alignment is <align>
240
3
  // bits. Now consider the accumulated function size FSIZE till the end
241
3
  // of previous block PBB. If the estimated FSIZE is multiple of
242
3
  // 2^<align>, we don't need any padding for the estimated address of
243
3
  // ABB. If actual FSIZE at the end of PBB is 4 bytes more than
244
3
  // multiple of 2^<align>, then we need (2^<align> - 4) bytes of
245
3
  // padding. It also means the actual branch offset is (2^<align> - 4)
246
3
  // larger than computed offset. Other actual FSIZE needs less padding
247
3
  // bytes, so causes smaller gap between actual and computed offset.
248
3
  //
249
3
  // On the other hand, if the inline asm or large alignment occurs
250
3
  // between the branch block and destination block, the estimated address
251
3
  // can be <delta> larger than actual address. If padding bytes are
252
3
  // needed for a later aligned block, the actual number of padding bytes
253
3
  // is at most <delta> more than estimated padding bytes. So the actual
254
3
  // aligned block address is less than or equal to the estimated aligned
255
3
  // block address. So the actual branch offset is less than or equal to
256
3
  // computed branch offset.
257
3
  //
258
3
  // The computed offset is at most ((1 << alignment) - 4) bytes smaller
259
3
  // than actual offset. So we add this number to the offset for safety.
260
3
  if (NeedExtraAdjustment)
261
3
    BranchSize += (1 << MaxAlign) - 4;
262
3
263
3
  return BranchSize;
264
3
}
265
266
11.2k
bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
267
11.2k
  const PPCInstrInfo *TII =
268
11.2k
      static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
269
11.2k
  // Give the blocks of the function a dense, in-order, numbering.
270
11.2k
  Fn.RenumberBlocks();
271
11.2k
  BlockSizes.resize(Fn.getNumBlockIDs());
272
11.2k
  FirstImpreciseBlock = -1;
273
11.2k
274
11.2k
  // Measure each MBB and compute a size for the entire function.
275
11.2k
  unsigned FuncSize = ComputeBlockSizes(Fn);
276
11.2k
277
11.2k
  // If the entire function is smaller than the displacement of a branch field,
278
11.2k
  // we know we don't need to shrink any branches in this function.  This is a
279
11.2k
  // common case.
280
11.2k
  if (FuncSize < (1 << 15)) {
281
11.2k
    BlockSizes.clear();
282
11.2k
    return false;
283
11.2k
  }
284
1
285
1
  // For each conditional branch, if the offset to its destination is larger
286
1
  // than the offset field allows, transform it into a long branch sequence
287
1
  // like this:
288
1
  //   short branch:
289
1
  //     bCC MBB
290
1
  //   long branch:
291
1
  //     b!CC $PC+8
292
1
  //     b MBB
293
1
  //
294
1
  bool MadeChange = true;
295
1
  bool EverMadeChange = false;
296
3
  while (MadeChange) {
297
2
    // Iteratively expand branches until we reach a fixed point.
298
2
    MadeChange = false;
299
2
300
12
    for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
301
10
         ++MFI) {
302
10
      MachineBasicBlock &MBB = *MFI;
303
10
      unsigned MBBStartOffset = 0;
304
10
      for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
305
29
           I != E; 
++I19
) {
306
19
        MachineBasicBlock *Dest = nullptr;
307
19
        if (I->getOpcode() == PPC::BCC && 
!I->getOperand(2).isImm()0
)
308
0
          Dest = I->getOperand(2).getMBB();
309
19
        else if ((I->getOpcode() == PPC::BC || 
I->getOpcode() == PPC::BCn18
) &&
310
19
                 
!I->getOperand(1).isImm()2
)
311
1
          Dest = I->getOperand(1).getMBB();
312
18
        else if ((I->getOpcode() == PPC::BDNZ8 || 
I->getOpcode() == PPC::BDNZ16
||
313
18
                  
I->getOpcode() == PPC::BDZ816
||
I->getOpcode() == PPC::BDZ16
) &&
314
18
                 
!I->getOperand(0).isImm()2
)
315
2
          Dest = I->getOperand(0).getMBB();
316
19
317
19
        if (!Dest) {
318
16
          MBBStartOffset += TII->getInstSizeInBytes(*I);
319
16
          continue;
320
16
        }
321
3
322
3
        // Determine the offset from the current branch to the destination
323
3
        // block.
324
3
        int BranchSize = computeBranchSize(Fn, &MBB, Dest, MBBStartOffset);
325
3
326
3
        // If this branch is in range, ignore it.
327
3
        if (isInt<16>(BranchSize)) {
328
2
          MBBStartOffset += 4;
329
2
          continue;
330
2
        }
331
1
332
1
        // Otherwise, we have to expand it to a long branch.
333
1
        MachineInstr &OldBranch = *I;
334
1
        DebugLoc dl = OldBranch.getDebugLoc();
335
1
336
1
        if (I->getOpcode() == PPC::BCC) {
337
0
          // The BCC operands are:
338
0
          // 0. PPC branch predicate
339
0
          // 1. CR register
340
0
          // 2. Target MBB
341
0
          PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
342
0
          unsigned CRReg = I->getOperand(1).getReg();
343
0
344
0
          // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
345
0
          BuildMI(MBB, I, dl, TII->get(PPC::BCC))
346
0
            .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
347
1
        } else if (I->getOpcode() == PPC::BC) {
348
0
          unsigned CRBit = I->getOperand(0).getReg();
349
0
          BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
350
1
        } else if (I->getOpcode() == PPC::BCn) {
351
1
          unsigned CRBit = I->getOperand(0).getReg();
352
1
          BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
353
1
        } else 
if (0
I->getOpcode() == PPC::BDNZ0
) {
354
0
          BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
355
0
        } else if (I->getOpcode() == PPC::BDNZ8) {
356
0
          BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
357
0
        } else if (I->getOpcode() == PPC::BDZ) {
358
0
          BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
359
0
        } else if (I->getOpcode() == PPC::BDZ8) {
360
0
          BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
361
0
        } else {
362
0
           llvm_unreachable("Unhandled branch type!");
363
0
        }
364
1
365
1
        // Uncond branch to the real destination.
366
1
        I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
367
1
368
1
        // Remove the old branch from the function.
369
1
        OldBranch.eraseFromParent();
370
1
371
1
        // Remember that this instruction is 8-bytes, increase the size of the
372
1
        // block by 4, remember to iterate.
373
1
        BlockSizes[MBB.getNumber()].first += 4;
374
1
        MBBStartOffset += 8;
375
1
        ++NumExpanded;
376
1
        MadeChange = true;
377
1
      }
378
10
    }
379
2
380
2
    if (MadeChange) {
381
1
      // If we're going to iterate again, make sure we've updated our
382
1
      // padding-based contributions to the block sizes.
383
1
      modifyAdjustment(Fn);
384
1
    }
385
2
386
2
    EverMadeChange |= MadeChange;
387
2
  }
388
1
389
1
  BlockSizes.clear();
390
1
  return true;
391
1
}