blob: 8ce3bf90da40ea1e23544dc194859a0275036eee [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmanef8cf022004-07-27 18:33:06 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmanef8cf022004-07-27 18:33:06 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb4402432005-04-21 23:30:14 +000010// This file contains a pass that scans a machine function to determine which
Misha Brukmanef8cf022004-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
Gabor Greif21fed662010-08-23 20:30:51 +000013// positions pass, and a branch pseudo op to machine branch opcode pass. This
Misha Brukmanef8cf022004-07-27 18:33:06 +000014// pass should be run last, just before the assembly printer.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000018#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "MCTargetDesc/PPCPredicates.h"
Chris Lattnere80bf1b2005-10-14 23:45:43 +000020#include "PPCInstrBuilder.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000021#include "PPCInstrInfo.h"
Chris Lattner96d73862006-11-16 18:13:49 +000022#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner542dfd52006-11-18 00:32:03 +000024#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetMachine.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000026using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "ppc-branch-select"
29
Chris Lattner1ef9cd42006-12-19 22:59:26 +000030STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattner96d73862006-11-16 18:13:49 +000031
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000032namespace llvm {
33 void initializePPCBSelPass(PassRegistry&);
34}
35
Misha Brukmanef8cf022004-07-27 18:33:06 +000036namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000037 struct PPCBSel : public MachineFunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000038 static char ID;
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000039 PPCBSel() : MachineFunctionPass(ID) {
40 initializePPCBSelPass(*PassRegistry::getPassRegistry());
41 }
Devang Patel09f162c2007-05-01 21:15:47 +000042
Chris Lattner542dfd52006-11-18 00:32:03 +000043 /// BlockSizes - The sizes of the basic blocks in the function.
44 std::vector<unsigned> BlockSizes;
Misha Brukmanef8cf022004-07-27 18:33:06 +000045
Chris Lattner26e385a2006-02-08 19:33:26 +000046 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukmanef8cf022004-07-27 18:33:06 +000047
48 virtual const char *getPassName() const {
Chris Lattner542dfd52006-11-18 00:32:03 +000049 return "PowerPC Branch Selector";
Misha Brukmanef8cf022004-07-27 18:33:06 +000050 }
51 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000052 char PPCBSel::ID = 0;
Misha Brukmanef8cf022004-07-27 18:33:06 +000053}
54
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000055INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
56 false, false)
57
Misha Brukmanef8cf022004-07-27 18:33:06 +000058/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
59/// Pass
60///
61FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000062 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000063}
Chris Lattner26e385a2006-02-08 19:33:26 +000064
Chris Lattner26e385a2006-02-08 19:33:26 +000065bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Gabor Greif4ad727172010-07-23 13:28:47 +000066 const PPCInstrInfo *TII =
67 static_cast<const PPCInstrInfo*>(Fn.getTarget().getInstrInfo());
Chris Lattner542dfd52006-11-18 00:32:03 +000068 // Give the blocks of the function a dense, in-order, numbering.
69 Fn.RenumberBlocks();
70 BlockSizes.resize(Fn.getNumBlockIDs());
71
72 // Measure each MBB and compute a size for the entire function.
73 unsigned FuncSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000074 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
75 ++MFI) {
76 MachineBasicBlock *MBB = MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +000077
78 unsigned BlockSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000079 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
80 MBBI != EE; ++MBBI)
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +000081 BlockSize += TII->GetInstSizeInBytes(MBBI);
Chris Lattner26e385a2006-02-08 19:33:26 +000082
Chris Lattner542dfd52006-11-18 00:32:03 +000083 BlockSizes[MBB->getNumber()] = BlockSize;
84 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +000085 }
86
Chris Lattner542dfd52006-11-18 00:32:03 +000087 // If the entire function is smaller than the displacement of a branch field,
88 // we know we don't need to shrink any branches in this function. This is a
89 // common case.
90 if (FuncSize < (1 << 15)) {
91 BlockSizes.clear();
92 return false;
93 }
94
95 // For each conditional branch, if the offset to its destination is larger
96 // than the offset field allows, transform it into a long branch sequence
97 // like this:
98 // short branch:
99 // bCC MBB
100 // long branch:
101 // b!CC $PC+8
102 // b MBB
103 //
104 bool MadeChange = true;
105 bool EverMadeChange = false;
106 while (MadeChange) {
107 // Iteratively expand branches until we reach a fixed point.
108 MadeChange = false;
109
110 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
111 ++MFI) {
112 MachineBasicBlock &MBB = *MFI;
113 unsigned MBBStartOffset = 0;
114 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
115 I != E; ++I) {
Hal Finkelc5211292013-05-21 14:21:09 +0000116 MachineBasicBlock *Dest = 0;
117 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
118 Dest = I->getOperand(2).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000119 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
120 !I->getOperand(1).isImm())
121 Dest = I->getOperand(1).getMBB();
Hal Finkelc5211292013-05-21 14:21:09 +0000122 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
123 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) &&
124 !I->getOperand(0).isImm())
125 Dest = I->getOperand(0).getMBB();
126
127 if (!Dest) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000128 MBBStartOffset += TII->GetInstSizeInBytes(I);
Chris Lattner542dfd52006-11-18 00:32:03 +0000129 continue;
130 }
131
132 // Determine the offset from the current branch to the destination
133 // block.
Chris Lattner542dfd52006-11-18 00:32:03 +0000134 int BranchSize;
135 if (Dest->getNumber() <= MBB.getNumber()) {
136 // If this is a backwards branch, the delta is the offset from the
137 // start of this block to this branch, plus the sizes of all blocks
138 // from this block to the dest.
139 BranchSize = MBBStartOffset;
140
141 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
142 BranchSize += BlockSizes[i];
143 } else {
144 // Otherwise, add the size of the blocks between this block and the
145 // dest to the number of bytes left in this block.
146 BranchSize = -MBBStartOffset;
147
148 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
149 BranchSize += BlockSizes[i];
150 }
151
152 // If this branch is in range, ignore it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000153 if (isInt<16>(BranchSize)) {
Chris Lattner542dfd52006-11-18 00:32:03 +0000154 MBBStartOffset += 4;
155 continue;
156 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000157
Chris Lattner542dfd52006-11-18 00:32:03 +0000158 // Otherwise, we have to expand it to a long branch.
Chris Lattner542dfd52006-11-18 00:32:03 +0000159 MachineInstr *OldBranch = I;
Dale Johannesene9f623e2009-02-13 02:27:39 +0000160 DebugLoc dl = OldBranch->getDebugLoc();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000161
162 if (I->getOpcode() == PPC::BCC) {
163 // The BCC operands are:
164 // 0. PPC branch predicate
165 // 1. CR register
166 // 2. Target MBB
167 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
168 unsigned CRReg = I->getOperand(1).getReg();
169
170 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
171 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
172 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
Hal Finkel940ab932014-02-28 00:27:01 +0000173 } else if (I->getOpcode() == PPC::BC) {
174 unsigned CRBit = I->getOperand(0).getReg();
175 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
176 } else if (I->getOpcode() == PPC::BCn) {
177 unsigned CRBit = I->getOperand(0).getReg();
178 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000179 } else if (I->getOpcode() == PPC::BDNZ) {
180 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
181 } else if (I->getOpcode() == PPC::BDNZ8) {
182 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
183 } else if (I->getOpcode() == PPC::BDZ) {
184 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
185 } else if (I->getOpcode() == PPC::BDZ8) {
186 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
187 } else {
188 llvm_unreachable("Unhandled branch type!");
189 }
Chris Lattner542dfd52006-11-18 00:32:03 +0000190
191 // Uncond branch to the real destination.
Dale Johannesene9f623e2009-02-13 02:27:39 +0000192 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner542dfd52006-11-18 00:32:03 +0000193
194 // Remove the old branch from the function.
195 OldBranch->eraseFromParent();
196
197 // Remember that this instruction is 8-bytes, increase the size of the
198 // block by 4, remember to iterate.
199 BlockSizes[MBB.getNumber()] += 4;
200 MBBStartOffset += 8;
201 ++NumExpanded;
202 MadeChange = true;
203 }
204 }
205 EverMadeChange |= MadeChange;
206 }
207
208 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000209 return true;
210}
211