blob: ae76386fdfb6ed1485bac2cc2b9c3855bb940b3e [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"
Hal Finkel530fa5f2016-10-03 04:06:44 +000022#include "PPCSubtarget.h"
Chris Lattner96d73862006-11-16 18:13:49 +000023#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Hal Finkel530fa5f2016-10-03 04:06:44 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner542dfd52006-11-18 00:32:03 +000026#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000028#include "llvm/Target/TargetSubtargetInfo.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000029using namespace llvm;
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031#define DEBUG_TYPE "ppc-branch-select"
32
Chris Lattner1ef9cd42006-12-19 22:59:26 +000033STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattner96d73862006-11-16 18:13:49 +000034
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000035namespace llvm {
36 void initializePPCBSelPass(PassRegistry&);
37}
38
Misha Brukmanef8cf022004-07-27 18:33:06 +000039namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000040 struct PPCBSel : public MachineFunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000041 static char ID;
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000042 PPCBSel() : MachineFunctionPass(ID) {
43 initializePPCBSelPass(*PassRegistry::getPassRegistry());
44 }
Devang Patel09f162c2007-05-01 21:15:47 +000045
Hal Finkelf0bc9db2016-09-04 14:18:29 +000046 // The sizes of the basic blocks in the function (the first
47 // element of the pair); the second element of the pair is the amount of the
48 // size that is due to potential padding.
49 std::vector<std::pair<unsigned, unsigned>> BlockSizes;
Misha Brukmanef8cf022004-07-27 18:33:06 +000050
Craig Topper0d3fa922014-04-29 07:57:37 +000051 bool runOnMachineFunction(MachineFunction &Fn) override;
Misha Brukmanef8cf022004-07-27 18:33:06 +000052
Derek Schuff1dbf7a52016-04-04 17:09:25 +000053 MachineFunctionProperties getRequiredProperties() const override {
54 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000055 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000056 }
57
Mehdi Amini117296c2016-10-01 02:56:57 +000058 StringRef getPassName() const override { return "PowerPC Branch Selector"; }
Misha Brukmanef8cf022004-07-27 18:33:06 +000059 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000060 char PPCBSel::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000061}
Misha Brukmanef8cf022004-07-27 18:33:06 +000062
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000063INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
64 false, false)
65
Misha Brukmanef8cf022004-07-27 18:33:06 +000066/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
67/// Pass
68///
69FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000070 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000071}
Chris Lattner26e385a2006-02-08 19:33:26 +000072
Chris Lattner26e385a2006-02-08 19:33:26 +000073bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopherfc6de422014-08-05 02:39:49 +000074 const PPCInstrInfo *TII =
75 static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
Chris Lattner542dfd52006-11-18 00:32:03 +000076 // Give the blocks of the function a dense, in-order, numbering.
77 Fn.RenumberBlocks();
78 BlockSizes.resize(Fn.getNumBlockIDs());
79
Hal Finkeld73bfba2015-01-03 14:58:25 +000080 auto GetAlignmentAdjustment =
81 [TII](MachineBasicBlock &MBB, unsigned Offset) -> unsigned {
82 unsigned Align = MBB.getAlignment();
83 if (!Align)
84 return 0;
85
86 unsigned AlignAmt = 1 << Align;
87 unsigned ParentAlign = MBB.getParent()->getAlignment();
88
89 if (Align <= ParentAlign)
90 return OffsetToAlignment(Offset, AlignAmt);
91
92 // The alignment of this MBB is larger than the function's alignment, so we
93 // can't tell whether or not it will insert nops. Assume that it will.
94 return AlignAmt + OffsetToAlignment(Offset, AlignAmt);
95 };
96
Hal Finkel530fa5f2016-10-03 04:06:44 +000097 // We need to be careful about the offset of the first block in the function
98 // because it might not have the function's alignment. This happens because,
99 // under the ELFv2 ABI, for functions which require a TOC pointer, we add a
100 // two-instruction sequence to the start of the function.
101 // Note: This needs to be synchronized with the check in
102 // PPCLinuxAsmPrinter::EmitFunctionBodyStart.
103 unsigned InitialOffset = 0;
104 if (Fn.getSubtarget<PPCSubtarget>().isELFv2ABI() &&
105 !Fn.getRegInfo().use_empty(PPC::X2))
106 InitialOffset = 8;
107
Chris Lattner542dfd52006-11-18 00:32:03 +0000108 // Measure each MBB and compute a size for the entire function.
Hal Finkel530fa5f2016-10-03 04:06:44 +0000109 unsigned FuncSize = InitialOffset;
Chris Lattner26e385a2006-02-08 19:33:26 +0000110 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
111 ++MFI) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +0000112 MachineBasicBlock *MBB = &*MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +0000113
Hal Finkeld73bfba2015-01-03 14:58:25 +0000114 // The end of the previous block may have extra nops if this block has an
115 // alignment requirement.
116 if (MBB->getNumber() > 0) {
117 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize);
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000118
119 auto &BS = BlockSizes[MBB->getNumber()-1];
120 BS.first += AlignExtra;
121 BS.second = AlignExtra;
122
Hal Finkeld73bfba2015-01-03 14:58:25 +0000123 FuncSize += AlignExtra;
124 }
125
Chris Lattner542dfd52006-11-18 00:32:03 +0000126 unsigned BlockSize = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000127 for (MachineInstr &MI : *MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000128 BlockSize += TII->getInstSizeInBytes(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000129
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000130 BlockSizes[MBB->getNumber()].first = BlockSize;
Chris Lattner542dfd52006-11-18 00:32:03 +0000131 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +0000132 }
133
Chris Lattner542dfd52006-11-18 00:32:03 +0000134 // If the entire function is smaller than the displacement of a branch field,
135 // we know we don't need to shrink any branches in this function. This is a
136 // common case.
137 if (FuncSize < (1 << 15)) {
138 BlockSizes.clear();
139 return false;
140 }
141
142 // For each conditional branch, if the offset to its destination is larger
143 // than the offset field allows, transform it into a long branch sequence
144 // like this:
145 // short branch:
146 // bCC MBB
147 // long branch:
148 // b!CC $PC+8
149 // b MBB
150 //
151 bool MadeChange = true;
152 bool EverMadeChange = false;
153 while (MadeChange) {
154 // Iteratively expand branches until we reach a fixed point.
155 MadeChange = false;
156
157 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
158 ++MFI) {
159 MachineBasicBlock &MBB = *MFI;
160 unsigned MBBStartOffset = 0;
161 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
162 I != E; ++I) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000163 MachineBasicBlock *Dest = nullptr;
Hal Finkelc5211292013-05-21 14:21:09 +0000164 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
165 Dest = I->getOperand(2).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000166 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
167 !I->getOperand(1).isImm())
168 Dest = I->getOperand(1).getMBB();
Hal Finkelc5211292013-05-21 14:21:09 +0000169 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
170 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) &&
171 !I->getOperand(0).isImm())
172 Dest = I->getOperand(0).getMBB();
173
174 if (!Dest) {
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000175 MBBStartOffset += TII->getInstSizeInBytes(*I);
Chris Lattner542dfd52006-11-18 00:32:03 +0000176 continue;
177 }
178
179 // Determine the offset from the current branch to the destination
180 // block.
Chris Lattner542dfd52006-11-18 00:32:03 +0000181 int BranchSize;
182 if (Dest->getNumber() <= MBB.getNumber()) {
183 // If this is a backwards branch, the delta is the offset from the
184 // start of this block to this branch, plus the sizes of all blocks
185 // from this block to the dest.
186 BranchSize = MBBStartOffset;
187
188 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000189 BranchSize += BlockSizes[i].first;
Chris Lattner542dfd52006-11-18 00:32:03 +0000190 } else {
191 // Otherwise, add the size of the blocks between this block and the
192 // dest to the number of bytes left in this block.
193 BranchSize = -MBBStartOffset;
194
195 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000196 BranchSize += BlockSizes[i].first;
Chris Lattner542dfd52006-11-18 00:32:03 +0000197 }
198
199 // If this branch is in range, ignore it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000200 if (isInt<16>(BranchSize)) {
Chris Lattner542dfd52006-11-18 00:32:03 +0000201 MBBStartOffset += 4;
202 continue;
203 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000204
Chris Lattner542dfd52006-11-18 00:32:03 +0000205 // Otherwise, we have to expand it to a long branch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000206 MachineInstr &OldBranch = *I;
207 DebugLoc dl = OldBranch.getDebugLoc();
208
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000209 if (I->getOpcode() == PPC::BCC) {
210 // The BCC operands are:
211 // 0. PPC branch predicate
212 // 1. CR register
213 // 2. Target MBB
214 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
215 unsigned CRReg = I->getOperand(1).getReg();
216
217 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
218 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
219 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
Hal Finkel940ab932014-02-28 00:27:01 +0000220 } else if (I->getOpcode() == PPC::BC) {
221 unsigned CRBit = I->getOperand(0).getReg();
222 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
223 } else if (I->getOpcode() == PPC::BCn) {
224 unsigned CRBit = I->getOperand(0).getReg();
225 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000226 } else if (I->getOpcode() == PPC::BDNZ) {
227 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
228 } else if (I->getOpcode() == PPC::BDNZ8) {
229 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
230 } else if (I->getOpcode() == PPC::BDZ) {
231 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
232 } else if (I->getOpcode() == PPC::BDZ8) {
233 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
234 } else {
235 llvm_unreachable("Unhandled branch type!");
236 }
Chris Lattner542dfd52006-11-18 00:32:03 +0000237
238 // Uncond branch to the real destination.
Dale Johannesene9f623e2009-02-13 02:27:39 +0000239 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner542dfd52006-11-18 00:32:03 +0000240
241 // Remove the old branch from the function.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000242 OldBranch.eraseFromParent();
243
Chris Lattner542dfd52006-11-18 00:32:03 +0000244 // Remember that this instruction is 8-bytes, increase the size of the
245 // block by 4, remember to iterate.
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000246 BlockSizes[MBB.getNumber()].first += 4;
Chris Lattner542dfd52006-11-18 00:32:03 +0000247 MBBStartOffset += 8;
248 ++NumExpanded;
249 MadeChange = true;
250 }
251 }
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000252
253 if (MadeChange) {
254 // If we're going to iterate again, make sure we've updated our
255 // padding-based contributions to the block sizes.
Hal Finkel530fa5f2016-10-03 04:06:44 +0000256 unsigned Offset = InitialOffset;
Hal Finkelf0bc9db2016-09-04 14:18:29 +0000257 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
258 ++MFI) {
259 MachineBasicBlock *MBB = &*MFI;
260
261 if (MBB->getNumber() > 0) {
262 auto &BS = BlockSizes[MBB->getNumber()-1];
263 BS.first -= BS.second;
264 Offset -= BS.second;
265
266 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset);
267
268 BS.first += AlignExtra;
269 BS.second = AlignExtra;
270
271 Offset += AlignExtra;
272 }
273
274 Offset += BlockSizes[MBB->getNumber()].first;
275 }
276 }
277
Chris Lattner542dfd52006-11-18 00:32:03 +0000278 EverMadeChange |= MadeChange;
279 }
280
281 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000282 return true;
283}