blob: 2fc5b3c025a15864781cbcde7824cf848101f023 [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"
26#include "llvm/Support/Compiler.h"
Chris Lattner54e853b2006-11-18 00:32:03 +000027#include "llvm/Support/MathExtras.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000028using namespace llvm;
29
Chris Lattner95b2c7d2006-12-19 22:59:26 +000030STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattnercb535952006-11-16 18:13:49 +000031
Misha Brukman999d9cf2004-07-27 18:33:06 +000032namespace {
Chris Lattner95255282006-06-28 23:17:24 +000033 struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000034 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000035 PPCBSel() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000036
Chris Lattner54e853b2006-11-18 00:32:03 +000037 /// BlockSizes - The sizes of the basic blocks in the function.
38 std::vector<unsigned> BlockSizes;
Misha Brukman999d9cf2004-07-27 18:33:06 +000039
Chris Lattner6f4a0722006-02-08 19:33:26 +000040 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukman999d9cf2004-07-27 18:33:06 +000041
42 virtual const char *getPassName() const {
Chris Lattner54e853b2006-11-18 00:32:03 +000043 return "PowerPC Branch Selector";
Misha Brukman999d9cf2004-07-27 18:33:06 +000044 }
45 };
Devang Patel19974732007-05-03 01:11:54 +000046 char PPCBSel::ID = 0;
Misha Brukman999d9cf2004-07-27 18:33:06 +000047}
48
49/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
50/// Pass
51///
52FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner6f4a0722006-02-08 19:33:26 +000053 return new PPCBSel();
Misha Brukman999d9cf2004-07-27 18:33:06 +000054}
Chris Lattner6f4a0722006-02-08 19:33:26 +000055
Chris Lattner6f4a0722006-02-08 19:33:26 +000056bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Evan Chengc0f64ff2006-11-27 23:37:22 +000057 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
Chris Lattner54e853b2006-11-18 00:32:03 +000058 // Give the blocks of the function a dense, in-order, numbering.
59 Fn.RenumberBlocks();
60 BlockSizes.resize(Fn.getNumBlockIDs());
61
62 // Measure each MBB and compute a size for the entire function.
63 unsigned FuncSize = 0;
Chris Lattner6f4a0722006-02-08 19:33:26 +000064 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
65 ++MFI) {
66 MachineBasicBlock *MBB = MFI;
Chris Lattner54e853b2006-11-18 00:32:03 +000067
68 unsigned BlockSize = 0;
Chris Lattner6f4a0722006-02-08 19:33:26 +000069 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
70 MBBI != EE; ++MBBI)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +000071 BlockSize += TII->GetInstSizeInBytes(MBBI);
Chris Lattner6f4a0722006-02-08 19:33:26 +000072
Chris Lattner54e853b2006-11-18 00:32:03 +000073 BlockSizes[MBB->getNumber()] = BlockSize;
74 FuncSize += BlockSize;
Chris Lattner6f4a0722006-02-08 19:33:26 +000075 }
76
Chris Lattner54e853b2006-11-18 00:32:03 +000077 // If the entire function is smaller than the displacement of a branch field,
78 // we know we don't need to shrink any branches in this function. This is a
79 // common case.
80 if (FuncSize < (1 << 15)) {
81 BlockSizes.clear();
82 return false;
83 }
84
85 // For each conditional branch, if the offset to its destination is larger
86 // than the offset field allows, transform it into a long branch sequence
87 // like this:
88 // short branch:
89 // bCC MBB
90 // long branch:
91 // b!CC $PC+8
92 // b MBB
93 //
94 bool MadeChange = true;
95 bool EverMadeChange = false;
96 while (MadeChange) {
97 // Iteratively expand branches until we reach a fixed point.
98 MadeChange = false;
99
100 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
101 ++MFI) {
102 MachineBasicBlock &MBB = *MFI;
103 unsigned MBBStartOffset = 0;
104 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
105 I != E; ++I) {
Dan Gohman92dfe202007-09-14 20:33:02 +0000106 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImmediate()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000107 MBBStartOffset += TII->GetInstSizeInBytes(I);
Chris Lattner54e853b2006-11-18 00:32:03 +0000108 continue;
109 }
110
111 // Determine the offset from the current branch to the destination
112 // block.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000113 MachineBasicBlock *Dest = I->getOperand(2).getMBB();
Chris Lattner54e853b2006-11-18 00:32:03 +0000114
115 int BranchSize;
116 if (Dest->getNumber() <= MBB.getNumber()) {
117 // If this is a backwards branch, the delta is the offset from the
118 // start of this block to this branch, plus the sizes of all blocks
119 // from this block to the dest.
120 BranchSize = MBBStartOffset;
121
122 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
123 BranchSize += BlockSizes[i];
124 } else {
125 // Otherwise, add the size of the blocks between this block and the
126 // dest to the number of bytes left in this block.
127 BranchSize = -MBBStartOffset;
128
129 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
130 BranchSize += BlockSizes[i];
131 }
132
133 // If this branch is in range, ignore it.
134 if (isInt16(BranchSize)) {
135 MBBStartOffset += 4;
136 continue;
137 }
138
139 // Otherwise, we have to expand it to a long branch.
140 // The BCC operands are:
141 // 0. PPC branch predicate
142 // 1. CR register
143 // 2. Target MBB
144 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
145 unsigned CRReg = I->getOperand(1).getReg();
146
147 MachineInstr *OldBranch = I;
148
149 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000150 BuildMI(MBB, I, 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.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000154 I = BuildMI(MBB, I, 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