blob: 4f0dfaf874f77a229dc7c0b8a8d08483f486892f [file] [log] [blame]
Matt Arsenault36919a42016-10-06 15:38:53 +00001//===-- BranchRelaxation.cpp ----------------------------------------------===//
Tim Northover3b0846e2014-05-24 12:50:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Tim Northover3b0846e2014-05-24 12:50:23 +00009
Matt Arsenault36919a42016-10-06 15:38:53 +000010#include "llvm/CodeGen/Passes.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000011#include "llvm/ADT/SmallVector.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000012#include "llvm/ADT/Statistic.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
Matt Arsenault36919a42016-10-06 15:38:53 +000014#include "llvm/Target/TargetInstrInfo.h"
15#include "llvm/Target/TargetSubtargetInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000016#include "llvm/Support/Debug.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "llvm/Support/Format.h"
18#include "llvm/Support/raw_ostream.h"
Matt Arsenault36919a42016-10-06 15:38:53 +000019
Tim Northover3b0846e2014-05-24 12:50:23 +000020using namespace llvm;
21
Matt Arsenault36919a42016-10-06 15:38:53 +000022#define DEBUG_TYPE "branch-relaxation"
Tim Northover3b0846e2014-05-24 12:50:23 +000023
Tim Northover3b0846e2014-05-24 12:50:23 +000024STATISTIC(NumSplit, "Number of basic blocks split");
Matt Arsenault567631b2016-08-23 01:30:30 +000025STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
Tim Northover3b0846e2014-05-24 12:50:23 +000026
Matt Arsenault36919a42016-10-06 15:38:53 +000027#define BRANCH_RELAX_NAME "Branch relaxation pass"
Chad Rosier1c814322015-08-05 16:12:10 +000028
Tim Northover3b0846e2014-05-24 12:50:23 +000029namespace {
Matt Arsenault36919a42016-10-06 15:38:53 +000030class BranchRelaxation : public MachineFunctionPass {
Tim Northover3b0846e2014-05-24 12:50:23 +000031 /// BasicBlockInfo - Information about the offset and size of a single
32 /// basic block.
33 struct BasicBlockInfo {
34 /// Offset - Distance from the beginning of the function to the beginning
35 /// of this basic block.
36 ///
37 /// The offset is always aligned as required by the basic block.
38 unsigned Offset;
39
40 /// Size - Size of the basic block in bytes. If the block contains
41 /// inline assembly, this is a worst case estimate.
42 ///
43 /// The size does not include any alignment padding whether from the
44 /// beginning of the block, or from an aligned jump table at the end.
45 unsigned Size;
46
47 BasicBlockInfo() : Offset(0), Size(0) {}
48
Matt Arsenaultef5bba02016-10-06 16:00:58 +000049 /// Compute the offset immediately following this block. \p MBB is the next
50 /// block.
51 unsigned postOffset(const MachineBasicBlock &MBB) const {
Tim Northover3b0846e2014-05-24 12:50:23 +000052 unsigned PO = Offset + Size;
Matt Arsenaultef5bba02016-10-06 16:00:58 +000053 unsigned Align = MBB.getAlignment();
54 if (Align == 0)
55 return PO;
56
57 unsigned AlignAmt = 1 << Align;
58 unsigned ParentAlign = MBB.getParent()->getAlignment();
59 if (Align <= ParentAlign)
60 return PO + OffsetToAlignment(PO, AlignAmt);
61
62 // The alignment of this MBB is larger than the function's alignment, so we
63 // can't tell whether or not it will insert nops. Assume that it will.
64 return PO + AlignAmt + OffsetToAlignment(PO, AlignAmt);
Tim Northover3b0846e2014-05-24 12:50:23 +000065 }
66 };
67
68 SmallVector<BasicBlockInfo, 16> BlockInfo;
69
70 MachineFunction *MF;
Matt Arsenault36919a42016-10-06 15:38:53 +000071 const TargetInstrInfo *TII;
Tim Northover3b0846e2014-05-24 12:50:23 +000072
73 bool relaxBranchInstructions();
74 void scanFunction();
Matt Arsenaultf7065e12016-08-02 07:20:09 +000075 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
Tim Northover3b0846e2014-05-24 12:50:23 +000076 void adjustBlockOffsets(MachineBasicBlock &MBB);
Matt Arsenaulte8da1452016-08-02 08:06:17 +000077 bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
Matt Arsenault5b549712016-08-02 08:30:06 +000078
Matt Arsenaultf7065e12016-08-02 07:20:09 +000079 bool fixupConditionalBranch(MachineInstr &MI);
Matt Arsenault36919a42016-10-06 15:38:53 +000080 uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
Matt Arsenaulte8da1452016-08-02 08:06:17 +000081 unsigned getInstrOffset(const MachineInstr &MI) const;
Tim Northover3b0846e2014-05-24 12:50:23 +000082 void dumpBBs();
83 void verify();
84
85public:
86 static char ID;
Matt Arsenault36919a42016-10-06 15:38:53 +000087 BranchRelaxation() : MachineFunctionPass(ID) { }
Tim Northover3b0846e2014-05-24 12:50:23 +000088
89 bool runOnMachineFunction(MachineFunction &MF) override;
90
Matt Arsenault36919a42016-10-06 15:38:53 +000091 StringRef getPassName() const override {
92 return BRANCH_RELAX_NAME;
93 }
Tim Northover3b0846e2014-05-24 12:50:23 +000094};
Matt Arsenault36919a42016-10-06 15:38:53 +000095
Alexander Kornienkof00654e2015-06-23 09:49:53 +000096}
Tim Northover3b0846e2014-05-24 12:50:23 +000097
Matt Arsenault36919a42016-10-06 15:38:53 +000098char BranchRelaxation::ID = 0;
99char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
100
101INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
Chad Rosier1c814322015-08-05 16:12:10 +0000102
Tim Northover3b0846e2014-05-24 12:50:23 +0000103/// verify - check BBOffsets, BBSizes, alignment of islands
Matt Arsenault36919a42016-10-06 15:38:53 +0000104void BranchRelaxation::verify() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000105#ifndef NDEBUG
106 unsigned PrevNum = MF->begin()->getNumber();
107 for (MachineBasicBlock &MBB : *MF) {
108 unsigned Align = MBB.getAlignment();
109 unsigned Num = MBB.getNumber();
110 assert(BlockInfo[Num].Offset % (1u << Align) == 0);
Matt Arsenaultef5bba02016-10-06 16:00:58 +0000111 assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
Tim Northover3b0846e2014-05-24 12:50:23 +0000112 PrevNum = Num;
113 }
114#endif
115}
116
117/// print block size and offset information - debugging
Matt Arsenault36919a42016-10-06 15:38:53 +0000118void BranchRelaxation::dumpBBs() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000119 for (auto &MBB : *MF) {
120 const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
121 dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
122 << format("size=%#x\n", BBI.Size);
123 }
124}
125
Tim Northover3b0846e2014-05-24 12:50:23 +0000126/// scanFunction - Do the initial scan of the function, building up
127/// information about each block.
Matt Arsenault36919a42016-10-06 15:38:53 +0000128void BranchRelaxation::scanFunction() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000129 BlockInfo.clear();
130 BlockInfo.resize(MF->getNumBlockIDs());
131
132 // First thing, compute the size of all basic blocks, and see if the function
133 // has any inline assembly in it. If so, we have to be conservative about
134 // alignment assumptions, as we don't know for sure the size of any
135 // instructions in the inline assembly.
136 for (MachineBasicBlock &MBB : *MF)
Matt Arsenault36919a42016-10-06 15:38:53 +0000137 BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000138
139 // Compute block offsets and known bits.
140 adjustBlockOffsets(*MF->begin());
141}
142
143/// computeBlockSize - Compute the size for MBB.
Matt Arsenault36919a42016-10-06 15:38:53 +0000144uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
145 uint64_t Size = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000146 for (const MachineInstr &MI : MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000147 Size += TII->getInstSizeInBytes(MI);
Matt Arsenault36919a42016-10-06 15:38:53 +0000148 return Size;
Tim Northover3b0846e2014-05-24 12:50:23 +0000149}
150
151/// getInstrOffset - Return the current offset of the specified machine
152/// instruction from the start of the function. This offset changes as stuff is
153/// moved around inside the function.
Matt Arsenault36919a42016-10-06 15:38:53 +0000154unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000155 const MachineBasicBlock *MBB = MI.getParent();
Tim Northover3b0846e2014-05-24 12:50:23 +0000156
157 // The offset is composed of two things: the sum of the sizes of all MBB's
158 // before this instruction's block, and the offset from the start of the block
159 // it is in.
160 unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
161
162 // Sum instructions before MI in MBB.
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000163 for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000164 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000165 Offset += TII->getInstSizeInBytes(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000166 }
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000167
Tim Northover3b0846e2014-05-24 12:50:23 +0000168 return Offset;
169}
170
Matt Arsenault36919a42016-10-06 15:38:53 +0000171void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000172 unsigned PrevNum = Start.getNumber();
173 for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
174 unsigned Num = MBB.getNumber();
175 if (!Num) // block zero is never changed from offset zero.
176 continue;
177 // Get the offset and known bits at the end of the layout predecessor.
178 // Include the alignment of the current block.
Matt Arsenaultef5bba02016-10-06 16:00:58 +0000179 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
180
Tim Northover3b0846e2014-05-24 12:50:23 +0000181 PrevNum = Num;
182 }
183}
184
185/// Split the basic block containing MI into two blocks, which are joined by
186/// an unconditional branch. Update data structures and renumber blocks to
187/// account for this change and returns the newly created block.
188/// NOTE: Successor list of the original BB is out of date after this function,
189/// and must be updated by the caller! Other transforms follow using this
190/// utility function, so no point updating now rather than waiting.
Matt Arsenault36919a42016-10-06 15:38:53 +0000191MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI) {
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000192 MachineBasicBlock *OrigBB = MI.getParent();
Tim Northover3b0846e2014-05-24 12:50:23 +0000193
194 // Create a new MBB for the code after the OrigBB.
195 MachineBasicBlock *NewBB =
196 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
Duncan P. N. Exon Smithd3b9df02015-10-13 20:02:15 +0000197 MF->insert(++OrigBB->getIterator(), NewBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000198
199 // Splice the instructions starting with MI over to NewBB.
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000200 NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
Tim Northover3b0846e2014-05-24 12:50:23 +0000201
202 // Add an unconditional branch from OrigBB to NewBB.
203 // Note the new unconditional branch is not being recorded.
204 // There doesn't seem to be meaningful DebugInfo available; this doesn't
205 // correspond to anything in the source.
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000206 TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
Tim Northover3b0846e2014-05-24 12:50:23 +0000207
208 // Insert an entry into BlockInfo to align it properly with the block numbers.
209 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
210
211 // Figure out how large the OrigBB is. As the first half of the original
212 // block, it cannot contain a tablejump. The size includes
213 // the new jump we added. (It should be possible to do this without
214 // recounting everything, but it's very confusing, and this is rarely
215 // executed.)
Matt Arsenault36919a42016-10-06 15:38:53 +0000216 BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000217
Matt Arsenault36919a42016-10-06 15:38:53 +0000218 // Figure out how large the NewMBB is. As the second half of the original
Tim Northover3b0846e2014-05-24 12:50:23 +0000219 // block, it may contain a tablejump.
Matt Arsenault36919a42016-10-06 15:38:53 +0000220 BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000221
222 // All BBOffsets following these blocks must be modified.
223 adjustBlockOffsets(*OrigBB);
224
225 ++NumSplit;
226
227 return NewBB;
228}
229
230/// isBlockInRange - Returns true if the distance between specific MI and
231/// specific BB can fit in MI's displacement field.
Matt Arsenault36919a42016-10-06 15:38:53 +0000232bool BranchRelaxation::isBlockInRange(
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000233 const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000234 int64_t BrOffset = getInstrOffset(MI);
235 int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
Tim Northover3b0846e2014-05-24 12:50:23 +0000236
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000237 if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +0000238 return true;
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000239
240 DEBUG(
241 dbgs() << "Out of range branch to destination BB#" << DestBB.getNumber()
242 << " from BB#" << MI.getParent()->getNumber()
243 << " to " << DestOffset
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000244 << " offset " << DestOffset - BrOffset
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000245 << '\t' << MI
246 );
247
248 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000249}
250
Tim Northover3b0846e2014-05-24 12:50:23 +0000251/// fixupConditionalBranch - Fix up a conditional branch whose destination is
252/// too far away to fit in its displacement field. It is converted to an inverse
253/// conditional branch + an unconditional branch to the destination.
Matt Arsenault36919a42016-10-06 15:38:53 +0000254bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000255 DebugLoc DL = MI.getDebugLoc();
256 MachineBasicBlock *MBB = MI.getParent();
257 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
258 SmallVector<MachineOperand, 4> Cond;
259
260 bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
261 assert(!Fail && "branches to be relaxed must be analyzable");
262 (void)Fail;
Tim Northover3b0846e2014-05-24 12:50:23 +0000263
264 // Add an unconditional branch to the destination and invert the branch
265 // condition to jump over it:
266 // tbz L1
267 // =>
268 // tbnz L2
269 // b L1
270 // L2:
271
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000272 if (FBB && isBlockInRange(MI, *FBB)) {
273 // Last MI in the BB is an unconditional branch. We can simply invert the
274 // condition and swap destinations:
275 // beq L1
276 // b L2
277 // =>
278 // bne L2
279 // b L1
280 DEBUG(dbgs() << " Invert condition and swap "
281 "its destination with " << MBB->back());
Tim Northover3b0846e2014-05-24 12:50:23 +0000282
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000283 TII->reverseBranchCondition(Cond);
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000284 int OldSize = 0, NewSize = 0;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000285 TII->removeBranch(*MBB, &OldSize);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000286 TII->insertBranch(*MBB, FBB, TBB, Cond, DL, &NewSize);
Matt Arsenault5b549712016-08-02 08:30:06 +0000287
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000288 BlockInfo[MBB->getNumber()].Size += (NewSize - OldSize);
289 return true;
290 } else if (FBB) {
291 // We need to split the basic block here to obtain two long-range
292 // unconditional branches.
293 auto &NewBB = *MF->CreateMachineBasicBlock(MBB->getBasicBlock());
294 MF->insert(++MBB->getIterator(), &NewBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000295
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000296 // Insert an entry into BlockInfo to align it properly with the block
297 // numbers.
298 BlockInfo.insert(BlockInfo.begin() + NewBB.getNumber(), BasicBlockInfo());
Tim Northover3b0846e2014-05-24 12:50:23 +0000299
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000300 unsigned &NewBBSize = BlockInfo[NewBB.getNumber()].Size;
301 int NewBrSize;
302 TII->insertUnconditionalBranch(NewBB, FBB, DL, &NewBrSize);
303 NewBBSize += NewBrSize;
Tim Northover3b0846e2014-05-24 12:50:23 +0000304
305 // Update the successor lists according to the transformation to follow.
306 // Do it here since if there's no split, no update is needed.
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000307 MBB->replaceSuccessor(FBB, &NewBB);
308 NewBB.addSuccessor(FBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000309 }
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000310
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000311 // We now have an appropriate fall-through block in place (either naturally or
312 // just created), so we can invert the condition.
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000313 MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
Tim Northover3b0846e2014-05-24 12:50:23 +0000314
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000315 DEBUG(dbgs() << " Insert B to BB#" << TBB->getNumber()
Tim Northover3b0846e2014-05-24 12:50:23 +0000316 << ", invert condition and change dest. to BB#"
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000317 << NextBB.getNumber() << '\n');
Tim Northover3b0846e2014-05-24 12:50:23 +0000318
Matt Arsenault5b549712016-08-02 08:30:06 +0000319 unsigned &MBBSize = BlockInfo[MBB->getNumber()].Size;
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000320
Matt Arsenault5b549712016-08-02 08:30:06 +0000321 // Insert a new conditional branch and a new unconditional branch.
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000322 int RemovedSize = 0;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000323 TII->reverseBranchCondition(Cond);
324 TII->removeBranch(*MBB, &RemovedSize);
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000325 MBBSize -= RemovedSize;
Matt Arsenault5b549712016-08-02 08:30:06 +0000326
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000327 int AddedSize = 0;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000328 TII->insertBranch(*MBB, &NextBB, TBB, Cond, DL, &AddedSize);
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000329 MBBSize += AddedSize;
Tim Northover3b0846e2014-05-24 12:50:23 +0000330
331 // Finally, keep the block offsets up to date.
332 adjustBlockOffsets(*MBB);
333 return true;
334}
335
Matt Arsenault36919a42016-10-06 15:38:53 +0000336bool BranchRelaxation::relaxBranchInstructions() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000337 bool Changed = false;
338 // Relaxing branches involves creating new basic blocks, so re-eval
339 // end() for termination.
Matt Arsenaultf1c39062016-06-16 21:21:49 +0000340 for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
341 MachineBasicBlock &MBB = *I;
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000342 MachineBasicBlock::iterator J = MBB.getFirstTerminator();
343 if (J == MBB.end())
344 continue;
345
Matt Arsenault36919a42016-10-06 15:38:53 +0000346
Matt Arsenault567631b2016-08-23 01:30:30 +0000347 MachineBasicBlock::iterator Next;
348 for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
349 J != MBB.end(); J = Next) {
350 Next = std::next(J);
351 MachineInstr &MI = *J;
352
353 if (MI.isConditionalBranch()) {
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000354 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
Matt Arsenault567631b2016-08-23 01:30:30 +0000355 if (!isBlockInRange(MI, *DestBB)) {
356 if (Next != MBB.end() && Next->isConditionalBranch()) {
357 // If there are multiple conditional branches, this isn't an
358 // analyzable block. Split later terminators into a new block so
359 // each one will be analyzable.
360
361 MachineBasicBlock *NewBB = splitBlockBeforeInstr(*Next);
362 NewBB->transferSuccessors(&MBB);
363 MBB.addSuccessor(NewBB);
364 MBB.addSuccessor(DestBB);
365
366 // Cleanup potential unconditional branch to successor block.
367 NewBB->updateTerminator();
368 MBB.updateTerminator();
369 } else {
370 fixupConditionalBranch(MI);
371 ++NumConditionalRelaxed;
372 }
373
374 Changed = true;
375
376 // This may have modified all of the terminators, so start over.
377 Next = MBB.getFirstTerminator();
378 }
Matt Arsenault567631b2016-08-23 01:30:30 +0000379 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000380 }
381 }
Matt Arsenault567631b2016-08-23 01:30:30 +0000382
Tim Northover3b0846e2014-05-24 12:50:23 +0000383 return Changed;
384}
385
Matt Arsenault36919a42016-10-06 15:38:53 +0000386bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000387 MF = &mf;
388
Matt Arsenault36919a42016-10-06 15:38:53 +0000389 DEBUG(dbgs() << "***** BranchRelaxation *****\n");
Tim Northover3b0846e2014-05-24 12:50:23 +0000390
Matt Arsenault36919a42016-10-06 15:38:53 +0000391 TII = MF->getSubtarget().getInstrInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000392
393 // Renumber all of the machine basic blocks in the function, guaranteeing that
394 // the numbers agree with the position of the block in the function.
395 MF->RenumberBlocks();
396
397 // Do the initial scan of the function, building up information about the
398 // sizes of each block.
399 scanFunction();
400
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000401 DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs(););
Tim Northover3b0846e2014-05-24 12:50:23 +0000402
403 bool MadeChange = false;
404 while (relaxBranchInstructions())
405 MadeChange = true;
406
407 // After a while, this might be made debug-only, but it is not expensive.
408 verify();
409
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000410 DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs());
Tim Northover3b0846e2014-05-24 12:50:23 +0000411
412 BlockInfo.clear();
413
414 return MadeChange;
415}