blob: 41594be42eca90b387adb351e20a06a74c9d94c7 [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"
Eric Christopherd9134482014-08-04 21:25:23 +000026#include "llvm/Target/TargetSubtargetInfo.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "ppc-branch-select"
30
Chris Lattner1ef9cd42006-12-19 22:59:26 +000031STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattner96d73862006-11-16 18:13:49 +000032
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000033namespace llvm {
34 void initializePPCBSelPass(PassRegistry&);
35}
36
Misha Brukmanef8cf022004-07-27 18:33:06 +000037namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000038 struct PPCBSel : public MachineFunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000039 static char ID;
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000040 PPCBSel() : MachineFunctionPass(ID) {
41 initializePPCBSelPass(*PassRegistry::getPassRegistry());
42 }
Devang Patel09f162c2007-05-01 21:15:47 +000043
Chris Lattner542dfd52006-11-18 00:32:03 +000044 /// BlockSizes - The sizes of the basic blocks in the function.
45 std::vector<unsigned> BlockSizes;
Misha Brukmanef8cf022004-07-27 18:33:06 +000046
Craig Topper0d3fa922014-04-29 07:57:37 +000047 bool runOnMachineFunction(MachineFunction &Fn) override;
Misha Brukmanef8cf022004-07-27 18:33:06 +000048
Craig Topper0d3fa922014-04-29 07:57:37 +000049 const char *getPassName() const override {
Chris Lattner542dfd52006-11-18 00:32:03 +000050 return "PowerPC Branch Selector";
Misha Brukmanef8cf022004-07-27 18:33:06 +000051 }
52 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000053 char PPCBSel::ID = 0;
Misha Brukmanef8cf022004-07-27 18:33:06 +000054}
55
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000056INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
57 false, false)
58
Misha Brukmanef8cf022004-07-27 18:33:06 +000059/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
60/// Pass
61///
62FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000063 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000064}
Chris Lattner26e385a2006-02-08 19:33:26 +000065
Chris Lattner26e385a2006-02-08 19:33:26 +000066bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopherfc6de422014-08-05 02:39:49 +000067 const PPCInstrInfo *TII =
68 static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
Chris Lattner542dfd52006-11-18 00:32:03 +000069 // Give the blocks of the function a dense, in-order, numbering.
70 Fn.RenumberBlocks();
71 BlockSizes.resize(Fn.getNumBlockIDs());
72
73 // Measure each MBB and compute a size for the entire function.
74 unsigned FuncSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000075 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
76 ++MFI) {
77 MachineBasicBlock *MBB = MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +000078
79 unsigned BlockSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000080 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
81 MBBI != EE; ++MBBI)
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +000082 BlockSize += TII->GetInstSizeInBytes(MBBI);
Chris Lattner26e385a2006-02-08 19:33:26 +000083
Chris Lattner542dfd52006-11-18 00:32:03 +000084 BlockSizes[MBB->getNumber()] = BlockSize;
85 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +000086 }
87
Chris Lattner542dfd52006-11-18 00:32:03 +000088 // If the entire function is smaller than the displacement of a branch field,
89 // we know we don't need to shrink any branches in this function. This is a
90 // common case.
91 if (FuncSize < (1 << 15)) {
92 BlockSizes.clear();
93 return false;
94 }
95
96 // For each conditional branch, if the offset to its destination is larger
97 // than the offset field allows, transform it into a long branch sequence
98 // like this:
99 // short branch:
100 // bCC MBB
101 // long branch:
102 // b!CC $PC+8
103 // b MBB
104 //
105 bool MadeChange = true;
106 bool EverMadeChange = false;
107 while (MadeChange) {
108 // Iteratively expand branches until we reach a fixed point.
109 MadeChange = false;
110
111 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
112 ++MFI) {
113 MachineBasicBlock &MBB = *MFI;
114 unsigned MBBStartOffset = 0;
115 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
116 I != E; ++I) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000117 MachineBasicBlock *Dest = nullptr;
Hal Finkelc5211292013-05-21 14:21:09 +0000118 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
119 Dest = I->getOperand(2).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000120 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
121 !I->getOperand(1).isImm())
122 Dest = I->getOperand(1).getMBB();
Hal Finkelc5211292013-05-21 14:21:09 +0000123 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
124 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) &&
125 !I->getOperand(0).isImm())
126 Dest = I->getOperand(0).getMBB();
127
128 if (!Dest) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000129 MBBStartOffset += TII->GetInstSizeInBytes(I);
Chris Lattner542dfd52006-11-18 00:32:03 +0000130 continue;
131 }
132
133 // Determine the offset from the current branch to the destination
134 // block.
Chris Lattner542dfd52006-11-18 00:32:03 +0000135 int BranchSize;
136 if (Dest->getNumber() <= MBB.getNumber()) {
137 // If this is a backwards branch, the delta is the offset from the
138 // start of this block to this branch, plus the sizes of all blocks
139 // from this block to the dest.
140 BranchSize = MBBStartOffset;
141
142 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
143 BranchSize += BlockSizes[i];
144 } else {
145 // Otherwise, add the size of the blocks between this block and the
146 // dest to the number of bytes left in this block.
147 BranchSize = -MBBStartOffset;
148
149 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
150 BranchSize += BlockSizes[i];
151 }
152
153 // If this branch is in range, ignore it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000154 if (isInt<16>(BranchSize)) {
Chris Lattner542dfd52006-11-18 00:32:03 +0000155 MBBStartOffset += 4;
156 continue;
157 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000158
Chris Lattner542dfd52006-11-18 00:32:03 +0000159 // Otherwise, we have to expand it to a long branch.
Chris Lattner542dfd52006-11-18 00:32:03 +0000160 MachineInstr *OldBranch = I;
Dale Johannesene9f623e2009-02-13 02:27:39 +0000161 DebugLoc dl = OldBranch->getDebugLoc();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000162
163 if (I->getOpcode() == PPC::BCC) {
164 // The BCC operands are:
165 // 0. PPC branch predicate
166 // 1. CR register
167 // 2. Target MBB
168 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
169 unsigned CRReg = I->getOperand(1).getReg();
170
171 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
172 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
173 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
Hal Finkel940ab932014-02-28 00:27:01 +0000174 } else if (I->getOpcode() == PPC::BC) {
175 unsigned CRBit = I->getOperand(0).getReg();
176 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
177 } else if (I->getOpcode() == PPC::BCn) {
178 unsigned CRBit = I->getOperand(0).getReg();
179 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000180 } else if (I->getOpcode() == PPC::BDNZ) {
181 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
182 } else if (I->getOpcode() == PPC::BDNZ8) {
183 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
184 } else if (I->getOpcode() == PPC::BDZ) {
185 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
186 } else if (I->getOpcode() == PPC::BDZ8) {
187 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
188 } else {
189 llvm_unreachable("Unhandled branch type!");
190 }
Chris Lattner542dfd52006-11-18 00:32:03 +0000191
192 // Uncond branch to the real destination.
Dale Johannesene9f623e2009-02-13 02:27:39 +0000193 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner542dfd52006-11-18 00:32:03 +0000194
195 // Remove the old branch from the function.
196 OldBranch->eraseFromParent();
197
198 // Remember that this instruction is 8-bytes, increase the size of the
199 // block by 4, remember to iterate.
200 BlockSizes[MBB.getNumber()] += 4;
201 MBBStartOffset += 8;
202 ++NumExpanded;
203 MadeChange = true;
204 }
205 }
206 EverMadeChange |= MadeChange;
207 }
208
209 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000210 return true;
211}
212