blob: 52948c868b9c9fcd2065f33c49f524a71036af58 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "ppc-branch-select"
19#include "PPC.h"
20#include "PPCInstrBuilder.h"
21#include "PPCInstrInfo.h"
22#include "PPCPredicates.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/Target/TargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/MathExtras.h"
27using namespace llvm;
28
29STATISTIC(NumExpanded, "Number of branches expanded to long format");
30
31namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000032 struct PPCBSel : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +000034 PPCBSel() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035
36 /// BlockSizes - The sizes of the basic blocks in the function.
37 std::vector<unsigned> BlockSizes;
38
39 virtual bool runOnMachineFunction(MachineFunction &Fn);
40
41 virtual const char *getPassName() const {
42 return "PowerPC Branch Selector";
43 }
44 };
45 char PPCBSel::ID = 0;
46}
47
48/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
49/// Pass
50///
51FunctionPass *llvm::createPPCBranchSelectionPass() {
52 return new PPCBSel();
53}
54
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
56 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
57 // Give the blocks of the function a dense, in-order, numbering.
58 Fn.RenumberBlocks();
59 BlockSizes.resize(Fn.getNumBlockIDs());
60
61 // Measure each MBB and compute a size for the entire function.
62 unsigned FuncSize = 0;
63 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
64 ++MFI) {
65 MachineBasicBlock *MBB = MFI;
66
67 unsigned BlockSize = 0;
68 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
69 MBBI != EE; ++MBBI)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +000070 BlockSize += TII->GetInstSizeInBytes(MBBI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 BlockSizes[MBB->getNumber()] = BlockSize;
73 FuncSize += BlockSize;
74 }
75
76 // If the entire function is smaller than the displacement of a branch field,
77 // we know we don't need to shrink any branches in this function. This is a
78 // common case.
79 if (FuncSize < (1 << 15)) {
80 BlockSizes.clear();
81 return false;
82 }
83
84 // For each conditional branch, if the offset to its destination is larger
85 // than the offset field allows, transform it into a long branch sequence
86 // like this:
87 // short branch:
88 // bCC MBB
89 // long branch:
90 // b!CC $PC+8
91 // b MBB
92 //
93 bool MadeChange = true;
94 bool EverMadeChange = false;
95 while (MadeChange) {
96 // Iteratively expand branches until we reach a fixed point.
97 MadeChange = false;
98
99 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
100 ++MFI) {
101 MachineBasicBlock &MBB = *MFI;
102 unsigned MBBStartOffset = 0;
103 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
104 I != E; ++I) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000105 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000106 MBBStartOffset += TII->GetInstSizeInBytes(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 continue;
108 }
109
110 // Determine the offset from the current branch to the destination
111 // block.
Chris Lattner6017d482007-12-30 23:10:15 +0000112 MachineBasicBlock *Dest = I->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
114 int BranchSize;
115 if (Dest->getNumber() <= MBB.getNumber()) {
116 // If this is a backwards branch, the delta is the offset from the
117 // start of this block to this branch, plus the sizes of all blocks
118 // from this block to the dest.
119 BranchSize = MBBStartOffset;
120
121 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
122 BranchSize += BlockSizes[i];
123 } else {
124 // Otherwise, add the size of the blocks between this block and the
125 // dest to the number of bytes left in this block.
126 BranchSize = -MBBStartOffset;
127
128 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
129 BranchSize += BlockSizes[i];
130 }
131
132 // If this branch is in range, ignore it.
Benjamin Kramer25c5cb62010-03-29 21:13:41 +0000133 if (isInt<16>(BranchSize)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 MBBStartOffset += 4;
135 continue;
136 }
137
138 // Otherwise, we have to expand it to a long branch.
139 // The BCC operands are:
140 // 0. PPC branch predicate
141 // 1. CR register
142 // 2. Target MBB
143 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
144 unsigned CRReg = I->getOperand(1).getReg();
145
146 MachineInstr *OldBranch = I;
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000147 DebugLoc dl = OldBranch->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
149 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000150 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
152
153 // Uncond branch to the real destination.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000154 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
156 // Remove the old branch from the function.
157 OldBranch->eraseFromParent();
158
159 // Remember that this instruction is 8-bytes, increase the size of the
160 // block by 4, remember to iterate.
161 BlockSizes[MBB.getNumber()] += 4;
162 MBBStartOffset += 8;
163 ++NumExpanded;
164 MadeChange = true;
165 }
166 }
167 EverMadeChange |= MadeChange;
168 }
169
170 BlockSizes.clear();
171 return true;
172}
173