blob: f1383064b79c2edcd6d0b6a684481d78016431cc [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
49 /// Compute the offset immediately following this block. If LogAlign is
50 /// specified, return the offset the successor block will get if it has
51 /// this alignment.
52 unsigned postOffset(unsigned LogAlign = 0) const {
53 unsigned PO = Offset + Size;
54 unsigned Align = 1 << LogAlign;
55 return (PO + Align - 1) / Align * Align;
56 }
57 };
58
59 SmallVector<BasicBlockInfo, 16> BlockInfo;
60
61 MachineFunction *MF;
Matt Arsenault36919a42016-10-06 15:38:53 +000062 const TargetInstrInfo *TII;
Tim Northover3b0846e2014-05-24 12:50:23 +000063
64 bool relaxBranchInstructions();
65 void scanFunction();
Matt Arsenaultf7065e12016-08-02 07:20:09 +000066 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
Tim Northover3b0846e2014-05-24 12:50:23 +000067 void adjustBlockOffsets(MachineBasicBlock &MBB);
Matt Arsenaulte8da1452016-08-02 08:06:17 +000068 bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
Matt Arsenault5b549712016-08-02 08:30:06 +000069
Matt Arsenaultf7065e12016-08-02 07:20:09 +000070 bool fixupConditionalBranch(MachineInstr &MI);
Matt Arsenault36919a42016-10-06 15:38:53 +000071 uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
Matt Arsenaulte8da1452016-08-02 08:06:17 +000072 unsigned getInstrOffset(const MachineInstr &MI) const;
Tim Northover3b0846e2014-05-24 12:50:23 +000073 void dumpBBs();
74 void verify();
75
76public:
77 static char ID;
Matt Arsenault36919a42016-10-06 15:38:53 +000078 BranchRelaxation() : MachineFunctionPass(ID) { }
Tim Northover3b0846e2014-05-24 12:50:23 +000079
80 bool runOnMachineFunction(MachineFunction &MF) override;
81
Matt Arsenault36919a42016-10-06 15:38:53 +000082 StringRef getPassName() const override {
83 return BRANCH_RELAX_NAME;
84 }
Tim Northover3b0846e2014-05-24 12:50:23 +000085};
Matt Arsenault36919a42016-10-06 15:38:53 +000086
Alexander Kornienkof00654e2015-06-23 09:49:53 +000087}
Tim Northover3b0846e2014-05-24 12:50:23 +000088
Matt Arsenault36919a42016-10-06 15:38:53 +000089char BranchRelaxation::ID = 0;
90char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
91
92INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
Chad Rosier1c814322015-08-05 16:12:10 +000093
Tim Northover3b0846e2014-05-24 12:50:23 +000094/// verify - check BBOffsets, BBSizes, alignment of islands
Matt Arsenault36919a42016-10-06 15:38:53 +000095void BranchRelaxation::verify() {
Tim Northover3b0846e2014-05-24 12:50:23 +000096#ifndef NDEBUG
97 unsigned PrevNum = MF->begin()->getNumber();
98 for (MachineBasicBlock &MBB : *MF) {
99 unsigned Align = MBB.getAlignment();
100 unsigned Num = MBB.getNumber();
101 assert(BlockInfo[Num].Offset % (1u << Align) == 0);
102 assert(!Num || BlockInfo[PrevNum].postOffset() <= BlockInfo[Num].Offset);
103 PrevNum = Num;
104 }
105#endif
106}
107
108/// print block size and offset information - debugging
Matt Arsenault36919a42016-10-06 15:38:53 +0000109void BranchRelaxation::dumpBBs() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000110 for (auto &MBB : *MF) {
111 const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
112 dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
113 << format("size=%#x\n", BBI.Size);
114 }
115}
116
Tim Northover3b0846e2014-05-24 12:50:23 +0000117/// scanFunction - Do the initial scan of the function, building up
118/// information about each block.
Matt Arsenault36919a42016-10-06 15:38:53 +0000119void BranchRelaxation::scanFunction() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000120 BlockInfo.clear();
121 BlockInfo.resize(MF->getNumBlockIDs());
122
123 // First thing, compute the size of all basic blocks, and see if the function
124 // has any inline assembly in it. If so, we have to be conservative about
125 // alignment assumptions, as we don't know for sure the size of any
126 // instructions in the inline assembly.
127 for (MachineBasicBlock &MBB : *MF)
Matt Arsenault36919a42016-10-06 15:38:53 +0000128 BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000129
130 // Compute block offsets and known bits.
131 adjustBlockOffsets(*MF->begin());
132}
133
134/// computeBlockSize - Compute the size for MBB.
Matt Arsenault36919a42016-10-06 15:38:53 +0000135uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
136 uint64_t Size = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000137 for (const MachineInstr &MI : MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000138 Size += TII->getInstSizeInBytes(MI);
Matt Arsenault36919a42016-10-06 15:38:53 +0000139 return Size;
Tim Northover3b0846e2014-05-24 12:50:23 +0000140}
141
142/// getInstrOffset - Return the current offset of the specified machine
143/// instruction from the start of the function. This offset changes as stuff is
144/// moved around inside the function.
Matt Arsenault36919a42016-10-06 15:38:53 +0000145unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000146 const MachineBasicBlock *MBB = MI.getParent();
Tim Northover3b0846e2014-05-24 12:50:23 +0000147
148 // The offset is composed of two things: the sum of the sizes of all MBB's
149 // before this instruction's block, and the offset from the start of the block
150 // it is in.
151 unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
152
153 // Sum instructions before MI in MBB.
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000154 for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000155 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000156 Offset += TII->getInstSizeInBytes(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000157 }
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000158
Tim Northover3b0846e2014-05-24 12:50:23 +0000159 return Offset;
160}
161
Matt Arsenault36919a42016-10-06 15:38:53 +0000162void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000163 unsigned PrevNum = Start.getNumber();
164 for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
165 unsigned Num = MBB.getNumber();
166 if (!Num) // block zero is never changed from offset zero.
167 continue;
168 // Get the offset and known bits at the end of the layout predecessor.
169 // Include the alignment of the current block.
170 unsigned LogAlign = MBB.getAlignment();
171 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(LogAlign);
172 PrevNum = Num;
173 }
174}
175
176/// Split the basic block containing MI into two blocks, which are joined by
177/// an unconditional branch. Update data structures and renumber blocks to
178/// account for this change and returns the newly created block.
179/// NOTE: Successor list of the original BB is out of date after this function,
180/// and must be updated by the caller! Other transforms follow using this
181/// utility function, so no point updating now rather than waiting.
Matt Arsenault36919a42016-10-06 15:38:53 +0000182MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI) {
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000183 MachineBasicBlock *OrigBB = MI.getParent();
Tim Northover3b0846e2014-05-24 12:50:23 +0000184
185 // Create a new MBB for the code after the OrigBB.
186 MachineBasicBlock *NewBB =
187 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
Duncan P. N. Exon Smithd3b9df02015-10-13 20:02:15 +0000188 MF->insert(++OrigBB->getIterator(), NewBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000189
190 // Splice the instructions starting with MI over to NewBB.
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000191 NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
Tim Northover3b0846e2014-05-24 12:50:23 +0000192
193 // Add an unconditional branch from OrigBB to NewBB.
194 // Note the new unconditional branch is not being recorded.
195 // There doesn't seem to be meaningful DebugInfo available; this doesn't
196 // correspond to anything in the source.
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000197 TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
Tim Northover3b0846e2014-05-24 12:50:23 +0000198
199 // Insert an entry into BlockInfo to align it properly with the block numbers.
200 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
201
202 // Figure out how large the OrigBB is. As the first half of the original
203 // block, it cannot contain a tablejump. The size includes
204 // the new jump we added. (It should be possible to do this without
205 // recounting everything, but it's very confusing, and this is rarely
206 // executed.)
Matt Arsenault36919a42016-10-06 15:38:53 +0000207 BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000208
Matt Arsenault36919a42016-10-06 15:38:53 +0000209 // Figure out how large the NewMBB is. As the second half of the original
Tim Northover3b0846e2014-05-24 12:50:23 +0000210 // block, it may contain a tablejump.
Matt Arsenault36919a42016-10-06 15:38:53 +0000211 BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000212
213 // All BBOffsets following these blocks must be modified.
214 adjustBlockOffsets(*OrigBB);
215
216 ++NumSplit;
217
218 return NewBB;
219}
220
221/// isBlockInRange - Returns true if the distance between specific MI and
222/// specific BB can fit in MI's displacement field.
Matt Arsenault36919a42016-10-06 15:38:53 +0000223bool BranchRelaxation::isBlockInRange(
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000224 const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000225 int64_t BrOffset = getInstrOffset(MI);
226 int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
Tim Northover3b0846e2014-05-24 12:50:23 +0000227
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000228 if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +0000229 return true;
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000230
231 DEBUG(
232 dbgs() << "Out of range branch to destination BB#" << DestBB.getNumber()
233 << " from BB#" << MI.getParent()->getNumber()
234 << " to " << DestOffset
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000235 << " offset " << DestOffset - BrOffset
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000236 << '\t' << MI
237 );
238
239 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000240}
241
Tim Northover3b0846e2014-05-24 12:50:23 +0000242/// fixupConditionalBranch - Fix up a conditional branch whose destination is
243/// too far away to fit in its displacement field. It is converted to an inverse
244/// conditional branch + an unconditional branch to the destination.
Matt Arsenault36919a42016-10-06 15:38:53 +0000245bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000246 DebugLoc DL = MI.getDebugLoc();
247 MachineBasicBlock *MBB = MI.getParent();
248 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
249 SmallVector<MachineOperand, 4> Cond;
250
251 bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
252 assert(!Fail && "branches to be relaxed must be analyzable");
253 (void)Fail;
Tim Northover3b0846e2014-05-24 12:50:23 +0000254
255 // Add an unconditional branch to the destination and invert the branch
256 // condition to jump over it:
257 // tbz L1
258 // =>
259 // tbnz L2
260 // b L1
261 // L2:
262
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000263 if (FBB && isBlockInRange(MI, *FBB)) {
264 // Last MI in the BB is an unconditional branch. We can simply invert the
265 // condition and swap destinations:
266 // beq L1
267 // b L2
268 // =>
269 // bne L2
270 // b L1
271 DEBUG(dbgs() << " Invert condition and swap "
272 "its destination with " << MBB->back());
Tim Northover3b0846e2014-05-24 12:50:23 +0000273
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000274 TII->reverseBranchCondition(Cond);
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000275 int OldSize = 0, NewSize = 0;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000276 TII->removeBranch(*MBB, &OldSize);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000277 TII->insertBranch(*MBB, FBB, TBB, Cond, DL, &NewSize);
Matt Arsenault5b549712016-08-02 08:30:06 +0000278
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000279 BlockInfo[MBB->getNumber()].Size += (NewSize - OldSize);
280 return true;
281 } else if (FBB) {
282 // We need to split the basic block here to obtain two long-range
283 // unconditional branches.
284 auto &NewBB = *MF->CreateMachineBasicBlock(MBB->getBasicBlock());
285 MF->insert(++MBB->getIterator(), &NewBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000286
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000287 // Insert an entry into BlockInfo to align it properly with the block
288 // numbers.
289 BlockInfo.insert(BlockInfo.begin() + NewBB.getNumber(), BasicBlockInfo());
Tim Northover3b0846e2014-05-24 12:50:23 +0000290
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000291 unsigned &NewBBSize = BlockInfo[NewBB.getNumber()].Size;
292 int NewBrSize;
293 TII->insertUnconditionalBranch(NewBB, FBB, DL, &NewBrSize);
294 NewBBSize += NewBrSize;
Tim Northover3b0846e2014-05-24 12:50:23 +0000295
296 // Update the successor lists according to the transformation to follow.
297 // Do it here since if there's no split, no update is needed.
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000298 MBB->replaceSuccessor(FBB, &NewBB);
299 NewBB.addSuccessor(FBB);
Tim Northover3b0846e2014-05-24 12:50:23 +0000300 }
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000301
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000302 // We now have an appropriate fall-through block in place (either naturally or
303 // just created), so we can invert the condition.
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000304 MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
Tim Northover3b0846e2014-05-24 12:50:23 +0000305
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000306 DEBUG(dbgs() << " Insert B to BB#" << TBB->getNumber()
Tim Northover3b0846e2014-05-24 12:50:23 +0000307 << ", invert condition and change dest. to BB#"
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000308 << NextBB.getNumber() << '\n');
Tim Northover3b0846e2014-05-24 12:50:23 +0000309
Matt Arsenault5b549712016-08-02 08:30:06 +0000310 unsigned &MBBSize = BlockInfo[MBB->getNumber()].Size;
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000311
Matt Arsenault5b549712016-08-02 08:30:06 +0000312 // Insert a new conditional branch and a new unconditional branch.
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000313 int RemovedSize = 0;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000314 TII->reverseBranchCondition(Cond);
315 TII->removeBranch(*MBB, &RemovedSize);
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000316 MBBSize -= RemovedSize;
Matt Arsenault5b549712016-08-02 08:30:06 +0000317
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000318 int AddedSize = 0;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000319 TII->insertBranch(*MBB, &NextBB, TBB, Cond, DL, &AddedSize);
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000320 MBBSize += AddedSize;
Tim Northover3b0846e2014-05-24 12:50:23 +0000321
322 // Finally, keep the block offsets up to date.
323 adjustBlockOffsets(*MBB);
324 return true;
325}
326
Matt Arsenault36919a42016-10-06 15:38:53 +0000327bool BranchRelaxation::relaxBranchInstructions() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000328 bool Changed = false;
329 // Relaxing branches involves creating new basic blocks, so re-eval
330 // end() for termination.
Matt Arsenaultf1c39062016-06-16 21:21:49 +0000331 for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
332 MachineBasicBlock &MBB = *I;
Matt Arsenaultf7065e12016-08-02 07:20:09 +0000333 MachineBasicBlock::iterator J = MBB.getFirstTerminator();
334 if (J == MBB.end())
335 continue;
336
Matt Arsenault36919a42016-10-06 15:38:53 +0000337
Matt Arsenault567631b2016-08-23 01:30:30 +0000338 MachineBasicBlock::iterator Next;
339 for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
340 J != MBB.end(); J = Next) {
341 Next = std::next(J);
342 MachineInstr &MI = *J;
343
344 if (MI.isConditionalBranch()) {
Matt Arsenault0a3ea892016-10-06 15:38:09 +0000345 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
Matt Arsenault567631b2016-08-23 01:30:30 +0000346 if (!isBlockInRange(MI, *DestBB)) {
347 if (Next != MBB.end() && Next->isConditionalBranch()) {
348 // If there are multiple conditional branches, this isn't an
349 // analyzable block. Split later terminators into a new block so
350 // each one will be analyzable.
351
352 MachineBasicBlock *NewBB = splitBlockBeforeInstr(*Next);
353 NewBB->transferSuccessors(&MBB);
354 MBB.addSuccessor(NewBB);
355 MBB.addSuccessor(DestBB);
356
357 // Cleanup potential unconditional branch to successor block.
358 NewBB->updateTerminator();
359 MBB.updateTerminator();
360 } else {
361 fixupConditionalBranch(MI);
362 ++NumConditionalRelaxed;
363 }
364
365 Changed = true;
366
367 // This may have modified all of the terminators, so start over.
368 Next = MBB.getFirstTerminator();
369 }
Matt Arsenault567631b2016-08-23 01:30:30 +0000370 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000371 }
372 }
Matt Arsenault567631b2016-08-23 01:30:30 +0000373
Tim Northover3b0846e2014-05-24 12:50:23 +0000374 return Changed;
375}
376
Matt Arsenault36919a42016-10-06 15:38:53 +0000377bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000378 MF = &mf;
379
Matt Arsenault36919a42016-10-06 15:38:53 +0000380 DEBUG(dbgs() << "***** BranchRelaxation *****\n");
Tim Northover3b0846e2014-05-24 12:50:23 +0000381
Matt Arsenault36919a42016-10-06 15:38:53 +0000382 TII = MF->getSubtarget().getInstrInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000383
384 // Renumber all of the machine basic blocks in the function, guaranteeing that
385 // the numbers agree with the position of the block in the function.
386 MF->RenumberBlocks();
387
388 // Do the initial scan of the function, building up information about the
389 // sizes of each block.
390 scanFunction();
391
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000392 DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs(););
Tim Northover3b0846e2014-05-24 12:50:23 +0000393
394 bool MadeChange = false;
395 while (relaxBranchInstructions())
396 MadeChange = true;
397
398 // After a while, this might be made debug-only, but it is not expensive.
399 verify();
400
Matt Arsenaulte8da1452016-08-02 08:06:17 +0000401 DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs());
Tim Northover3b0846e2014-05-24 12:50:23 +0000402
403 BlockInfo.clear();
404
405 return MadeChange;
406}