blob: a752421580d4be5364ec4e9f79c268afee9751b6 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman999d9cf2004-07-27 18:33:06 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman999d9cf2004-07-27 18:33:06 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb5f662f2005-04-21 23:30:14 +000010// This file contains a pass that scans a machine function to determine which
Misha Brukman999d9cf2004-07-27 18:33:06 +000011// 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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000018#define DEBUG_TYPE "ppc-branch-select"
Chris Lattner26689592005-10-14 23:51:18 +000019#include "PPC.h"
Chris Lattner26bd0d42005-10-14 23:45:43 +000020#include "PPCInstrBuilder.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000021#include "PPCInstrInfo.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000022#include "PPCPredicates.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnereea52d32006-10-13 17:56:02 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattnercb535952006-11-16 18:13:49 +000025#include "llvm/ADT/Statistic.h"
Chris Lattner54e853b2006-11-18 00:32:03 +000026#include "llvm/Support/MathExtras.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000027using namespace llvm;
28
Chris Lattner95b2c7d2006-12-19 22:59:26 +000029STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattnercb535952006-11-16 18:13:49 +000030
Misha Brukman999d9cf2004-07-27 18:33:06 +000031namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000032 struct PPCBSel : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000033 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000034 PPCBSel() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000035
Chris Lattner54e853b2006-11-18 00:32:03 +000036 /// BlockSizes - The sizes of the basic blocks in the function.
37 std::vector<unsigned> BlockSizes;
Misha Brukman999d9cf2004-07-27 18:33:06 +000038
Chris Lattner6f4a0722006-02-08 19:33:26 +000039 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukman999d9cf2004-07-27 18:33:06 +000040
41 virtual const char *getPassName() const {
Chris Lattner54e853b2006-11-18 00:32:03 +000042 return "PowerPC Branch Selector";
Misha Brukman999d9cf2004-07-27 18:33:06 +000043 }
44 };
Devang Patel19974732007-05-03 01:11:54 +000045 char PPCBSel::ID = 0;
Misha Brukman999d9cf2004-07-27 18:33:06 +000046}
47
48/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
49/// Pass
50///
51FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner6f4a0722006-02-08 19:33:26 +000052 return new PPCBSel();
Misha Brukman999d9cf2004-07-27 18:33:06 +000053}
Chris Lattner6f4a0722006-02-08 19:33:26 +000054
Chris Lattner6f4a0722006-02-08 19:33:26 +000055bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Evan Chengc0f64ff2006-11-27 23:37:22 +000056 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
Chris Lattner54e853b2006-11-18 00:32:03 +000057 // 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;
Chris Lattner6f4a0722006-02-08 19:33:26 +000063 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
64 ++MFI) {
65 MachineBasicBlock *MBB = MFI;
Chris Lattner54e853b2006-11-18 00:32:03 +000066
67 unsigned BlockSize = 0;
Chris Lattner6f4a0722006-02-08 19:33:26 +000068 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
69 MBBI != EE; ++MBBI)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +000070 BlockSize += TII->GetInstSizeInBytes(MBBI);
Chris Lattner6f4a0722006-02-08 19:33:26 +000071
Chris Lattner54e853b2006-11-18 00:32:03 +000072 BlockSizes[MBB->getNumber()] = BlockSize;
73 FuncSize += BlockSize;
Chris Lattner6f4a0722006-02-08 19:33:26 +000074 }
75
Chris Lattner54e853b2006-11-18 00:32:03 +000076 // 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 Gohmand735b802008-10-03 15:45:36 +0000105 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000106 MBBStartOffset += TII->GetInstSizeInBytes(I);
Chris Lattner54e853b2006-11-18 00:32:03 +0000107 continue;
108 }
109
110 // Determine the offset from the current branch to the destination
111 // block.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000112 MachineBasicBlock *Dest = I->getOperand(2).getMBB();
Chris Lattner54e853b2006-11-18 00:32:03 +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.
133 if (isInt16(BranchSize)) {
134 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 Johannesen536a2f12009-02-13 02:27:39 +0000147 DebugLoc dl = OldBranch->getDebugLoc();
Chris Lattner54e853b2006-11-18 00:32:03 +0000148
149 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
Dale Johannesen536a2f12009-02-13 02:27:39 +0000150 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
Chris Lattner54e853b2006-11-18 00:32:03 +0000151 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
152
153 // Uncond branch to the real destination.
Dale Johannesen536a2f12009-02-13 02:27:39 +0000154 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner54e853b2006-11-18 00:32:03 +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();
Chris Lattner6f4a0722006-02-08 19:33:26 +0000171 return true;
172}
173