[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / lib / CodeGen / BranchRelaxation.cpp
blobeda0f37fdeb76e98ff70733cee144f3a4513641f
1 //===- BranchRelaxation.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/SmallVector.h"
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/CodeGen/LivePhysRegs.h"
12 #include "llvm/CodeGen/MachineBasicBlock.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/RegisterScavenging.h"
17 #include "llvm/CodeGen/TargetInstrInfo.h"
18 #include "llvm/CodeGen/TargetRegisterInfo.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/IR/DebugLoc.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <cassert>
30 #include <cstdint>
31 #include <iterator>
32 #include <memory>
34 using namespace llvm;
36 #define DEBUG_TYPE "branch-relaxation"
38 STATISTIC(NumSplit, "Number of basic blocks split");
39 STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
40 STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed");
42 #define BRANCH_RELAX_NAME "Branch relaxation pass"
44 namespace {
46 class BranchRelaxation : public MachineFunctionPass {
47 /// BasicBlockInfo - Information about the offset and size of a single
48 /// basic block.
49 struct BasicBlockInfo {
50 /// Offset - Distance from the beginning of the function to the beginning
51 /// of this basic block.
52 ///
53 /// The offset is always aligned as required by the basic block.
54 unsigned Offset = 0;
56 /// Size - Size of the basic block in bytes. If the block contains
57 /// inline assembly, this is a worst case estimate.
58 ///
59 /// The size does not include any alignment padding whether from the
60 /// beginning of the block, or from an aligned jump table at the end.
61 unsigned Size = 0;
63 BasicBlockInfo() = default;
65 /// Compute the offset immediately following this block. \p MBB is the next
66 /// block.
67 unsigned postOffset(const MachineBasicBlock &MBB) const {
68 const unsigned PO = Offset + Size;
69 const Align Alignment = MBB.getAlignment();
70 const Align ParentAlign = MBB.getParent()->getAlignment();
71 if (Alignment <= ParentAlign)
72 return alignTo(PO, Alignment);
74 // The alignment of this MBB is larger than the function's alignment, so we
75 // can't tell whether or not it will insert nops. Assume that it will.
76 return alignTo(PO, Alignment) + Alignment.value() - ParentAlign.value();
80 SmallVector<BasicBlockInfo, 16> BlockInfo;
81 std::unique_ptr<RegScavenger> RS;
82 LivePhysRegs LiveRegs;
84 MachineFunction *MF;
85 const TargetRegisterInfo *TRI;
86 const TargetInstrInfo *TII;
88 bool relaxBranchInstructions();
89 void scanFunction();
91 MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &BB);
93 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI,
94 MachineBasicBlock *DestBB);
95 void adjustBlockOffsets(MachineBasicBlock &Start);
96 bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
98 bool fixupConditionalBranch(MachineInstr &MI);
99 bool fixupUnconditionalBranch(MachineInstr &MI);
100 uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
101 unsigned getInstrOffset(const MachineInstr &MI) const;
102 void dumpBBs();
103 void verify();
105 public:
106 static char ID;
108 BranchRelaxation() : MachineFunctionPass(ID) {}
110 bool runOnMachineFunction(MachineFunction &MF) override;
112 StringRef getPassName() const override { return BRANCH_RELAX_NAME; }
115 } // end anonymous namespace
117 char BranchRelaxation::ID = 0;
119 char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
121 INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
123 /// verify - check BBOffsets, BBSizes, alignment of islands
124 void BranchRelaxation::verify() {
125 #ifndef NDEBUG
126 unsigned PrevNum = MF->begin()->getNumber();
127 for (MachineBasicBlock &MBB : *MF) {
128 const unsigned Num = MBB.getNumber();
129 assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
130 assert(BlockInfo[Num].Size == computeBlockSize(MBB));
131 PrevNum = Num;
133 #endif
136 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
137 /// print block size and offset information - debugging
138 LLVM_DUMP_METHOD void BranchRelaxation::dumpBBs() {
139 for (auto &MBB : *MF) {
140 const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
141 dbgs() << format("%%bb.%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
142 << format("size=%#x\n", BBI.Size);
145 #endif
147 /// scanFunction - Do the initial scan of the function, building up
148 /// information about each block.
149 void BranchRelaxation::scanFunction() {
150 BlockInfo.clear();
151 BlockInfo.resize(MF->getNumBlockIDs());
153 // First thing, compute the size of all basic blocks, and see if the function
154 // has any inline assembly in it. If so, we have to be conservative about
155 // alignment assumptions, as we don't know for sure the size of any
156 // instructions in the inline assembly.
157 for (MachineBasicBlock &MBB : *MF)
158 BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
160 // Compute block offsets and known bits.
161 adjustBlockOffsets(*MF->begin());
164 /// computeBlockSize - Compute the size for MBB.
165 uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
166 uint64_t Size = 0;
167 for (const MachineInstr &MI : MBB)
168 Size += TII->getInstSizeInBytes(MI);
169 return Size;
172 /// getInstrOffset - Return the current offset of the specified machine
173 /// instruction from the start of the function. This offset changes as stuff is
174 /// moved around inside the function.
175 unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
176 const MachineBasicBlock *MBB = MI.getParent();
178 // The offset is composed of two things: the sum of the sizes of all MBB's
179 // before this instruction's block, and the offset from the start of the block
180 // it is in.
181 unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
183 // Sum instructions before MI in MBB.
184 for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
185 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
186 Offset += TII->getInstSizeInBytes(*I);
189 return Offset;
192 void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
193 unsigned PrevNum = Start.getNumber();
194 for (auto &MBB :
195 make_range(std::next(MachineFunction::iterator(Start)), MF->end())) {
196 unsigned Num = MBB.getNumber();
197 // Get the offset and known bits at the end of the layout predecessor.
198 // Include the alignment of the current block.
199 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
201 PrevNum = Num;
205 /// Insert a new empty basic block and insert it after \BB
206 MachineBasicBlock *BranchRelaxation::createNewBlockAfter(MachineBasicBlock &BB) {
207 // Create a new MBB for the code after the OrigBB.
208 MachineBasicBlock *NewBB =
209 MF->CreateMachineBasicBlock(BB.getBasicBlock());
210 MF->insert(++BB.getIterator(), NewBB);
212 // Insert an entry into BlockInfo to align it properly with the block numbers.
213 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
215 return NewBB;
218 /// Split the basic block containing MI into two blocks, which are joined by
219 /// an unconditional branch. Update data structures and renumber blocks to
220 /// account for this change and returns the newly created block.
221 MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI,
222 MachineBasicBlock *DestBB) {
223 MachineBasicBlock *OrigBB = MI.getParent();
225 // Create a new MBB for the code after the OrigBB.
226 MachineBasicBlock *NewBB =
227 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
228 MF->insert(++OrigBB->getIterator(), NewBB);
230 // Splice the instructions starting with MI over to NewBB.
231 NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
233 // Add an unconditional branch from OrigBB to NewBB.
234 // Note the new unconditional branch is not being recorded.
235 // There doesn't seem to be meaningful DebugInfo available; this doesn't
236 // correspond to anything in the source.
237 TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
239 // Insert an entry into BlockInfo to align it properly with the block numbers.
240 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
242 NewBB->transferSuccessors(OrigBB);
243 OrigBB->addSuccessor(NewBB);
244 OrigBB->addSuccessor(DestBB);
246 // Cleanup potential unconditional branch to successor block.
247 // Note that updateTerminator may change the size of the blocks.
248 OrigBB->updateTerminator(NewBB);
250 // Figure out how large the OrigBB is. As the first half of the original
251 // block, it cannot contain a tablejump. The size includes
252 // the new jump we added. (It should be possible to do this without
253 // recounting everything, but it's very confusing, and this is rarely
254 // executed.)
255 BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
257 // Figure out how large the NewMBB is. As the second half of the original
258 // block, it may contain a tablejump.
259 BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
261 // All BBOffsets following these blocks must be modified.
262 adjustBlockOffsets(*OrigBB);
264 // Need to fix live-in lists if we track liveness.
265 if (TRI->trackLivenessAfterRegAlloc(*MF))
266 computeAndAddLiveIns(LiveRegs, *NewBB);
268 ++NumSplit;
270 return NewBB;
273 /// isBlockInRange - Returns true if the distance between specific MI and
274 /// specific BB can fit in MI's displacement field.
275 bool BranchRelaxation::isBlockInRange(
276 const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
277 int64_t BrOffset = getInstrOffset(MI);
278 int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
280 if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset))
281 return true;
283 LLVM_DEBUG(dbgs() << "Out of range branch to destination "
284 << printMBBReference(DestBB) << " from "
285 << printMBBReference(*MI.getParent()) << " to "
286 << DestOffset << " offset " << DestOffset - BrOffset << '\t'
287 << MI);
289 return false;
292 /// fixupConditionalBranch - Fix up a conditional branch whose destination is
293 /// too far away to fit in its displacement field. It is converted to an inverse
294 /// conditional branch + an unconditional branch to the destination.
295 bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
296 DebugLoc DL = MI.getDebugLoc();
297 MachineBasicBlock *MBB = MI.getParent();
298 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
299 MachineBasicBlock *NewBB = nullptr;
300 SmallVector<MachineOperand, 4> Cond;
302 auto insertUncondBranch = [&](MachineBasicBlock *MBB,
303 MachineBasicBlock *DestBB) {
304 unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
305 int NewBrSize = 0;
306 TII->insertUnconditionalBranch(*MBB, DestBB, DL, &NewBrSize);
307 BBSize += NewBrSize;
309 auto insertBranch = [&](MachineBasicBlock *MBB, MachineBasicBlock *TBB,
310 MachineBasicBlock *FBB,
311 SmallVectorImpl<MachineOperand>& Cond) {
312 unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
313 int NewBrSize = 0;
314 TII->insertBranch(*MBB, TBB, FBB, Cond, DL, &NewBrSize);
315 BBSize += NewBrSize;
317 auto removeBranch = [&](MachineBasicBlock *MBB) {
318 unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
319 int RemovedSize = 0;
320 TII->removeBranch(*MBB, &RemovedSize);
321 BBSize -= RemovedSize;
324 auto finalizeBlockChanges = [&](MachineBasicBlock *MBB,
325 MachineBasicBlock *NewBB) {
326 // Keep the block offsets up to date.
327 adjustBlockOffsets(*MBB);
329 // Need to fix live-in lists if we track liveness.
330 if (NewBB && TRI->trackLivenessAfterRegAlloc(*MF))
331 computeAndAddLiveIns(LiveRegs, *NewBB);
334 bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
335 assert(!Fail && "branches to be relaxed must be analyzable");
336 (void)Fail;
338 // Add an unconditional branch to the destination and invert the branch
339 // condition to jump over it:
340 // tbz L1
341 // =>
342 // tbnz L2
343 // b L1
344 // L2:
346 bool ReversedCond = !TII->reverseBranchCondition(Cond);
347 if (ReversedCond) {
348 if (FBB && isBlockInRange(MI, *FBB)) {
349 // Last MI in the BB is an unconditional branch. We can simply invert the
350 // condition and swap destinations:
351 // beq L1
352 // b L2
353 // =>
354 // bne L2
355 // b L1
356 LLVM_DEBUG(dbgs() << " Invert condition and swap "
357 "its destination with "
358 << MBB->back());
360 removeBranch(MBB);
361 insertBranch(MBB, FBB, TBB, Cond);
362 finalizeBlockChanges(MBB, nullptr);
363 return true;
365 if (FBB) {
366 // We need to split the basic block here to obtain two long-range
367 // unconditional branches.
368 NewBB = createNewBlockAfter(*MBB);
370 insertUncondBranch(NewBB, FBB);
371 // Update the succesor lists according to the transformation to follow.
372 // Do it here since if there's no split, no update is needed.
373 MBB->replaceSuccessor(FBB, NewBB);
374 NewBB->addSuccessor(FBB);
377 // We now have an appropriate fall-through block in place (either naturally or
378 // just created), so we can use the inverted the condition.
379 MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
381 LLVM_DEBUG(dbgs() << " Insert B to " << printMBBReference(*TBB)
382 << ", invert condition and change dest. to "
383 << printMBBReference(NextBB) << '\n');
385 removeBranch(MBB);
386 // Insert a new conditional branch and a new unconditional branch.
387 insertBranch(MBB, &NextBB, TBB, Cond);
389 finalizeBlockChanges(MBB, NewBB);
390 return true;
392 // Branch cond can't be inverted.
393 // In this case we always add a block after the MBB.
394 LLVM_DEBUG(dbgs() << " The branch condition can't be inverted. "
395 << " Insert a new BB after " << MBB->back());
397 if (!FBB)
398 FBB = &(*std::next(MachineFunction::iterator(MBB)));
400 // This is the block with cond. branch and the distance to TBB is too long.
401 // beq L1
402 // L2:
404 // We do the following transformation:
405 // beq NewBB
406 // b L2
407 // NewBB:
408 // b L1
409 // L2:
411 NewBB = createNewBlockAfter(*MBB);
412 insertUncondBranch(NewBB, TBB);
414 LLVM_DEBUG(dbgs() << " Insert cond B to the new BB "
415 << printMBBReference(*NewBB)
416 << " Keep the exiting condition.\n"
417 << " Insert B to " << printMBBReference(*FBB) << ".\n"
418 << " In the new BB: Insert B to "
419 << printMBBReference(*TBB) << ".\n");
421 // Update the successor lists according to the transformation to follow.
422 MBB->replaceSuccessor(TBB, NewBB);
423 NewBB->addSuccessor(TBB);
425 // Replace branch in the current (MBB) block.
426 removeBranch(MBB);
427 insertBranch(MBB, NewBB, FBB, Cond);
429 finalizeBlockChanges(MBB, NewBB);
430 return true;
433 bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
434 MachineBasicBlock *MBB = MI.getParent();
436 unsigned OldBrSize = TII->getInstSizeInBytes(MI);
437 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
439 int64_t DestOffset = BlockInfo[DestBB->getNumber()].Offset;
440 int64_t SrcOffset = getInstrOffset(MI);
442 assert(!TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - SrcOffset));
444 BlockInfo[MBB->getNumber()].Size -= OldBrSize;
446 MachineBasicBlock *BranchBB = MBB;
448 // If this was an expanded conditional branch, there is already a single
449 // unconditional branch in a block.
450 if (!MBB->empty()) {
451 BranchBB = createNewBlockAfter(*MBB);
453 // Add live outs.
454 for (const MachineBasicBlock *Succ : MBB->successors()) {
455 for (const MachineBasicBlock::RegisterMaskPair &LiveIn : Succ->liveins())
456 BranchBB->addLiveIn(LiveIn);
459 BranchBB->sortUniqueLiveIns();
460 BranchBB->addSuccessor(DestBB);
461 MBB->replaceSuccessor(DestBB, BranchBB);
464 DebugLoc DL = MI.getDebugLoc();
465 MI.eraseFromParent();
467 // Create the optional restore block and, initially, place it at the end of
468 // function. That block will be placed later if it's used; otherwise, it will
469 // be erased.
470 MachineBasicBlock *RestoreBB = createNewBlockAfter(MF->back());
472 TII->insertIndirectBranch(*BranchBB, *DestBB, *RestoreBB, DL,
473 DestOffset - SrcOffset, RS.get());
475 BlockInfo[BranchBB->getNumber()].Size = computeBlockSize(*BranchBB);
476 adjustBlockOffsets(*MBB);
478 // If RestoreBB is required, try to place just before DestBB.
479 if (!RestoreBB->empty()) {
480 // TODO: For multiple far branches to the same destination, there are
481 // chances that some restore blocks could be shared if they clobber the
482 // same registers and share the same restore sequence. So far, those
483 // restore blocks are just duplicated for each far branch.
484 assert(!DestBB->isEntryBlock());
485 MachineBasicBlock *PrevBB = &*std::prev(DestBB->getIterator());
486 if (auto *FT = PrevBB->getFallThrough()) {
487 assert(FT == DestBB);
488 TII->insertUnconditionalBranch(*PrevBB, FT, DebugLoc());
489 // Recalculate the block size.
490 BlockInfo[PrevBB->getNumber()].Size = computeBlockSize(*PrevBB);
492 // Now, RestoreBB could be placed directly before DestBB.
493 MF->splice(DestBB->getIterator(), RestoreBB->getIterator());
494 // Update successors and predecessors.
495 RestoreBB->addSuccessor(DestBB);
496 BranchBB->replaceSuccessor(DestBB, RestoreBB);
497 if (TRI->trackLivenessAfterRegAlloc(*MF))
498 computeAndAddLiveIns(LiveRegs, *RestoreBB);
499 // Compute the restore block size.
500 BlockInfo[RestoreBB->getNumber()].Size = computeBlockSize(*RestoreBB);
501 // Update the offset starting from the previous block.
502 adjustBlockOffsets(*PrevBB);
503 } else {
504 // Remove restore block if it's not required.
505 MF->erase(RestoreBB);
508 return true;
511 bool BranchRelaxation::relaxBranchInstructions() {
512 bool Changed = false;
514 // Relaxing branches involves creating new basic blocks, so re-eval
515 // end() for termination.
516 for (MachineBasicBlock &MBB : *MF) {
517 // Empty block?
518 MachineBasicBlock::iterator Last = MBB.getLastNonDebugInstr();
519 if (Last == MBB.end())
520 continue;
522 // Expand the unconditional branch first if necessary. If there is a
523 // conditional branch, this will end up changing the branch destination of
524 // it to be over the newly inserted indirect branch block, which may avoid
525 // the need to try expanding the conditional branch first, saving an extra
526 // jump.
527 if (Last->isUnconditionalBranch()) {
528 // Unconditional branch destination might be unanalyzable, assume these
529 // are OK.
530 if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(*Last)) {
531 if (!isBlockInRange(*Last, *DestBB)) {
532 fixupUnconditionalBranch(*Last);
533 ++NumUnconditionalRelaxed;
534 Changed = true;
539 // Loop over the conditional branches.
540 MachineBasicBlock::iterator Next;
541 for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
542 J != MBB.end(); J = Next) {
543 Next = std::next(J);
544 MachineInstr &MI = *J;
546 if (!MI.isConditionalBranch())
547 continue;
549 if (MI.getOpcode() == TargetOpcode::FAULTING_OP)
550 // FAULTING_OP's destination is not encoded in the instruction stream
551 // and thus never needs relaxed.
552 continue;
554 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
555 if (!isBlockInRange(MI, *DestBB)) {
556 if (Next != MBB.end() && Next->isConditionalBranch()) {
557 // If there are multiple conditional branches, this isn't an
558 // analyzable block. Split later terminators into a new block so
559 // each one will be analyzable.
561 splitBlockBeforeInstr(*Next, DestBB);
562 } else {
563 fixupConditionalBranch(MI);
564 ++NumConditionalRelaxed;
567 Changed = true;
569 // This may have modified all of the terminators, so start over.
570 Next = MBB.getFirstTerminator();
575 return Changed;
578 bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
579 MF = &mf;
581 LLVM_DEBUG(dbgs() << "***** BranchRelaxation *****\n");
583 const TargetSubtargetInfo &ST = MF->getSubtarget();
584 TII = ST.getInstrInfo();
586 TRI = ST.getRegisterInfo();
587 if (TRI->trackLivenessAfterRegAlloc(*MF))
588 RS.reset(new RegScavenger());
590 // Renumber all of the machine basic blocks in the function, guaranteeing that
591 // the numbers agree with the position of the block in the function.
592 MF->RenumberBlocks();
594 // Do the initial scan of the function, building up information about the
595 // sizes of each block.
596 scanFunction();
598 LLVM_DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs(););
600 bool MadeChange = false;
601 while (relaxBranchInstructions())
602 MadeChange = true;
604 // After a while, this might be made debug-only, but it is not expensive.
605 verify();
607 LLVM_DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs());
609 BlockInfo.clear();
611 return MadeChange;