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 using namespace llvm; 29 30 #define DEBUG_TYPE "ppc-branch-select" 31 32 STATISTIC(NumExpanded, "Number of branches expanded to long format"); 33 34 namespace llvm { 35 void initializePPCBSelPass(PassRegistry&); 36 } 37 38 namespace { 39 struct PPCBSel : public MachineFunctionPass { 40 static char ID; 41 PPCBSel() : MachineFunctionPass(ID) { 42 initializePPCBSelPass(*PassRegistry::getPassRegistry()); 43 } 44 45 // The sizes of the basic blocks in the function (the first 46 // element of the pair); the second element of the pair is the amount of the 47 // size that is due to potential padding. 48 std::vector<std::pair<unsigned, unsigned>> BlockSizes; 49 50 bool runOnMachineFunction(MachineFunction &Fn) override; 51 52 MachineFunctionProperties getRequiredProperties() const override { 53 return MachineFunctionProperties().set( 54 MachineFunctionProperties::Property::NoVRegs); 55 } 56 57 StringRef getPassName() const override { return "PowerPC Branch Selector"; } 58 }; 59 char PPCBSel::ID = 0; 60 } 61 62 INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector", 63 false, false) 64 65 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection 66 /// Pass 67 /// 68 FunctionPass *llvm::createPPCBranchSelectionPass() { 69 return new PPCBSel(); 70 } 71 72 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) { 73 const PPCInstrInfo *TII = 74 static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo()); 75 // Give the blocks of the function a dense, in-order, numbering. 76 Fn.RenumberBlocks(); 77 BlockSizes.resize(Fn.getNumBlockIDs()); 78 79 auto GetAlignmentAdjustment = 80 [](MachineBasicBlock &MBB, unsigned Offset) -> unsigned { 81 unsigned Align = MBB.getAlignment(); 82 if (!Align) 83 return 0; 84 85 unsigned AlignAmt = 1 << Align; 86 unsigned ParentAlign = MBB.getParent()->getAlignment(); 87 88 if (Align <= ParentAlign) 89 return OffsetToAlignment(Offset, AlignAmt); 90 91 // The alignment of this MBB is larger than the function's alignment, so we 92 // can't tell whether or not it will insert nops. Assume that it will. 93 return AlignAmt + OffsetToAlignment(Offset, AlignAmt); 94 }; 95 96 // We need to be careful about the offset of the first block in the function 97 // because it might not have the function's alignment. This happens because, 98 // under the ELFv2 ABI, for functions which require a TOC pointer, we add a 99 // two-instruction sequence to the start of the function. 100 // Note: This needs to be synchronized with the check in 101 // PPCLinuxAsmPrinter::EmitFunctionBodyStart. 102 unsigned InitialOffset = 0; 103 if (Fn.getSubtarget<PPCSubtarget>().isELFv2ABI() && 104 !Fn.getRegInfo().use_empty(PPC::X2)) 105 InitialOffset = 8; 106 107 // Measure each MBB and compute a size for the entire function. 108 unsigned FuncSize = InitialOffset; 109 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; 110 ++MFI) { 111 MachineBasicBlock *MBB = &*MFI; 112 113 // The end of the previous block may have extra nops if this block has an 114 // alignment requirement. 115 if (MBB->getNumber() > 0) { 116 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize); 117 118 auto &BS = BlockSizes[MBB->getNumber()-1]; 119 BS.first += AlignExtra; 120 BS.second = AlignExtra; 121 122 FuncSize += AlignExtra; 123 } 124 125 unsigned BlockSize = 0; 126 for (MachineInstr &MI : *MBB) 127 BlockSize += TII->getInstSizeInBytes(MI); 128 129 BlockSizes[MBB->getNumber()].first = BlockSize; 130 FuncSize += BlockSize; 131 } 132 133 // If the entire function is smaller than the displacement of a branch field, 134 // we know we don't need to shrink any branches in this function. This is a 135 // common case. 136 if (FuncSize < (1 << 15)) { 137 BlockSizes.clear(); 138 return false; 139 } 140 141 // For each conditional branch, if the offset to its destination is larger 142 // than the offset field allows, transform it into a long branch sequence 143 // like this: 144 // short branch: 145 // bCC MBB 146 // long branch: 147 // b!CC $PC+8 148 // b MBB 149 // 150 bool MadeChange = true; 151 bool EverMadeChange = false; 152 while (MadeChange) { 153 // Iteratively expand branches until we reach a fixed point. 154 MadeChange = false; 155 156 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; 157 ++MFI) { 158 MachineBasicBlock &MBB = *MFI; 159 unsigned MBBStartOffset = 0; 160 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); 161 I != E; ++I) { 162 MachineBasicBlock *Dest = nullptr; 163 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm()) 164 Dest = I->getOperand(2).getMBB(); 165 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) && 166 !I->getOperand(1).isImm()) 167 Dest = I->getOperand(1).getMBB(); 168 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ || 169 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) && 170 !I->getOperand(0).isImm()) 171 Dest = I->getOperand(0).getMBB(); 172 173 if (!Dest) { 174 MBBStartOffset += TII->getInstSizeInBytes(*I); 175 continue; 176 } 177 178 // Determine the offset from the current branch to the destination 179 // block. 180 int BranchSize; 181 if (Dest->getNumber() <= MBB.getNumber()) { 182 // If this is a backwards branch, the delta is the offset from the 183 // start of this block to this branch, plus the sizes of all blocks 184 // from this block to the dest. 185 BranchSize = MBBStartOffset; 186 187 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i) 188 BranchSize += BlockSizes[i].first; 189 } else { 190 // Otherwise, add the size of the blocks between this block and the 191 // dest to the number of bytes left in this block. 192 BranchSize = -MBBStartOffset; 193 194 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i) 195 BranchSize += BlockSizes[i].first; 196 } 197 198 // If this branch is in range, ignore it. 199 if (isInt<16>(BranchSize)) { 200 MBBStartOffset += 4; 201 continue; 202 } 203 204 // Otherwise, we have to expand it to a long branch. 205 MachineInstr &OldBranch = *I; 206 DebugLoc dl = OldBranch.getDebugLoc(); 207 208 if (I->getOpcode() == PPC::BCC) { 209 // The BCC operands are: 210 // 0. PPC branch predicate 211 // 1. CR register 212 // 2. Target MBB 213 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm(); 214 unsigned CRReg = I->getOperand(1).getReg(); 215 216 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition. 217 BuildMI(MBB, I, dl, TII->get(PPC::BCC)) 218 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2); 219 } else if (I->getOpcode() == PPC::BC) { 220 unsigned CRBit = I->getOperand(0).getReg(); 221 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2); 222 } else if (I->getOpcode() == PPC::BCn) { 223 unsigned CRBit = I->getOperand(0).getReg(); 224 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2); 225 } else if (I->getOpcode() == PPC::BDNZ) { 226 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2); 227 } else if (I->getOpcode() == PPC::BDNZ8) { 228 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2); 229 } else if (I->getOpcode() == PPC::BDZ) { 230 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2); 231 } else if (I->getOpcode() == PPC::BDZ8) { 232 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2); 233 } else { 234 llvm_unreachable("Unhandled branch type!"); 235 } 236 237 // Uncond branch to the real destination. 238 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest); 239 240 // Remove the old branch from the function. 241 OldBranch.eraseFromParent(); 242 243 // Remember that this instruction is 8-bytes, increase the size of the 244 // block by 4, remember to iterate. 245 BlockSizes[MBB.getNumber()].first += 4; 246 MBBStartOffset += 8; 247 ++NumExpanded; 248 MadeChange = true; 249 } 250 } 251 252 if (MadeChange) { 253 // If we're going to iterate again, make sure we've updated our 254 // padding-based contributions to the block sizes. 255 unsigned Offset = InitialOffset; 256 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; 257 ++MFI) { 258 MachineBasicBlock *MBB = &*MFI; 259 260 if (MBB->getNumber() > 0) { 261 auto &BS = BlockSizes[MBB->getNumber()-1]; 262 BS.first -= BS.second; 263 Offset -= BS.second; 264 265 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset); 266 267 BS.first += AlignExtra; 268 BS.second = AlignExtra; 269 270 Offset += AlignExtra; 271 } 272 273 Offset += BlockSizes[MBB->getNumber()].first; 274 } 275 } 276 277 EverMadeChange |= MadeChange; 278 } 279 280 BlockSizes.clear(); 281 return true; 282 } 283