xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCBranchSelector.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
10b57cec5SDimitry Andric //===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a pass that scans a machine function to determine which
100b57cec5SDimitry Andric // conditional branches need more than 16 bits of displacement to reach their
110b57cec5SDimitry Andric // target basic block.  It does this in two passes; a calculation of basic block
120b57cec5SDimitry Andric // positions pass, and a branch pseudo op to machine branch opcode pass.  This
130b57cec5SDimitry Andric // pass should be run last, just before the assembly printer.
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "MCTargetDesc/PPCPredicates.h"
180b57cec5SDimitry Andric #include "PPC.h"
190b57cec5SDimitry Andric #include "PPCInstrBuilder.h"
200b57cec5SDimitry Andric #include "PPCInstrInfo.h"
210b57cec5SDimitry Andric #include "PPCSubtarget.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
260b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
270b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
280b57cec5SDimitry Andric #include <algorithm>
290b57cec5SDimitry Andric using namespace llvm;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-branch-select"
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric STATISTIC(NumExpanded, "Number of branches expanded to long format");
345ffd83dbSDimitry Andric STATISTIC(NumPrefixed, "Number of prefixed instructions");
355ffd83dbSDimitry Andric STATISTIC(NumPrefixedAligned,
365ffd83dbSDimitry Andric           "Number of prefixed instructions that have been aligned");
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric namespace {
390b57cec5SDimitry Andric   struct PPCBSel : public MachineFunctionPass {
400b57cec5SDimitry Andric     static char ID;
PPCBSel__anon70e7ac190111::PPCBSel410b57cec5SDimitry Andric     PPCBSel() : MachineFunctionPass(ID) {
420b57cec5SDimitry Andric       initializePPCBSelPass(*PassRegistry::getPassRegistry());
430b57cec5SDimitry Andric     }
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric     // The sizes of the basic blocks in the function (the first
460b57cec5SDimitry Andric     // element of the pair); the second element of the pair is the amount of the
470b57cec5SDimitry Andric     // size that is due to potential padding.
480b57cec5SDimitry Andric     std::vector<std::pair<unsigned, unsigned>> BlockSizes;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric     // The first block number which has imprecise instruction address.
510b57cec5SDimitry Andric     int FirstImpreciseBlock = -1;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric     unsigned GetAlignmentAdjustment(MachineBasicBlock &MBB, unsigned Offset);
540b57cec5SDimitry Andric     unsigned ComputeBlockSizes(MachineFunction &Fn);
550b57cec5SDimitry Andric     void modifyAdjustment(MachineFunction &Fn);
560b57cec5SDimitry Andric     int computeBranchSize(MachineFunction &Fn,
570b57cec5SDimitry Andric                           const MachineBasicBlock *Src,
580b57cec5SDimitry Andric                           const MachineBasicBlock *Dest,
590b57cec5SDimitry Andric                           unsigned BrOffset);
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &Fn) override;
620b57cec5SDimitry Andric 
getRequiredProperties__anon70e7ac190111::PPCBSel630b57cec5SDimitry Andric     MachineFunctionProperties getRequiredProperties() const override {
640b57cec5SDimitry Andric       return MachineFunctionProperties().set(
650b57cec5SDimitry Andric           MachineFunctionProperties::Property::NoVRegs);
660b57cec5SDimitry Andric     }
670b57cec5SDimitry Andric 
getPassName__anon70e7ac190111::PPCBSel680b57cec5SDimitry Andric     StringRef getPassName() const override { return "PowerPC Branch Selector"; }
690b57cec5SDimitry Andric   };
700b57cec5SDimitry Andric   char PPCBSel::ID = 0;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
740b57cec5SDimitry Andric                 false, false)
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
770b57cec5SDimitry Andric /// Pass
780b57cec5SDimitry Andric ///
createPPCBranchSelectionPass()790b57cec5SDimitry Andric FunctionPass *llvm::createPPCBranchSelectionPass() {
800b57cec5SDimitry Andric   return new PPCBSel();
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /// In order to make MBB aligned, we need to add an adjustment value to the
840b57cec5SDimitry Andric /// original Offset.
GetAlignmentAdjustment(MachineBasicBlock & MBB,unsigned Offset)850b57cec5SDimitry Andric unsigned PPCBSel::GetAlignmentAdjustment(MachineBasicBlock &MBB,
860b57cec5SDimitry Andric                                          unsigned Offset) {
878bcb0991SDimitry Andric   const Align Alignment = MBB.getAlignment();
885ffd83dbSDimitry Andric   if (Alignment == Align(1))
890b57cec5SDimitry Andric     return 0;
900b57cec5SDimitry Andric 
918bcb0991SDimitry Andric   const Align ParentAlign = MBB.getParent()->getAlignment();
920b57cec5SDimitry Andric 
938bcb0991SDimitry Andric   if (Alignment <= ParentAlign)
948bcb0991SDimitry Andric     return offsetToAlignment(Offset, Alignment);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   // The alignment of this MBB is larger than the function's alignment, so we
970b57cec5SDimitry Andric   // can't tell whether or not it will insert nops. Assume that it will.
980b57cec5SDimitry Andric   if (FirstImpreciseBlock < 0)
990b57cec5SDimitry Andric     FirstImpreciseBlock = MBB.getNumber();
1008bcb0991SDimitry Andric   return Alignment.value() + offsetToAlignment(Offset, Alignment);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric /// We need to be careful about the offset of the first block in the function
1040b57cec5SDimitry Andric /// because it might not have the function's alignment. This happens because,
1050b57cec5SDimitry Andric /// under the ELFv2 ABI, for functions which require a TOC pointer, we add a
1060b57cec5SDimitry Andric /// two-instruction sequence to the start of the function.
1070b57cec5SDimitry Andric /// Note: This needs to be synchronized with the check in
1080b57cec5SDimitry Andric /// PPCLinuxAsmPrinter::EmitFunctionBodyStart.
GetInitialOffset(MachineFunction & Fn)1090b57cec5SDimitry Andric static inline unsigned GetInitialOffset(MachineFunction &Fn) {
1100b57cec5SDimitry Andric   unsigned InitialOffset = 0;
1110b57cec5SDimitry Andric   if (Fn.getSubtarget<PPCSubtarget>().isELFv2ABI() &&
1120b57cec5SDimitry Andric       !Fn.getRegInfo().use_empty(PPC::X2))
1130b57cec5SDimitry Andric     InitialOffset = 8;
1140b57cec5SDimitry Andric   return InitialOffset;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric /// Measure each MBB and compute a size for the entire function.
ComputeBlockSizes(MachineFunction & Fn)1180b57cec5SDimitry Andric unsigned PPCBSel::ComputeBlockSizes(MachineFunction &Fn) {
1190b57cec5SDimitry Andric   const PPCInstrInfo *TII =
1200b57cec5SDimitry Andric       static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
1210b57cec5SDimitry Andric   unsigned FuncSize = GetInitialOffset(Fn);
1220b57cec5SDimitry Andric 
123*4824e7fdSDimitry Andric   for (MachineBasicBlock &MBB : Fn) {
1240b57cec5SDimitry Andric     // The end of the previous block may have extra nops if this block has an
1250b57cec5SDimitry Andric     // alignment requirement.
126*4824e7fdSDimitry Andric     if (MBB.getNumber() > 0) {
127*4824e7fdSDimitry Andric       unsigned AlignExtra = GetAlignmentAdjustment(MBB, FuncSize);
1280b57cec5SDimitry Andric 
129*4824e7fdSDimitry Andric       auto &BS = BlockSizes[MBB.getNumber()-1];
1300b57cec5SDimitry Andric       BS.first += AlignExtra;
1310b57cec5SDimitry Andric       BS.second = AlignExtra;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric       FuncSize += AlignExtra;
1340b57cec5SDimitry Andric     }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric     unsigned BlockSize = 0;
1375ffd83dbSDimitry Andric     unsigned UnalignedBytesRemaining = 0;
138*4824e7fdSDimitry Andric     for (MachineInstr &MI : MBB) {
1395ffd83dbSDimitry Andric       unsigned MINumBytes = TII->getInstSizeInBytes(MI);
1400b57cec5SDimitry Andric       if (MI.isInlineAsm() && (FirstImpreciseBlock < 0))
141*4824e7fdSDimitry Andric         FirstImpreciseBlock = MBB.getNumber();
1425ffd83dbSDimitry Andric       if (TII->isPrefixed(MI.getOpcode())) {
1435ffd83dbSDimitry Andric         NumPrefixed++;
1445ffd83dbSDimitry Andric 
1455ffd83dbSDimitry Andric         // All 8 byte instructions may require alignment. Each 8 byte
1465ffd83dbSDimitry Andric         // instruction may be aligned by another 4 bytes.
1475ffd83dbSDimitry Andric         // This means that an 8 byte instruction may require 12 bytes
1485ffd83dbSDimitry Andric         // (8 for the instruction itself and 4 for the alignment nop).
1495ffd83dbSDimitry Andric         // This will happen if an 8 byte instruction can be aligned to 64 bytes
1505ffd83dbSDimitry Andric         // by only adding a 4 byte nop.
1515ffd83dbSDimitry Andric         // We don't know the alignment at this point in the code so we have to
1525ffd83dbSDimitry Andric         // adopt a more pessimistic approach. If an instruction may need
1535ffd83dbSDimitry Andric         // alignment we assume that it does need alignment and add 4 bytes to
1545ffd83dbSDimitry Andric         // it. As a result we may end up with more long branches than before
1555ffd83dbSDimitry Andric         // but we are in the safe position where if we need a long branch we
1565ffd83dbSDimitry Andric         // have one.
1575ffd83dbSDimitry Andric         // The if statement checks to make sure that two 8 byte instructions
1585ffd83dbSDimitry Andric         // are at least 64 bytes away from each other. It is not possible for
1595ffd83dbSDimitry Andric         // two instructions that both need alignment to be within 64 bytes of
1605ffd83dbSDimitry Andric         // each other.
1615ffd83dbSDimitry Andric         if (!UnalignedBytesRemaining) {
1625ffd83dbSDimitry Andric           BlockSize += 4;
1635ffd83dbSDimitry Andric           UnalignedBytesRemaining = 60;
1645ffd83dbSDimitry Andric           NumPrefixedAligned++;
1655ffd83dbSDimitry Andric         }
1665ffd83dbSDimitry Andric       }
1675ffd83dbSDimitry Andric       UnalignedBytesRemaining -= std::min(UnalignedBytesRemaining, MINumBytes);
1685ffd83dbSDimitry Andric       BlockSize += MINumBytes;
1690b57cec5SDimitry Andric     }
1700b57cec5SDimitry Andric 
171*4824e7fdSDimitry Andric     BlockSizes[MBB.getNumber()].first = BlockSize;
1720b57cec5SDimitry Andric     FuncSize += BlockSize;
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   return FuncSize;
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric /// Modify the basic block align adjustment.
modifyAdjustment(MachineFunction & Fn)1790b57cec5SDimitry Andric void PPCBSel::modifyAdjustment(MachineFunction &Fn) {
1800b57cec5SDimitry Andric   unsigned Offset = GetInitialOffset(Fn);
181*4824e7fdSDimitry Andric   for (MachineBasicBlock &MBB : Fn) {
182*4824e7fdSDimitry Andric     if (MBB.getNumber() > 0) {
183*4824e7fdSDimitry Andric       auto &BS = BlockSizes[MBB.getNumber()-1];
1840b57cec5SDimitry Andric       BS.first -= BS.second;
1850b57cec5SDimitry Andric       Offset -= BS.second;
1860b57cec5SDimitry Andric 
187*4824e7fdSDimitry Andric       unsigned AlignExtra = GetAlignmentAdjustment(MBB, Offset);
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric       BS.first += AlignExtra;
1900b57cec5SDimitry Andric       BS.second = AlignExtra;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric       Offset += AlignExtra;
1930b57cec5SDimitry Andric     }
1940b57cec5SDimitry Andric 
195*4824e7fdSDimitry Andric     Offset += BlockSizes[MBB.getNumber()].first;
1960b57cec5SDimitry Andric   }
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric /// Determine the offset from the branch in Src block to the Dest block.
2000b57cec5SDimitry Andric /// BrOffset is the offset of the branch instruction inside Src block.
computeBranchSize(MachineFunction & Fn,const MachineBasicBlock * Src,const MachineBasicBlock * Dest,unsigned BrOffset)2010b57cec5SDimitry Andric int PPCBSel::computeBranchSize(MachineFunction &Fn,
2020b57cec5SDimitry Andric                                const MachineBasicBlock *Src,
2030b57cec5SDimitry Andric                                const MachineBasicBlock *Dest,
2040b57cec5SDimitry Andric                                unsigned BrOffset) {
2050b57cec5SDimitry Andric   int BranchSize;
2068bcb0991SDimitry Andric   Align MaxAlign = Align(4);
2070b57cec5SDimitry Andric   bool NeedExtraAdjustment = false;
2080b57cec5SDimitry Andric   if (Dest->getNumber() <= Src->getNumber()) {
2090b57cec5SDimitry Andric     // If this is a backwards branch, the delta is the offset from the
2100b57cec5SDimitry Andric     // start of this block to this branch, plus the sizes of all blocks
2110b57cec5SDimitry Andric     // from this block to the dest.
2120b57cec5SDimitry Andric     BranchSize = BrOffset;
2130b57cec5SDimitry Andric     MaxAlign = std::max(MaxAlign, Src->getAlignment());
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric     int DestBlock = Dest->getNumber();
2160b57cec5SDimitry Andric     BranchSize += BlockSizes[DestBlock].first;
2170b57cec5SDimitry Andric     for (unsigned i = DestBlock+1, e = Src->getNumber(); i < e; ++i) {
2180b57cec5SDimitry Andric       BranchSize += BlockSizes[i].first;
2198bcb0991SDimitry Andric       MaxAlign = std::max(MaxAlign, Fn.getBlockNumbered(i)->getAlignment());
2200b57cec5SDimitry Andric     }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric     NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
2230b57cec5SDimitry Andric                           (DestBlock >= FirstImpreciseBlock);
2240b57cec5SDimitry Andric   } else {
2250b57cec5SDimitry Andric     // Otherwise, add the size of the blocks between this block and the
2260b57cec5SDimitry Andric     // dest to the number of bytes left in this block.
2270b57cec5SDimitry Andric     unsigned StartBlock = Src->getNumber();
2280b57cec5SDimitry Andric     BranchSize = BlockSizes[StartBlock].first - BrOffset;
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric     MaxAlign = std::max(MaxAlign, Dest->getAlignment());
2310b57cec5SDimitry Andric     for (unsigned i = StartBlock+1, e = Dest->getNumber(); i != e; ++i) {
2320b57cec5SDimitry Andric       BranchSize += BlockSizes[i].first;
2338bcb0991SDimitry Andric       MaxAlign = std::max(MaxAlign, Fn.getBlockNumbered(i)->getAlignment());
2340b57cec5SDimitry Andric     }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric     NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
2370b57cec5SDimitry Andric                           (Src->getNumber() >= FirstImpreciseBlock);
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   // We tend to over estimate code size due to large alignment and
2410b57cec5SDimitry Andric   // inline assembly. Usually it causes larger computed branch offset.
2420b57cec5SDimitry Andric   // But sometimes it may also causes smaller computed branch offset
2430b57cec5SDimitry Andric   // than actual branch offset. If the offset is close to the limit of
2440b57cec5SDimitry Andric   // encoding, it may cause problem at run time.
2450b57cec5SDimitry Andric   // Following is a simplified example.
2460b57cec5SDimitry Andric   //
2470b57cec5SDimitry Andric   //              actual        estimated
2480b57cec5SDimitry Andric   //              address        address
2490b57cec5SDimitry Andric   //    ...
2500b57cec5SDimitry Andric   //   bne Far      100            10c
2510b57cec5SDimitry Andric   //   .p2align 4
2520b57cec5SDimitry Andric   //   Near:        110            110
2530b57cec5SDimitry Andric   //    ...
2540b57cec5SDimitry Andric   //   Far:        8108           8108
2550b57cec5SDimitry Andric   //
2560b57cec5SDimitry Andric   //   Actual offset:    0x8108 - 0x100 = 0x8008
2570b57cec5SDimitry Andric   //   Computed offset:  0x8108 - 0x10c = 0x7ffc
2580b57cec5SDimitry Andric   //
2590b57cec5SDimitry Andric   // This example also shows when we can get the largest gap between
2600b57cec5SDimitry Andric   // estimated offset and actual offset. If there is an aligned block
2610b57cec5SDimitry Andric   // ABB between branch and target, assume its alignment is <align>
2620b57cec5SDimitry Andric   // bits. Now consider the accumulated function size FSIZE till the end
2630b57cec5SDimitry Andric   // of previous block PBB. If the estimated FSIZE is multiple of
2640b57cec5SDimitry Andric   // 2^<align>, we don't need any padding for the estimated address of
2650b57cec5SDimitry Andric   // ABB. If actual FSIZE at the end of PBB is 4 bytes more than
2660b57cec5SDimitry Andric   // multiple of 2^<align>, then we need (2^<align> - 4) bytes of
2670b57cec5SDimitry Andric   // padding. It also means the actual branch offset is (2^<align> - 4)
2680b57cec5SDimitry Andric   // larger than computed offset. Other actual FSIZE needs less padding
2690b57cec5SDimitry Andric   // bytes, so causes smaller gap between actual and computed offset.
2700b57cec5SDimitry Andric   //
2710b57cec5SDimitry Andric   // On the other hand, if the inline asm or large alignment occurs
2720b57cec5SDimitry Andric   // between the branch block and destination block, the estimated address
2730b57cec5SDimitry Andric   // can be <delta> larger than actual address. If padding bytes are
2740b57cec5SDimitry Andric   // needed for a later aligned block, the actual number of padding bytes
2750b57cec5SDimitry Andric   // is at most <delta> more than estimated padding bytes. So the actual
2760b57cec5SDimitry Andric   // aligned block address is less than or equal to the estimated aligned
2770b57cec5SDimitry Andric   // block address. So the actual branch offset is less than or equal to
2780b57cec5SDimitry Andric   // computed branch offset.
2790b57cec5SDimitry Andric   //
2800b57cec5SDimitry Andric   // The computed offset is at most ((1 << alignment) - 4) bytes smaller
2810b57cec5SDimitry Andric   // than actual offset. So we add this number to the offset for safety.
2820b57cec5SDimitry Andric   if (NeedExtraAdjustment)
2838bcb0991SDimitry Andric     BranchSize += MaxAlign.value() - 4;
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   return BranchSize;
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & Fn)2880b57cec5SDimitry Andric bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
2890b57cec5SDimitry Andric   const PPCInstrInfo *TII =
2900b57cec5SDimitry Andric       static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
2910b57cec5SDimitry Andric   // Give the blocks of the function a dense, in-order, numbering.
2920b57cec5SDimitry Andric   Fn.RenumberBlocks();
2930b57cec5SDimitry Andric   BlockSizes.resize(Fn.getNumBlockIDs());
2940b57cec5SDimitry Andric   FirstImpreciseBlock = -1;
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   // Measure each MBB and compute a size for the entire function.
2970b57cec5SDimitry Andric   unsigned FuncSize = ComputeBlockSizes(Fn);
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   // If the entire function is smaller than the displacement of a branch field,
3000b57cec5SDimitry Andric   // we know we don't need to shrink any branches in this function.  This is a
3010b57cec5SDimitry Andric   // common case.
3020b57cec5SDimitry Andric   if (FuncSize < (1 << 15)) {
3030b57cec5SDimitry Andric     BlockSizes.clear();
3040b57cec5SDimitry Andric     return false;
3050b57cec5SDimitry Andric   }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   // For each conditional branch, if the offset to its destination is larger
3080b57cec5SDimitry Andric   // than the offset field allows, transform it into a long branch sequence
3090b57cec5SDimitry Andric   // like this:
3100b57cec5SDimitry Andric   //   short branch:
3110b57cec5SDimitry Andric   //     bCC MBB
3120b57cec5SDimitry Andric   //   long branch:
3130b57cec5SDimitry Andric   //     b!CC $PC+8
3140b57cec5SDimitry Andric   //     b MBB
3150b57cec5SDimitry Andric   //
3160b57cec5SDimitry Andric   bool MadeChange = true;
3170b57cec5SDimitry Andric   bool EverMadeChange = false;
3180b57cec5SDimitry Andric   while (MadeChange) {
3190b57cec5SDimitry Andric     // Iteratively expand branches until we reach a fixed point.
3200b57cec5SDimitry Andric     MadeChange = false;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric     for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
3230b57cec5SDimitry Andric          ++MFI) {
3240b57cec5SDimitry Andric       MachineBasicBlock &MBB = *MFI;
3250b57cec5SDimitry Andric       unsigned MBBStartOffset = 0;
3260b57cec5SDimitry Andric       for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
3270b57cec5SDimitry Andric            I != E; ++I) {
3280b57cec5SDimitry Andric         MachineBasicBlock *Dest = nullptr;
3290b57cec5SDimitry Andric         if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
3300b57cec5SDimitry Andric           Dest = I->getOperand(2).getMBB();
3310b57cec5SDimitry Andric         else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
3320b57cec5SDimitry Andric                  !I->getOperand(1).isImm())
3330b57cec5SDimitry Andric           Dest = I->getOperand(1).getMBB();
3340b57cec5SDimitry Andric         else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
3350b57cec5SDimitry Andric                   I->getOpcode() == PPC::BDZ8  || I->getOpcode() == PPC::BDZ) &&
3360b57cec5SDimitry Andric                  !I->getOperand(0).isImm())
3370b57cec5SDimitry Andric           Dest = I->getOperand(0).getMBB();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric         if (!Dest) {
3400b57cec5SDimitry Andric           MBBStartOffset += TII->getInstSizeInBytes(*I);
3410b57cec5SDimitry Andric           continue;
3420b57cec5SDimitry Andric         }
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric         // Determine the offset from the current branch to the destination
3450b57cec5SDimitry Andric         // block.
3460b57cec5SDimitry Andric         int BranchSize = computeBranchSize(Fn, &MBB, Dest, MBBStartOffset);
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric         // If this branch is in range, ignore it.
3490b57cec5SDimitry Andric         if (isInt<16>(BranchSize)) {
3500b57cec5SDimitry Andric           MBBStartOffset += 4;
3510b57cec5SDimitry Andric           continue;
3520b57cec5SDimitry Andric         }
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric         // Otherwise, we have to expand it to a long branch.
3550b57cec5SDimitry Andric         MachineInstr &OldBranch = *I;
3560b57cec5SDimitry Andric         DebugLoc dl = OldBranch.getDebugLoc();
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric         if (I->getOpcode() == PPC::BCC) {
3590b57cec5SDimitry Andric           // The BCC operands are:
3600b57cec5SDimitry Andric           // 0. PPC branch predicate
3610b57cec5SDimitry Andric           // 1. CR register
3620b57cec5SDimitry Andric           // 2. Target MBB
3630b57cec5SDimitry Andric           PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
3648bcb0991SDimitry Andric           Register CRReg = I->getOperand(1).getReg();
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric           // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
3670b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BCC))
3680b57cec5SDimitry Andric             .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
3690b57cec5SDimitry Andric         } else if (I->getOpcode() == PPC::BC) {
3708bcb0991SDimitry Andric           Register CRBit = I->getOperand(0).getReg();
3710b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
3720b57cec5SDimitry Andric         } else if (I->getOpcode() == PPC::BCn) {
3738bcb0991SDimitry Andric           Register CRBit = I->getOperand(0).getReg();
3740b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
3750b57cec5SDimitry Andric         } else if (I->getOpcode() == PPC::BDNZ) {
3760b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
3770b57cec5SDimitry Andric         } else if (I->getOpcode() == PPC::BDNZ8) {
3780b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
3790b57cec5SDimitry Andric         } else if (I->getOpcode() == PPC::BDZ) {
3800b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
3810b57cec5SDimitry Andric         } else if (I->getOpcode() == PPC::BDZ8) {
3820b57cec5SDimitry Andric           BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
3830b57cec5SDimitry Andric         } else {
3840b57cec5SDimitry Andric            llvm_unreachable("Unhandled branch type!");
3850b57cec5SDimitry Andric         }
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric         // Uncond branch to the real destination.
3880b57cec5SDimitry Andric         I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric         // Remove the old branch from the function.
3910b57cec5SDimitry Andric         OldBranch.eraseFromParent();
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric         // Remember that this instruction is 8-bytes, increase the size of the
3940b57cec5SDimitry Andric         // block by 4, remember to iterate.
3950b57cec5SDimitry Andric         BlockSizes[MBB.getNumber()].first += 4;
3960b57cec5SDimitry Andric         MBBStartOffset += 8;
3970b57cec5SDimitry Andric         ++NumExpanded;
3980b57cec5SDimitry Andric         MadeChange = true;
3990b57cec5SDimitry Andric       }
4000b57cec5SDimitry Andric     }
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric     if (MadeChange) {
4030b57cec5SDimitry Andric       // If we're going to iterate again, make sure we've updated our
4040b57cec5SDimitry Andric       // padding-based contributions to the block sizes.
4050b57cec5SDimitry Andric       modifyAdjustment(Fn);
4060b57cec5SDimitry Andric     }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric     EverMadeChange |= MadeChange;
4090b57cec5SDimitry Andric   }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   BlockSizes.clear();
412fe6060f1SDimitry Andric   return EverMadeChange;
4130b57cec5SDimitry Andric }
414