blob: 6bcb062aad8c9795b8f2b70fd92e890f16a3c4b1 [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
Hal Finkelf0bc9db2016-09-04 14:18:29 +000044 // The sizes of the basic blocks in the function (the first
45 // element of the pair); the second element of the pair is the amount of the
46 // size that is due to potential padding.
47 std::vector<std::pair<unsigned, unsigned>> BlockSizes;
Misha Brukmanef8cf022004-07-27 18:33:06 +000048
Craig Topper0d3fa922014-04-29 07:57:37 +000049 bool runOnMachineFunction(MachineFunction &Fn) override;
Misha Brukmanef8cf022004-07-27 18:33:06 +000050
Derek Schuff1dbf7a52016-04-04 17:09:25 +000051 MachineFunctionProperties getRequiredProperties() const override {
52 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000053 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000054 }
55
Mehdi Amini117296c2016-10-01 02:56:57 +000056 StringRef getPassName() const override { return "PowerPC Branch Selector"; }
Misha Brukmanef8cf022004-07-27 18:33:06 +000057 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000058 char PPCBSel::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000059}
Misha Brukmanef8cf022004-07-27 18:33:06 +000060
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000061INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
62 false, false)
63
Misha Brukmanef8cf022004-07-27 18:33:06 +000064/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
65/// Pass
66///
67FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000068 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000069}
Chris Lattner26e385a2006-02-08 19:33:26 +000070
Chris Lattner26e385a2006-02-08 19:33:26 +000071bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopherfc6de422014-08-05 02:39:49 +000072 const PPCInstrInfo *TII =
73 static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
Chris Lattner542dfd52006-11-18 00:32:03 +000074 // Give the blocks of the function a dense, in-order, numbering.
75 Fn.RenumberBlocks();
76 BlockSizes.resize(Fn.getNumBlockIDs());
77
Hal Finkeld73bfba2015-01-03 14:58:25 +000078 auto GetAlignmentAdjustment =
79 [TII](MachineBasicBlock &MBB, unsigned Offset) -> unsigned {
80 unsigned Align = MBB.getAlignment();
81 if (!Align)
82 return 0;
83
84 unsigned AlignAmt = 1 << Align;
85 unsigned ParentAlign = MBB.getParent()->getAlignment();
86
87 if (Align <= ParentAlign)
88 return OffsetToAlignment(Offset, AlignAmt);
89
90 // The alignment of this MBB is larger than the function's alignment, so we
91 // can't tell whether or not it will insert nops. Assume that it will.
92 return AlignAmt + OffsetToAlignment(Offset, AlignAmt);
93 };
94
Chris Lattner542dfd52006-11-18 00:32:03 +000095 // Measure each MBB and compute a size for the entire function.
96 unsigned FuncSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000097 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
98 ++MFI) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +000099 MachineBasicBlock *MBB = &*MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +0000100
Hal Finkeld73bfba2015-01-03 14:58:25 +0000101 // The end of the previous block may have extra nops if this block has an
102 // alignment requirement.
103 if (MBB->getNumber() > 0) {
104 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize);
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000105
106 auto &BS = BlockSizes[MBB->getNumber()-1];
107 BS.first += AlignExtra;
108 BS.second = AlignExtra;
109
Hal Finkeld73bfba2015-01-03 14:58:25 +0000110 FuncSize += AlignExtra;
111 }
112
Chris Lattner542dfd52006-11-18 00:32:03 +0000113 unsigned BlockSize = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000114 for (MachineInstr &MI : *MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000115 BlockSize += TII->getInstSizeInBytes(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000116
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000117 BlockSizes[MBB->getNumber()].first = BlockSize;
Chris Lattner542dfd52006-11-18 00:32:03 +0000118 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +0000119 }
120
Chris Lattner542dfd52006-11-18 00:32:03 +0000121 // If the entire function is smaller than the displacement of a branch field,
122 // we know we don't need to shrink any branches in this function. This is a
123 // common case.
124 if (FuncSize < (1 << 15)) {
125 BlockSizes.clear();
126 return false;
127 }
128
129 // For each conditional branch, if the offset to its destination is larger
130 // than the offset field allows, transform it into a long branch sequence
131 // like this:
132 // short branch:
133 // bCC MBB
134 // long branch:
135 // b!CC $PC+8
136 // b MBB
137 //
138 bool MadeChange = true;
139 bool EverMadeChange = false;
140 while (MadeChange) {
141 // Iteratively expand branches until we reach a fixed point.
142 MadeChange = false;
143
144 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
145 ++MFI) {
146 MachineBasicBlock &MBB = *MFI;
147 unsigned MBBStartOffset = 0;
148 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
149 I != E; ++I) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000150 MachineBasicBlock *Dest = nullptr;
Hal Finkelc5211292013-05-21 14:21:09 +0000151 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
152 Dest = I->getOperand(2).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000153 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
154 !I->getOperand(1).isImm())
155 Dest = I->getOperand(1).getMBB();
Hal Finkelc5211292013-05-21 14:21:09 +0000156 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
157 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) &&
158 !I->getOperand(0).isImm())
159 Dest = I->getOperand(0).getMBB();
160
161 if (!Dest) {
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000162 MBBStartOffset += TII->getInstSizeInBytes(*I);
Chris Lattner542dfd52006-11-18 00:32:03 +0000163 continue;
164 }
165
166 // Determine the offset from the current branch to the destination
167 // block.
Chris Lattner542dfd52006-11-18 00:32:03 +0000168 int BranchSize;
169 if (Dest->getNumber() <= MBB.getNumber()) {
170 // If this is a backwards branch, the delta is the offset from the
171 // start of this block to this branch, plus the sizes of all blocks
172 // from this block to the dest.
173 BranchSize = MBBStartOffset;
174
175 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000176 BranchSize += BlockSizes[i].first;
Chris Lattner542dfd52006-11-18 00:32:03 +0000177 } else {
178 // Otherwise, add the size of the blocks between this block and the
179 // dest to the number of bytes left in this block.
180 BranchSize = -MBBStartOffset;
181
182 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000183 BranchSize += BlockSizes[i].first;
Chris Lattner542dfd52006-11-18 00:32:03 +0000184 }
185
186 // If this branch is in range, ignore it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000187 if (isInt<16>(BranchSize)) {
Chris Lattner542dfd52006-11-18 00:32:03 +0000188 MBBStartOffset += 4;
189 continue;
190 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000191
Chris Lattner542dfd52006-11-18 00:32:03 +0000192 // Otherwise, we have to expand it to a long branch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000193 MachineInstr &OldBranch = *I;
194 DebugLoc dl = OldBranch.getDebugLoc();
195
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000196 if (I->getOpcode() == PPC::BCC) {
197 // The BCC operands are:
198 // 0. PPC branch predicate
199 // 1. CR register
200 // 2. Target MBB
201 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
202 unsigned CRReg = I->getOperand(1).getReg();
203
204 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
205 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
206 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
Hal Finkel940ab932014-02-28 00:27:01 +0000207 } else if (I->getOpcode() == PPC::BC) {
208 unsigned CRBit = I->getOperand(0).getReg();
209 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
210 } else if (I->getOpcode() == PPC::BCn) {
211 unsigned CRBit = I->getOperand(0).getReg();
212 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000213 } else if (I->getOpcode() == PPC::BDNZ) {
214 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
215 } else if (I->getOpcode() == PPC::BDNZ8) {
216 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
217 } else if (I->getOpcode() == PPC::BDZ) {
218 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
219 } else if (I->getOpcode() == PPC::BDZ8) {
220 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
221 } else {
222 llvm_unreachable("Unhandled branch type!");
223 }
Chris Lattner542dfd52006-11-18 00:32:03 +0000224
225 // Uncond branch to the real destination.
Dale Johannesene9f623e2009-02-13 02:27:39 +0000226 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner542dfd52006-11-18 00:32:03 +0000227
228 // Remove the old branch from the function.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000229 OldBranch.eraseFromParent();
230
Chris Lattner542dfd52006-11-18 00:32:03 +0000231 // Remember that this instruction is 8-bytes, increase the size of the
232 // block by 4, remember to iterate.
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000233 BlockSizes[MBB.getNumber()].first += 4;
Chris Lattner542dfd52006-11-18 00:32:03 +0000234 MBBStartOffset += 8;
235 ++NumExpanded;
236 MadeChange = true;
237 }
238 }
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000239
240 if (MadeChange) {
241 // If we're going to iterate again, make sure we've updated our
242 // padding-based contributions to the block sizes.
243 unsigned Offset = 0;
244 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
245 ++MFI) {
246 MachineBasicBlock *MBB = &*MFI;
247
248 if (MBB->getNumber() > 0) {
249 auto &BS = BlockSizes[MBB->getNumber()-1];
250 BS.first -= BS.second;
251 Offset -= BS.second;
252
253 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset);
254
255 BS.first += AlignExtra;
256 BS.second = AlignExtra;
257
258 Offset += AlignExtra;
259 }
260
261 Offset += BlockSizes[MBB->getNumber()].first;
262 }
263 }
264
Chris Lattner542dfd52006-11-18 00:32:03 +0000265 EverMadeChange |= MadeChange;
266 }
267
268 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000269 return true;
270}