1 //===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Nate Baegeman and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that scans a machine function to determine which
11 // conditional branches need more than 16 bits of displacement to reach their
12 // target basic block. It does this in two passes; a calculation of basic block
13 // positions pass, and a branch psuedo op to machine branch opcode pass. This
14 // pass should be run last, just before the assembly printer.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "ppc-branch-select"
20 #include "PPCInstrBuilder.h"
21 #include "PPCInstrInfo.h"
22 #include "PPCPredicates.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/MathExtras.h"
31 STATISTIC(NumExpanded, "Number of branches expanded to long format");
34 struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass {
35 /// BlockSizes - The sizes of the basic blocks in the function.
36 std::vector<unsigned> BlockSizes;
38 virtual bool runOnMachineFunction(MachineFunction &Fn);
40 virtual const char *getPassName() const {
41 return "PowerPC Branch Selector";
46 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
49 FunctionPass *llvm::createPPCBranchSelectionPass() {
53 /// getNumBytesForInstruction - Return the number of bytes of code the specified
54 /// instruction may be. This returns the maximum number of bytes.
56 static unsigned getNumBytesForInstruction(MachineInstr *MI) {
57 switch (MI->getOpcode()) {
58 case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
59 case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
60 case PPC::IMPLICIT_DEF_F4: // no asm emitted
61 case PPC::IMPLICIT_DEF_F8: // no asm emitted
62 case PPC::IMPLICIT_DEF_VRRC: // no asm emitted
64 case PPC::INLINEASM: { // Inline Asm: Variable size.
65 MachineFunction *MF = MI->getParent()->getParent();
66 const char *AsmStr = MI->getOperand(0).getSymbolName();
67 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
73 return 4; // PowerPC instructions are all 4 bytes
78 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
79 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
80 // Give the blocks of the function a dense, in-order, numbering.
82 BlockSizes.resize(Fn.getNumBlockIDs());
84 // Measure each MBB and compute a size for the entire function.
85 unsigned FuncSize = 0;
86 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
88 MachineBasicBlock *MBB = MFI;
90 unsigned BlockSize = 0;
91 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
93 BlockSize += getNumBytesForInstruction(MBBI);
95 BlockSizes[MBB->getNumber()] = BlockSize;
96 FuncSize += BlockSize;
99 // If the entire function is smaller than the displacement of a branch field,
100 // we know we don't need to shrink any branches in this function. This is a
102 if (FuncSize < (1 << 15)) {
107 // For each conditional branch, if the offset to its destination is larger
108 // than the offset field allows, transform it into a long branch sequence
116 bool MadeChange = true;
117 bool EverMadeChange = false;
119 // Iteratively expand branches until we reach a fixed point.
122 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
124 MachineBasicBlock &MBB = *MFI;
125 unsigned MBBStartOffset = 0;
126 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
128 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
129 MBBStartOffset += getNumBytesForInstruction(I);
133 // Determine the offset from the current branch to the destination
135 MachineBasicBlock *Dest = I->getOperand(2).getMachineBasicBlock();
138 if (Dest->getNumber() <= MBB.getNumber()) {
139 // If this is a backwards branch, the delta is the offset from the
140 // start of this block to this branch, plus the sizes of all blocks
141 // from this block to the dest.
142 BranchSize = MBBStartOffset;
144 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
145 BranchSize += BlockSizes[i];
147 // Otherwise, add the size of the blocks between this block and the
148 // dest to the number of bytes left in this block.
149 BranchSize = -MBBStartOffset;
151 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
152 BranchSize += BlockSizes[i];
155 // If this branch is in range, ignore it.
156 if (isInt16(BranchSize)) {
161 // Otherwise, we have to expand it to a long branch.
162 // The BCC operands are:
163 // 0. PPC branch predicate
166 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
167 unsigned CRReg = I->getOperand(1).getReg();
169 MachineInstr *OldBranch = I;
171 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
172 BuildMI(MBB, I, TII->get(PPC::BCC))
173 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
175 // Uncond branch to the real destination.
176 I = BuildMI(MBB, I, TII->get(PPC::B)).addMBB(Dest);
178 // Remove the old branch from the function.
179 OldBranch->eraseFromParent();
181 // Remember that this instruction is 8-bytes, increase the size of the
182 // block by 4, remember to iterate.
183 BlockSizes[MBB.getNumber()] += 4;
189 EverMadeChange |= MadeChange;