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 {
36 PPCBSel() : MachineFunctionPass((intptr_t)&ID) {}
38 /// BlockSizes - The sizes of the basic blocks in the function.
39 std::vector<unsigned> BlockSizes;
41 virtual bool runOnMachineFunction(MachineFunction &Fn);
43 virtual const char *getPassName() const {
44 return "PowerPC Branch Selector";
50 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
53 FunctionPass *llvm::createPPCBranchSelectionPass() {
57 /// getNumBytesForInstruction - Return the number of bytes of code the specified
58 /// instruction may be. This returns the maximum number of bytes.
60 static unsigned getNumBytesForInstruction(MachineInstr *MI) {
61 switch (MI->getOpcode()) {
62 case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
63 case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
64 case PPC::IMPLICIT_DEF_F4: // no asm emitted
65 case PPC::IMPLICIT_DEF_F8: // no asm emitted
66 case PPC::IMPLICIT_DEF_VRRC: // no asm emitted
68 case PPC::INLINEASM: { // Inline Asm: Variable size.
69 MachineFunction *MF = MI->getParent()->getParent();
70 const char *AsmStr = MI->getOperand(0).getSymbolName();
71 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
77 return 4; // PowerPC instructions are all 4 bytes
82 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
83 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
84 // Give the blocks of the function a dense, in-order, numbering.
86 BlockSizes.resize(Fn.getNumBlockIDs());
88 // Measure each MBB and compute a size for the entire function.
89 unsigned FuncSize = 0;
90 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
92 MachineBasicBlock *MBB = MFI;
94 unsigned BlockSize = 0;
95 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
97 BlockSize += getNumBytesForInstruction(MBBI);
99 BlockSizes[MBB->getNumber()] = BlockSize;
100 FuncSize += BlockSize;
103 // If the entire function is smaller than the displacement of a branch field,
104 // we know we don't need to shrink any branches in this function. This is a
106 if (FuncSize < (1 << 15)) {
111 // For each conditional branch, if the offset to its destination is larger
112 // than the offset field allows, transform it into a long branch sequence
120 bool MadeChange = true;
121 bool EverMadeChange = false;
123 // Iteratively expand branches until we reach a fixed point.
126 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
128 MachineBasicBlock &MBB = *MFI;
129 unsigned MBBStartOffset = 0;
130 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
132 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImmediate()) {
133 MBBStartOffset += getNumBytesForInstruction(I);
137 // Determine the offset from the current branch to the destination
139 MachineBasicBlock *Dest = I->getOperand(2).getMachineBasicBlock();
142 if (Dest->getNumber() <= MBB.getNumber()) {
143 // If this is a backwards branch, the delta is the offset from the
144 // start of this block to this branch, plus the sizes of all blocks
145 // from this block to the dest.
146 BranchSize = MBBStartOffset;
148 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
149 BranchSize += BlockSizes[i];
151 // Otherwise, add the size of the blocks between this block and the
152 // dest to the number of bytes left in this block.
153 BranchSize = -MBBStartOffset;
155 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
156 BranchSize += BlockSizes[i];
159 // If this branch is in range, ignore it.
160 if (isInt16(BranchSize)) {
165 // Otherwise, we have to expand it to a long branch.
166 // The BCC operands are:
167 // 0. PPC branch predicate
170 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
171 unsigned CRReg = I->getOperand(1).getReg();
173 MachineInstr *OldBranch = I;
175 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
176 BuildMI(MBB, I, TII->get(PPC::BCC))
177 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
179 // Uncond branch to the real destination.
180 I = BuildMI(MBB, I, TII->get(PPC::B)).addMBB(Dest);
182 // Remove the old branch from the function.
183 OldBranch->eraseFromParent();
185 // Remember that this instruction is 8-bytes, increase the size of the
186 // block by 4, remember to iterate.
187 BlockSizes[MBB.getNumber()] += 4;
193 EverMadeChange |= MadeChange;