LLVM 23.0.0git
BranchFolding.cpp
Go to the documentation of this file.
1//===- BranchFolding.cpp - Fold machine code branch instructions ----------===//
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//===----------------------------------------------------------------------===//
8//
9// This pass forwards branches to unconditional branches to make them branch
10// directly to the target block. This pass often results in dead MBB's, which
11// it then removes.
12//
13// Note that this pass must be run after register allocation, it cannot handle
14// SSA form. It also must handle virtual registers for targets that emit virtual
15// ISA (e.g. NVPTX).
16//
17//===----------------------------------------------------------------------===//
18
19#include "BranchFolding.h"
20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/Statistic.h"
47#include "llvm/Config/llvm-config.h"
49#include "llvm/IR/DebugLoc.h"
50#include "llvm/IR/Function.h"
52#include "llvm/MC/LaneBitmask.h"
54#include "llvm/Pass.h"
58#include "llvm/Support/Debug.h"
62#include <cassert>
63#include <cstddef>
64#include <iterator>
65#include <numeric>
66
67using namespace llvm;
68
69#define DEBUG_TYPE "branch-folder"
70
71STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
72STATISTIC(NumBranchOpts, "Number of branches optimized");
73STATISTIC(NumTailMerge , "Number of block tails merged");
74STATISTIC(NumHoist , "Number of times common instructions are hoisted");
75STATISTIC(NumTailCalls, "Number of tail calls optimized");
76
78 FlagEnableTailMerge("enable-tail-merge",
80
81// Throttle for huge numbers of predecessors (compile speed problems)
83TailMergeThreshold("tail-merge-threshold",
84 cl::desc("Max number of predecessors to consider tail merging"),
85 cl::init(150), cl::Hidden);
86
87// Heuristic for tail merging (and, inversely, tail duplication).
89TailMergeSize("tail-merge-size",
90 cl::desc("Min number of instructions to consider tail merging"),
92
93namespace {
94
95 /// BranchFolderPass - Wrap branch folder in a machine function pass.
96class BranchFolderLegacy : public MachineFunctionPass {
97public:
98 static char ID;
99
100 explicit BranchFolderLegacy() : MachineFunctionPass(ID) {}
101
102 bool runOnMachineFunction(MachineFunction &MF) override;
103
104 void getAnalysisUsage(AnalysisUsage &AU) const override {
105 AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
106 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
107 AU.addRequired<ProfileSummaryInfoWrapperPass>();
108 AU.addRequired<TargetPassConfig>();
110 }
111
112 MachineFunctionProperties getRequiredProperties() const override {
113 return MachineFunctionProperties().setNoPHIs();
114 }
115};
116
117} // end anonymous namespace
118
119char BranchFolderLegacy::ID = 0;
120
121char &llvm::BranchFolderPassID = BranchFolderLegacy::ID;
122
123INITIALIZE_PASS(BranchFolderLegacy, DEBUG_TYPE, "Control Flow Optimizer", false,
124 false)
125
128 MFPropsModifier _(*this, MF);
129 bool EnableTailMerge =
130 !MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge;
131
132 auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
133 auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
134 .getCachedResult<ProfileSummaryAnalysis>(
135 *MF.getFunction().getParent());
136 if (!PSI)
138 "ProfileSummaryAnalysis is required for BranchFoldingPass", false);
139
140 auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
141 MBFIWrapper MBBFreqInfo(MBFI);
142 BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, MBPI,
143 PSI);
144 if (Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(),
145 MF.getSubtarget().getRegisterInfo()))
147
148 return PreservedAnalyses::all();
149}
150
151bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
152 if (skipFunction(MF.getFunction()))
153 return false;
154
155 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
156 // TailMerge can create jump into if branches that make CFG irreducible for
157 // HW that requires structurized CFG.
158 bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
159 PassConfig->getEnableTailMerge();
160 MBFIWrapper MBBFreqInfo(
161 getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
162 BranchFolder Folder(
163 EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo,
164 getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(),
165 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI());
166 return Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(),
168}
169
170BranchFolder::BranchFolder(bool DefaultEnableTailMerge, bool CommonHoist,
171 MBFIWrapper &FreqInfo,
172 const MachineBranchProbabilityInfo &ProbInfo,
173 ProfileSummaryInfo *PSI, unsigned MinTailLength)
174 : EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength),
175 MBBFreqInfo(FreqInfo), MBPI(ProbInfo), PSI(PSI) {
176 switch (FlagEnableTailMerge) {
178 EnableTailMerge = DefaultEnableTailMerge;
179 break;
181 EnableTailMerge = true;
182 break;
184 EnableTailMerge = false;
185 break;
186 }
187}
188
189void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
190 assert(MBB->pred_empty() && "MBB must be dead!");
191 LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
192
193 MachineFunction *MF = MBB->getParent();
194 // drop all successors.
195 while (!MBB->succ_empty())
196 MBB->removeSuccessor(MBB->succ_end()-1);
197
198 // Avoid matching if this pointer gets reused.
199 TriedMerging.erase(MBB);
200
201 // Update call info.
202 for (const MachineInstr &MI : *MBB)
203 if (MI.shouldUpdateAdditionalCallInfo())
205
206 // Remove the block.
207 if (MLI)
208 MLI->removeBlock(MBB);
209 MF->erase(MBB);
210 EHScopeMembership.erase(MBB);
211}
212
214 const TargetInstrInfo *tii,
215 const TargetRegisterInfo *tri,
216 MachineLoopInfo *mli, bool AfterPlacement) {
217 if (!tii) return false;
218
219 TriedMerging.clear();
220
222 AfterBlockPlacement = AfterPlacement;
223 TII = tii;
224 TRI = tri;
225 MLI = mli;
226 this->MRI = &MRI;
227
228 if (MinCommonTailLength == 0) {
229 MinCommonTailLength = TailMergeSize.getNumOccurrences() > 0
231 : TII->getTailMergeSize(MF);
232 }
233
234 UpdateLiveIns = MRI.tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF);
235 if (!UpdateLiveIns)
236 MRI.invalidateLiveness();
237
238 bool MadeChange = false;
239
240 // Recalculate EH scope membership.
241 EHScopeMembership = getEHScopeMembership(MF);
242
243 bool MadeChangeThisIteration = true;
244 while (MadeChangeThisIteration) {
245 MadeChangeThisIteration = TailMergeBlocks(MF);
246 // No need to clean up if tail merging does not change anything after the
247 // block placement.
248 if (!AfterBlockPlacement || MadeChangeThisIteration)
249 MadeChangeThisIteration |= OptimizeBranches(MF);
250 if (EnableHoistCommonCode)
251 MadeChangeThisIteration |= HoistCommonCode(MF);
252 MadeChange |= MadeChangeThisIteration;
253 }
254
255 // See if any jump tables have become dead as the code generator
256 // did its thing.
258 if (!JTI)
259 return MadeChange;
260
261 // Walk the function to find jump tables that are live.
262 BitVector JTIsLive(JTI->getJumpTables().size());
263 for (const MachineBasicBlock &BB : MF) {
264 for (const MachineInstr &I : BB)
265 for (const MachineOperand &Op : I.operands()) {
266 if (!Op.isJTI()) continue;
267
268 // Remember that this JT is live.
269 JTIsLive.set(Op.getIndex());
270 }
271 }
272
273 // Finally, remove dead jump tables. This happens when the
274 // indirect jump was unreachable (and thus deleted).
275 for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i)
276 if (!JTIsLive.test(i)) {
277 JTI->RemoveJumpTable(i);
278 MadeChange = true;
279 }
280
281 return MadeChange;
282}
283
284//===----------------------------------------------------------------------===//
285// Tail Merging of Blocks
286//===----------------------------------------------------------------------===//
287
288/// HashMachineInstr - Compute a hash value for MI and its operands.
289static unsigned HashMachineInstr(const MachineInstr &MI) {
290 unsigned Hash = MI.getOpcode();
291 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
292 const MachineOperand &Op = MI.getOperand(i);
293
294 // Merge in bits from the operand if easy. We can't use MachineOperand's
295 // hash_code here because it's not deterministic and we sort by hash value
296 // later.
297 unsigned OperandHash = 0;
298 switch (Op.getType()) {
300 OperandHash = Op.getReg().id();
301 break;
303 OperandHash = Op.getImm();
304 break;
306 OperandHash = Op.getMBB()->getNumber();
307 break;
311 OperandHash = Op.getIndex();
312 break;
315 // Global address / external symbol are too hard, don't bother, but do
316 // pull in the offset.
317 OperandHash = Op.getOffset();
318 break;
319 default:
320 break;
321 }
322
323 Hash += ((OperandHash << 3) | Op.getType()) << (i & 31);
324 }
325 return Hash;
326}
327
328/// HashEndOfMBB - Hash the last instruction in the MBB.
329static unsigned HashEndOfMBB(const MachineBasicBlock &MBB) {
330 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr(false);
331 if (I == MBB.end())
332 return 0;
333
334 return HashMachineInstr(*I);
335}
336
337/// Whether MI should be counted as an instruction when calculating common tail.
339 return !(MI.isDebugInstr() || MI.isCFIInstruction());
340}
341
342/// Iterate backwards from the given iterator \p I, towards the beginning of the
343/// block. If a MI satisfying 'countsAsInstruction' is found, return an iterator
344/// pointing to that MI. If no such MI is found, return the end iterator.
348 while (I != MBB->begin()) {
349 --I;
351 return I;
352 }
353 return MBB->end();
354}
355
356/// Given two machine basic blocks, return the number of instructions they
357/// actually have in common together at their end. If a common tail is found (at
358/// least by one instruction), then iterators for the first shared instruction
359/// in each block are returned as well.
360///
361/// Non-instructions according to countsAsInstruction are ignored.
363 MachineBasicBlock *MBB2,
366 MachineBasicBlock::iterator MBBI1 = MBB1->end();
367 MachineBasicBlock::iterator MBBI2 = MBB2->end();
368
369 unsigned TailLen = 0;
370 while (true) {
371 MBBI1 = skipBackwardPastNonInstructions(MBBI1, MBB1);
372 MBBI2 = skipBackwardPastNonInstructions(MBBI2, MBB2);
373 if (MBBI1 == MBB1->end() || MBBI2 == MBB2->end())
374 break;
375 if (!MBBI1->isIdenticalTo(*MBBI2) ||
376 // FIXME: This check is dubious. It's used to get around a problem where
377 // people incorrectly expect inline asm directives to remain in the same
378 // relative order. This is untenable because normal compiler
379 // optimizations (like this one) may reorder and/or merge these
380 // directives.
381 MBBI1->isInlineAsm()) {
382 break;
383 }
384 if (MBBI1->getFlag(MachineInstr::NoMerge) ||
385 MBBI2->getFlag(MachineInstr::NoMerge))
386 break;
387 ++TailLen;
388 I1 = MBBI1;
389 I2 = MBBI2;
390 }
391
392 return TailLen;
393}
394
395void BranchFolder::replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
396 MachineBasicBlock &NewDest) {
397 if (UpdateLiveIns) {
398 // OldInst should always point to an instruction.
399 MachineBasicBlock &OldMBB = *OldInst->getParent();
400 LiveRegs.clear();
401 LiveRegs.addLiveOuts(OldMBB);
402 // Move backward to the place where will insert the jump.
404 do {
405 --I;
406 LiveRegs.stepBackward(*I);
407 } while (I != OldInst);
408
409 // Merging the tails may have switched some undef operand to non-undef ones.
410 // Add IMPLICIT_DEFS into OldMBB as necessary to have a definition of the
411 // register.
412 for (MachineBasicBlock::RegisterMaskPair P : NewDest.liveins()) {
413 // We computed the liveins with computeLiveIn earlier and should only see
414 // full registers:
415 assert(P.LaneMask == LaneBitmask::getAll() &&
416 "Can only handle full register.");
417 MCRegister Reg = P.PhysReg;
418 if (!LiveRegs.available(*MRI, Reg))
419 continue;
420 DebugLoc DL;
421 BuildMI(OldMBB, OldInst, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Reg);
422 }
423 }
424
425 TII->ReplaceTailWithBranchTo(OldInst, &NewDest);
426 ++NumTailMerge;
427}
428
429MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
431 const BasicBlock *BB) {
432 if (!TII->isLegalToSplitMBBAt(CurMBB, BBI1))
433 return nullptr;
434
435 MachineFunction &MF = *CurMBB.getParent();
436
437 // Create the fall-through block.
439 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(BB);
440 CurMBB.getParent()->insert(++MBBI, NewMBB);
441
442 // Move all the successors of this block to the specified block.
443 NewMBB->transferSuccessors(&CurMBB);
444
445 // Add an edge from CurMBB to NewMBB for the fall-through.
446 CurMBB.addSuccessor(NewMBB);
447
448 // Splice the code over.
449 NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end());
450
451 // NewMBB belongs to the same loop as CurMBB.
452 if (MLI)
453 if (MachineLoop *ML = MLI->getLoopFor(&CurMBB))
454 ML->addBasicBlockToLoop(NewMBB, *MLI);
455
456 // NewMBB inherits CurMBB's block frequency.
457 MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB));
458
459 if (UpdateLiveIns)
460 computeAndAddLiveIns(LiveRegs, *NewMBB);
461
462 // Add the new block to the EH scope.
463 const auto &EHScopeI = EHScopeMembership.find(&CurMBB);
464 if (EHScopeI != EHScopeMembership.end()) {
465 auto n = EHScopeI->second;
466 EHScopeMembership[NewMBB] = n;
467 }
468
469 return NewMBB;
470}
471
472/// EstimateRuntime - Make a rough estimate for how long it will take to run
473/// the specified code.
476 unsigned Time = 0;
477 for (; I != E; ++I) {
478 if (!countsAsInstruction(*I))
479 continue;
480 if (I->isCall())
481 Time += 10;
482 else if (I->mayLoadOrStore())
483 Time += 2;
484 else
485 ++Time;
486 }
487 return Time;
488}
489
490// CurMBB needs to add an unconditional branch to SuccMBB (we removed these
491// branches temporarily for tail merging). In the case where CurMBB ends
492// with a conditional branch to the next block, optimize by reversing the
493// test and conditionally branching to SuccMBB instead.
494static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB,
495 const TargetInstrInfo *TII, const DebugLoc &BranchDL) {
496 MachineFunction *MF = CurMBB->getParent();
498 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
500 DebugLoc dl = CurMBB->findBranchDebugLoc();
501 if (!dl)
502 dl = BranchDL;
503 if (I != MF->end() && !TII->analyzeBranch(*CurMBB, TBB, FBB, Cond, true)) {
504 MachineBasicBlock *NextBB = &*I;
505 if (TBB == NextBB && !Cond.empty() && !FBB) {
506 if (!TII->reverseBranchCondition(Cond)) {
507 TII->removeBranch(*CurMBB);
508 TII->insertBranch(*CurMBB, SuccBB, nullptr, Cond, dl);
509 return;
510 }
511 }
512 }
513 TII->insertBranch(*CurMBB, SuccBB, nullptr,
515}
516
517bool
518BranchFolder::MergePotentialsElt::operator<(const MergePotentialsElt &o) const {
519 if (getHash() < o.getHash())
520 return true;
521 if (getHash() > o.getHash())
522 return false;
523 if (getBlock()->getNumber() < o.getBlock()->getNumber())
524 return true;
525 if (getBlock()->getNumber() > o.getBlock()->getNumber())
526 return false;
527 return false;
528}
529
530/// CountTerminators - Count the number of terminators in the given
531/// block and set I to the position of the first non-terminator, if there
532/// is one, or MBB->end() otherwise.
535 I = MBB->end();
536 unsigned NumTerms = 0;
537 while (true) {
538 if (I == MBB->begin()) {
539 I = MBB->end();
540 break;
541 }
542 --I;
543 if (!I->isTerminator()) break;
544 ++NumTerms;
545 }
546 return NumTerms;
547}
548
549/// A no successor, non-return block probably ends in unreachable and is cold.
550/// Also consider a block that ends in an indirect branch to be a return block,
551/// since many targets use plain indirect branches to return.
553 if (!MBB->succ_empty())
554 return false;
555 if (MBB->empty())
556 return true;
557 return !(MBB->back().isReturn() || MBB->back().isIndirectBranch());
558}
559
560/// ProfitableToMerge - Check if two machine basic blocks have a common tail
561/// and decide if it would be profitable to merge those tails. Return the
562/// length of the common tail and iterators to the first common instruction
563/// in each block.
564/// MBB1, MBB2 The blocks to check
565/// MinCommonTailLength Minimum size of tail block to be merged.
566/// CommonTailLen Out parameter to record the size of the shared tail between
567/// MBB1 and MBB2
568/// I1, I2 Iterator references that will be changed to point to the first
569/// instruction in the common tail shared by MBB1,MBB2
570/// SuccBB A common successor of MBB1, MBB2 which are in a canonical form
571/// relative to SuccBB
572/// PredBB The layout predecessor of SuccBB, if any.
573/// EHScopeMembership map from block to EH scope #.
574/// AfterPlacement True if we are merging blocks after layout. Stricter
575/// thresholds apply to prevent undoing tail-duplication.
576static bool
578 unsigned MinCommonTailLength, unsigned &CommonTailLen,
581 MachineBasicBlock *PredBB,
583 bool AfterPlacement,
584 MBFIWrapper &MBBFreqInfo,
585 ProfileSummaryInfo *PSI) {
586 // It is never profitable to tail-merge blocks from two different EH scopes.
587 if (!EHScopeMembership.empty()) {
588 auto EHScope1 = EHScopeMembership.find(MBB1);
589 assert(EHScope1 != EHScopeMembership.end());
590 auto EHScope2 = EHScopeMembership.find(MBB2);
591 assert(EHScope2 != EHScopeMembership.end());
592 if (EHScope1->second != EHScope2->second)
593 return false;
594 }
595
596 CommonTailLen = ComputeCommonTailLength(MBB1, MBB2, I1, I2);
597 if (CommonTailLen == 0)
598 return false;
599 LLVM_DEBUG(dbgs() << "Common tail length of " << printMBBReference(*MBB1)
600 << " and " << printMBBReference(*MBB2) << " is "
601 << CommonTailLen << '\n');
602
603 // Move the iterators to the beginning of the MBB if we only got debug
604 // instructions before the tail. This is to avoid splitting a block when we
605 // only got debug instructions before the tail (to be invariant on -g).
606 if (skipDebugInstructionsForward(MBB1->begin(), MBB1->end(), false) == I1)
607 I1 = MBB1->begin();
608 if (skipDebugInstructionsForward(MBB2->begin(), MBB2->end(), false) == I2)
609 I2 = MBB2->begin();
610
611 bool FullBlockTail1 = I1 == MBB1->begin();
612 bool FullBlockTail2 = I2 == MBB2->begin();
613
614 // It's almost always profitable to merge any number of non-terminator
615 // instructions with the block that falls through into the common successor.
616 // This is true only for a single successor. For multiple successors, we are
617 // trading a conditional branch for an unconditional one.
618 // TODO: Re-visit successor size for non-layout tail merging.
619 if ((MBB1 == PredBB || MBB2 == PredBB) &&
620 (!AfterPlacement || MBB1->succ_size() == 1)) {
622 unsigned NumTerms = CountTerminators(MBB1 == PredBB ? MBB2 : MBB1, I);
623 if (CommonTailLen > NumTerms)
624 return true;
625 }
626
627 // If these are identical non-return blocks with no successors, merge them.
628 // Such blocks are typically cold calls to noreturn functions like abort, and
629 // are unlikely to become a fallthrough target after machine block placement.
630 // Tail merging these blocks is unlikely to create additional unconditional
631 // branches, and will reduce the size of this cold code.
632 if (FullBlockTail1 && FullBlockTail2 &&
634 return true;
635
636 // If one of the blocks can be completely merged and happens to be in
637 // a position where the other could fall through into it, merge any number
638 // of instructions, because it can be done without a branch.
639 // TODO: If the blocks are not adjacent, move one of them so that they are?
640 if (MBB1->isLayoutSuccessor(MBB2) && FullBlockTail2)
641 return true;
642 if (MBB2->isLayoutSuccessor(MBB1) && FullBlockTail1)
643 return true;
644
645 // If both blocks are identical and end in a branch, merge them unless they
646 // both have a fallthrough predecessor and successor.
647 // We can only do this after block placement because it depends on whether
648 // there are fallthroughs, and we don't know until after layout.
649 if (AfterPlacement && FullBlockTail1 && FullBlockTail2) {
650 auto BothFallThrough = [](MachineBasicBlock *MBB) {
651 if (!MBB->succ_empty() && !MBB->canFallThrough())
652 return false;
654 MachineFunction *MF = MBB->getParent();
655 return (MBB != &*MF->begin()) && std::prev(I)->canFallThrough();
656 };
657 if (!BothFallThrough(MBB1) || !BothFallThrough(MBB2))
658 return true;
659 }
660
661 // If both blocks have an unconditional branch temporarily stripped out,
662 // count that as an additional common instruction for the following
663 // heuristics. This heuristic is only accurate for single-succ blocks, so to
664 // make sure that during layout merging and duplicating don't crash, we check
665 // for that when merging during layout.
666 unsigned EffectiveTailLen = CommonTailLen;
667 if (SuccBB && MBB1 != PredBB && MBB2 != PredBB &&
668 (MBB1->succ_size() == 1 || !AfterPlacement) &&
669 !MBB1->back().isBarrier() &&
670 !MBB2->back().isBarrier())
671 ++EffectiveTailLen;
672
673 // Check if the common tail is long enough to be worthwhile.
674 if (EffectiveTailLen >= MinCommonTailLength)
675 return true;
676
677 // If we are optimizing for code size, 2 instructions in common is enough if
678 // we don't have to split a block. At worst we will be introducing 1 new
679 // branch instruction, which is likely to be smaller than the 2
680 // instructions that would be deleted in the merge.
681 bool OptForSize = llvm::shouldOptimizeForSize(MBB1, PSI, &MBBFreqInfo) &&
682 llvm::shouldOptimizeForSize(MBB2, PSI, &MBBFreqInfo);
683 return EffectiveTailLen >= 2 && OptForSize &&
684 (FullBlockTail1 || FullBlockTail2);
685}
686
687unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
688 unsigned MinCommonTailLength,
689 MachineBasicBlock *SuccBB,
690 MachineBasicBlock *PredBB) {
691 unsigned maxCommonTailLength = 0U;
692 SameTails.clear();
693 MachineBasicBlock::iterator TrialBBI1, TrialBBI2;
694 MPIterator HighestMPIter = std::prev(MergePotentials.end());
695 for (MPIterator CurMPIter = std::prev(MergePotentials.end()),
696 B = MergePotentials.begin();
697 CurMPIter != B && CurMPIter->getHash() == CurHash; --CurMPIter) {
698 for (MPIterator I = std::prev(CurMPIter); I->getHash() == CurHash; --I) {
699 unsigned CommonTailLen;
700 if (ProfitableToMerge(CurMPIter->getBlock(), I->getBlock(),
701 MinCommonTailLength,
702 CommonTailLen, TrialBBI1, TrialBBI2,
703 SuccBB, PredBB,
704 EHScopeMembership,
705 AfterBlockPlacement, MBBFreqInfo, PSI)) {
706 if (CommonTailLen > maxCommonTailLength) {
707 SameTails.clear();
708 maxCommonTailLength = CommonTailLen;
709 HighestMPIter = CurMPIter;
710 SameTails.push_back(SameTailElt(CurMPIter, TrialBBI1));
711 }
712 if (HighestMPIter == CurMPIter &&
713 CommonTailLen == maxCommonTailLength)
714 SameTails.push_back(SameTailElt(I, TrialBBI2));
715 }
716 if (I == B)
717 break;
718 }
719 }
720 return maxCommonTailLength;
721}
722
723void BranchFolder::RemoveBlocksWithHash(unsigned CurHash,
724 MachineBasicBlock *SuccBB,
725 MachineBasicBlock *PredBB,
726 const DebugLoc &BranchDL) {
727 MPIterator CurMPIter, B;
728 for (CurMPIter = std::prev(MergePotentials.end()),
729 B = MergePotentials.begin();
730 CurMPIter->getHash() == CurHash; --CurMPIter) {
731 // Put the unconditional branch back, if we need one.
732 MachineBasicBlock *CurMBB = CurMPIter->getBlock();
733 if (SuccBB && CurMBB != PredBB)
734 FixTail(CurMBB, SuccBB, TII, BranchDL);
735 if (CurMPIter == B)
736 break;
737 }
738 if (CurMPIter->getHash() != CurHash)
739 CurMPIter++;
740 MergePotentials.erase(CurMPIter, MergePotentials.end());
741}
742
743bool BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
744 MachineBasicBlock *SuccBB,
745 unsigned maxCommonTailLength,
746 unsigned &commonTailIndex) {
747 commonTailIndex = 0;
748 unsigned TimeEstimate = ~0U;
749 for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
750 // Use PredBB if possible; that doesn't require a new branch.
751 if (SameTails[i].getBlock() == PredBB) {
752 commonTailIndex = i;
753 break;
754 }
755 // Otherwise, make a (fairly bogus) choice based on estimate of
756 // how long it will take the various blocks to execute.
757 unsigned t = EstimateRuntime(SameTails[i].getBlock()->begin(),
758 SameTails[i].getTailStartPos());
759 if (t <= TimeEstimate) {
760 TimeEstimate = t;
761 commonTailIndex = i;
762 }
763 }
764
766 SameTails[commonTailIndex].getTailStartPos();
767 MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
768
769 LLVM_DEBUG(dbgs() << "\nSplitting " << printMBBReference(*MBB) << ", size "
770 << maxCommonTailLength);
771
772 // If the split block unconditionally falls-thru to SuccBB, it will be
773 // merged. In control flow terms it should then take SuccBB's name. e.g. If
774 // SuccBB is an inner loop, the common tail is still part of the inner loop.
775 const BasicBlock *BB = (SuccBB && MBB->succ_size() == 1) ?
776 SuccBB->getBasicBlock() : MBB->getBasicBlock();
777 MachineBasicBlock *newMBB = SplitMBBAt(*MBB, BBI, BB);
778 if (!newMBB) {
779 LLVM_DEBUG(dbgs() << "... failed!");
780 return false;
781 }
782
783 SameTails[commonTailIndex].setBlock(newMBB);
784 SameTails[commonTailIndex].setTailStartPos(newMBB->begin());
785
786 // If we split PredBB, newMBB is the new predecessor.
787 if (PredBB == MBB)
788 PredBB = newMBB;
789
790 return true;
791}
792
793/// Ensure undef flag is preserved only when it is present in both instructions.
794static void mergeUndefFlag(MachineInstr &Merged, const MachineInstr &Other) {
795 for (unsigned I = 0, E = Merged.getNumOperands(); I != E; ++I) {
796 MachineOperand &MO = Merged.getOperand(I);
797 if (MO.isReg() && MO.isUndef() && !Other.getOperand(I).isUndef())
798 MO.setIsUndef(false);
799 }
800}
801
802static void
804 MachineBasicBlock &MBBCommon) {
805 MachineBasicBlock *MBB = MBBIStartPos->getParent();
806 // Note CommonTailLen does not necessarily matches the size of
807 // the common BB nor all its instructions because of debug
808 // instructions differences.
809 unsigned CommonTailLen = 0;
810 for (auto E = MBB->end(); MBBIStartPos != E; ++MBBIStartPos)
811 ++CommonTailLen;
812
815 MachineBasicBlock::reverse_iterator MBBICommon = MBBCommon.rbegin();
816 MachineBasicBlock::reverse_iterator MBBIECommon = MBBCommon.rend();
817
818 while (CommonTailLen--) {
819 assert(MBBI != MBBIE && "Reached BB end within common tail length!");
820 (void)MBBIE;
821
822 if (!countsAsInstruction(*MBBI)) {
823 ++MBBI;
824 continue;
825 }
826
827 while ((MBBICommon != MBBIECommon) && !countsAsInstruction(*MBBICommon))
828 ++MBBICommon;
829
830 assert(MBBICommon != MBBIECommon &&
831 "Reached BB end within common tail length!");
832 assert(MBBICommon->isIdenticalTo(*MBBI) && "Expected matching MIIs!");
833
834 // Merge MMOs from memory operations in the common block.
835 if (MBBICommon->mayLoadOrStore())
836 MBBICommon->cloneMergedMemRefs(*MBB->getParent(), {&*MBBICommon, &*MBBI});
837
838 // Drop undef flags if they aren't present in all merged instructions.
839 mergeUndefFlag(*MBBICommon, *MBBI);
840
841 ++MBBI;
842 ++MBBICommon;
843 }
844}
845
846void BranchFolder::mergeCommonTails(unsigned commonTailIndex) {
847 MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
848
849 std::vector<MachineBasicBlock::iterator> NextCommonInsts(SameTails.size());
850 for (unsigned int i = 0 ; i != SameTails.size() ; ++i) {
851 if (i != commonTailIndex) {
852 NextCommonInsts[i] = SameTails[i].getTailStartPos();
853 mergeOperations(SameTails[i].getTailStartPos(), *MBB);
854 } else {
855 assert(SameTails[i].getTailStartPos() == MBB->begin() &&
856 "MBB is not a common tail only block");
857 }
858 }
859
860 for (auto &MI : *MBB) {
862 continue;
863 DebugLoc DL = MI.getDebugLoc();
864 for (unsigned int i = 0 ; i < NextCommonInsts.size() ; i++) {
865 if (i == commonTailIndex)
866 continue;
867
868 auto &Pos = NextCommonInsts[i];
869 assert(Pos != SameTails[i].getBlock()->end() &&
870 "Reached BB end within common tail");
871 while (!countsAsInstruction(*Pos)) {
872 ++Pos;
873 assert(Pos != SameTails[i].getBlock()->end() &&
874 "Reached BB end within common tail");
875 }
876 assert(MI.isIdenticalTo(*Pos) && "Expected matching MIIs!");
877 DL = DebugLoc::getMergedLocation(DL, Pos->getDebugLoc());
878 NextCommonInsts[i] = ++Pos;
879 }
880 MI.setDebugLoc(DL);
881 }
882
883 if (UpdateLiveIns) {
884 LivePhysRegs NewLiveIns(*TRI);
885 computeLiveIns(NewLiveIns, *MBB);
886 LiveRegs.init(*TRI);
887
888 // The flag merging may lead to some register uses no longer using the
889 // <undef> flag, add IMPLICIT_DEFs in the predecessors as necessary.
890 for (MachineBasicBlock *Pred : MBB->predecessors()) {
891 LiveRegs.clear();
892 LiveRegs.addLiveOuts(*Pred);
893 MachineBasicBlock::iterator InsertBefore = Pred->getFirstTerminator();
894 for (Register Reg : NewLiveIns) {
895 if (!LiveRegs.available(*MRI, Reg))
896 continue;
897
898 // Skip the register if we are about to add one of its super registers.
899 // TODO: Common this up with the same logic in addLineIns().
900 if (any_of(TRI->superregs(Reg), [&](MCPhysReg SReg) {
901 return NewLiveIns.contains(SReg) && !MRI->isReserved(SReg);
902 }))
903 continue;
904
905 DebugLoc DL;
906 BuildMI(*Pred, InsertBefore, DL, TII->get(TargetOpcode::IMPLICIT_DEF),
907 Reg);
908 }
909 }
910
911 MBB->clearLiveIns();
912 addLiveIns(*MBB, NewLiveIns);
913 }
914}
915
916// See if any of the blocks in MergePotentials (which all have SuccBB as a
917// successor, or all have no successor if it is null) can be tail-merged.
918// If there is a successor, any blocks in MergePotentials that are not
919// tail-merged and are not immediately before Succ must have an unconditional
920// branch to Succ added (but the predecessor/successor lists need no
921// adjustment). The lone predecessor of Succ that falls through into Succ,
922// if any, is given in PredBB.
923// MinCommonTailLength - Except for the special cases below, tail-merge if
924// there are at least this many instructions in common.
925bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
926 MachineBasicBlock *PredBB,
927 unsigned MinCommonTailLength) {
928 bool MadeChange = false;
929
930 LLVM_DEBUG({
931 dbgs() << "\nTryTailMergeBlocks: ";
932 for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
933 dbgs() << printMBBReference(*MergePotentials[i].getBlock())
934 << (i == e - 1 ? "" : ", ");
935 dbgs() << "\n";
936 if (SuccBB) {
937 dbgs() << " with successor " << printMBBReference(*SuccBB) << '\n';
938 if (PredBB)
939 dbgs() << " which has fall-through from " << printMBBReference(*PredBB)
940 << "\n";
941 }
942 dbgs() << "Looking for common tails of at least " << MinCommonTailLength
943 << " instruction" << (MinCommonTailLength == 1 ? "" : "s") << '\n';
944 });
945
946 // Sort by hash value so that blocks with identical end sequences sort
947 // together.
948#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
949 // If origin-tracking is enabled then MergePotentialElt is no longer a POD
950 // type, so we need std::sort instead.
951 std::sort(MergePotentials.begin(), MergePotentials.end());
952#else
953 array_pod_sort(MergePotentials.begin(), MergePotentials.end());
954#endif
955
956 // Walk through equivalence sets looking for actual exact matches.
957 while (MergePotentials.size() > 1) {
958 unsigned CurHash = MergePotentials.back().getHash();
959 const DebugLoc &BranchDL = MergePotentials.back().getBranchDebugLoc();
960
961 // Build SameTails, identifying the set of blocks with this hash code
962 // and with the maximum number of instructions in common.
963 unsigned maxCommonTailLength = ComputeSameTails(CurHash,
964 MinCommonTailLength,
965 SuccBB, PredBB);
966
967 // If we didn't find any pair that has at least MinCommonTailLength
968 // instructions in common, remove all blocks with this hash code and retry.
969 if (SameTails.empty()) {
970 RemoveBlocksWithHash(CurHash, SuccBB, PredBB, BranchDL);
971 continue;
972 }
973
974 // If one of the blocks is the entire common tail (and is not the entry
975 // block/an EH pad, which we can't jump to), we can treat all blocks with
976 // this same tail at once. Use PredBB if that is one of the possibilities,
977 // as that will not introduce any extra branches.
978 MachineBasicBlock *EntryBB =
979 &MergePotentials.front().getBlock()->getParent()->front();
980 unsigned commonTailIndex = SameTails.size();
981 // If there are two blocks, check to see if one can be made to fall through
982 // into the other.
983 if (SameTails.size() == 2 &&
984 SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
985 SameTails[1].tailIsWholeBlock() && !SameTails[1].getBlock()->isEHPad())
986 commonTailIndex = 1;
987 else if (SameTails.size() == 2 &&
988 SameTails[1].getBlock()->isLayoutSuccessor(
989 SameTails[0].getBlock()) &&
990 SameTails[0].tailIsWholeBlock() &&
991 !SameTails[0].getBlock()->isEHPad())
992 commonTailIndex = 0;
993 else {
994 // Otherwise just pick one, favoring the fall-through predecessor if
995 // there is one.
996 for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
997 MachineBasicBlock *MBB = SameTails[i].getBlock();
998 if ((MBB == EntryBB || MBB->isEHPad()) &&
999 SameTails[i].tailIsWholeBlock())
1000 continue;
1001 if (MBB == PredBB) {
1002 commonTailIndex = i;
1003 break;
1004 }
1005 if (SameTails[i].tailIsWholeBlock())
1006 commonTailIndex = i;
1007 }
1008 }
1009
1010 if (commonTailIndex == SameTails.size() ||
1011 (SameTails[commonTailIndex].getBlock() == PredBB &&
1012 !SameTails[commonTailIndex].tailIsWholeBlock())) {
1013 // None of the blocks consist entirely of the common tail.
1014 // Split a block so that one does.
1015 if (!CreateCommonTailOnlyBlock(PredBB, SuccBB,
1016 maxCommonTailLength, commonTailIndex)) {
1017 RemoveBlocksWithHash(CurHash, SuccBB, PredBB, BranchDL);
1018 continue;
1019 }
1020 }
1021
1022 MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
1023
1024 // Recompute common tail MBB's edge weights and block frequency.
1025 setCommonTailEdgeWeights(*MBB);
1026
1027 // Merge debug locations, MMOs and undef flags across identical instructions
1028 // for common tail.
1029 mergeCommonTails(commonTailIndex);
1030
1031 // MBB is common tail. Adjust all other BB's to jump to this one.
1032 // Traversal must be forwards so erases work.
1033 LLVM_DEBUG(dbgs() << "\nUsing common tail in " << printMBBReference(*MBB)
1034 << " for ");
1035 for (unsigned int i=0, e = SameTails.size(); i != e; ++i) {
1036 if (commonTailIndex == i)
1037 continue;
1038 LLVM_DEBUG(dbgs() << printMBBReference(*SameTails[i].getBlock())
1039 << (i == e - 1 ? "" : ", "));
1040 // Hack the end off BB i, making it jump to BB commonTailIndex instead.
1041 replaceTailWithBranchTo(SameTails[i].getTailStartPos(), *MBB);
1042 // BB i is no longer a predecessor of SuccBB; remove it from the worklist.
1043 MergePotentials.erase(SameTails[i].getMPIter());
1044 }
1045 LLVM_DEBUG(dbgs() << "\n");
1046 // We leave commonTailIndex in the worklist in case there are other blocks
1047 // that match it with a smaller number of instructions.
1048 MadeChange = true;
1049 }
1050 return MadeChange;
1051}
1052
1053bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
1054 bool MadeChange = false;
1055 if (!EnableTailMerge)
1056 return MadeChange;
1057
1058 // First find blocks with no successors.
1059 // Block placement may create new tail merging opportunities for these blocks.
1060 MergePotentials.clear();
1061 for (MachineBasicBlock &MBB : MF) {
1062 if (MergePotentials.size() == TailMergeThreshold)
1063 break;
1064 if (!TriedMerging.count(&MBB) && MBB.succ_empty())
1065 MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(MBB), &MBB,
1067 }
1068
1069 // If this is a large problem, avoid visiting the same basic blocks
1070 // multiple times.
1071 if (MergePotentials.size() == TailMergeThreshold)
1072 for (const MergePotentialsElt &Elt : MergePotentials)
1073 TriedMerging.insert(Elt.getBlock());
1074
1075 // See if we can do any tail merging on those.
1076 if (MergePotentials.size() >= 2)
1077 MadeChange |= TryTailMergeBlocks(nullptr, nullptr, MinCommonTailLength);
1078
1079 // Look at blocks (IBB) with multiple predecessors (PBB).
1080 // We change each predecessor to a canonical form, by
1081 // (1) temporarily removing any unconditional branch from the predecessor
1082 // to IBB, and
1083 // (2) alter conditional branches so they branch to the other block
1084 // not IBB; this may require adding back an unconditional branch to IBB
1085 // later, where there wasn't one coming in. E.g.
1086 // Bcc IBB
1087 // fallthrough to QBB
1088 // here becomes
1089 // Bncc QBB
1090 // with a conceptual B to IBB after that, which never actually exists.
1091 // With those changes, we see whether the predecessors' tails match,
1092 // and merge them if so. We change things out of canonical form and
1093 // back to the way they were later in the process. (OptimizeBranches
1094 // would undo some of this, but we can't use it, because we'd get into
1095 // a compile-time infinite loop repeatedly doing and undoing the same
1096 // transformations.)
1097
1098 for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
1099 I != E; ++I) {
1100 if (I->pred_size() < 2) continue;
1101 SmallPtrSet<MachineBasicBlock *, 8> UniquePreds;
1102 MachineBasicBlock *IBB = &*I;
1103 MachineBasicBlock *PredBB = &*std::prev(I);
1104 MergePotentials.clear();
1105 MachineLoop *ML;
1106
1107 // Bail if merging after placement and IBB is the loop header because
1108 // -- If merging predecessors that belong to the same loop as IBB, the
1109 // common tail of merged predecessors may become the loop top if block
1110 // placement is called again and the predecessors may branch to this common
1111 // tail and require more branches. This can be relaxed if
1112 // MachineBlockPlacement::findBestLoopTop is more flexible.
1113 // --If merging predecessors that do not belong to the same loop as IBB, the
1114 // loop info of IBB's loop and the other loops may be affected. Calling the
1115 // block placement again may make big change to the layout and eliminate the
1116 // reason to do tail merging here.
1117 if (AfterBlockPlacement && MLI) {
1118 ML = MLI->getLoopFor(IBB);
1119 if (ML && IBB == ML->getHeader())
1120 continue;
1121 }
1122
1123 for (MachineBasicBlock *PBB : I->predecessors()) {
1124 if (MergePotentials.size() == TailMergeThreshold)
1125 break;
1126
1127 if (TriedMerging.count(PBB))
1128 continue;
1129
1130 // Skip blocks that loop to themselves, can't tail merge these.
1131 if (PBB == IBB)
1132 continue;
1133
1134 // Visit each predecessor only once.
1135 if (!UniquePreds.insert(PBB).second)
1136 continue;
1137
1138 // Skip blocks which may jump to a landing pad or jump from an asm blob.
1139 // Can't tail merge these.
1140 if (PBB->hasEHPadSuccessor() || PBB->mayHaveInlineAsmBr())
1141 continue;
1142
1143 // After block placement, only consider predecessors that belong to the
1144 // same loop as IBB. The reason is the same as above when skipping loop
1145 // header.
1146 if (AfterBlockPlacement && MLI)
1147 if (ML != MLI->getLoopFor(PBB))
1148 continue;
1149
1150 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1152 if (!TII->analyzeBranch(*PBB, TBB, FBB, Cond, true)) {
1153 // Failing case: IBB is the target of a cbr, and we cannot reverse the
1154 // branch.
1156 if (!Cond.empty() && TBB == IBB) {
1157 if (TII->reverseBranchCondition(NewCond))
1158 continue;
1159 // This is the QBB case described above
1160 if (!FBB) {
1161 auto Next = ++PBB->getIterator();
1162 if (Next != MF.end())
1163 FBB = &*Next;
1164 }
1165 }
1166
1167 // Remove the unconditional branch at the end, if any.
1168 DebugLoc dl = PBB->findBranchDebugLoc();
1169 if (TBB && (Cond.empty() || FBB)) {
1170 TII->removeBranch(*PBB);
1171 if (!Cond.empty())
1172 // reinsert conditional branch only, for now
1173 TII->insertBranch(*PBB, (TBB == IBB) ? FBB : TBB, nullptr,
1174 NewCond, dl);
1175 }
1176
1177 MergePotentials.push_back(
1178 MergePotentialsElt(HashEndOfMBB(*PBB), PBB, dl));
1179 }
1180 }
1181
1182 // If this is a large problem, avoid visiting the same basic blocks multiple
1183 // times.
1184 if (MergePotentials.size() == TailMergeThreshold)
1185 for (MergePotentialsElt &Elt : MergePotentials)
1186 TriedMerging.insert(Elt.getBlock());
1187
1188 if (MergePotentials.size() >= 2)
1189 MadeChange |= TryTailMergeBlocks(IBB, PredBB, MinCommonTailLength);
1190
1191 // Reinsert an unconditional branch if needed. The 1 below can occur as a
1192 // result of removing blocks in TryTailMergeBlocks.
1193 PredBB = &*std::prev(I); // this may have been changed in TryTailMergeBlocks
1194 if (MergePotentials.size() == 1 &&
1195 MergePotentials.begin()->getBlock() != PredBB)
1196 FixTail(MergePotentials.begin()->getBlock(), IBB, TII,
1197 MergePotentials.begin()->getBranchDebugLoc());
1198 }
1199
1200 return MadeChange;
1201}
1202
1203void BranchFolder::setCommonTailEdgeWeights(MachineBasicBlock &TailMBB) {
1204 SmallVector<BlockFrequency, 2> EdgeFreqLs(TailMBB.succ_size());
1205 BlockFrequency AccumulatedMBBFreq;
1206
1207 // Aggregate edge frequency of successor edge j:
1208 // edgeFreq(j) = sum (freq(bb) * edgeProb(bb, j)),
1209 // where bb is a basic block that is in SameTails.
1210 for (const auto &Src : SameTails) {
1211 const MachineBasicBlock *SrcMBB = Src.getBlock();
1212 BlockFrequency BlockFreq = MBBFreqInfo.getBlockFreq(SrcMBB);
1213 AccumulatedMBBFreq += BlockFreq;
1214
1215 // It is not necessary to recompute edge weights if TailBB has less than two
1216 // successors.
1217 if (TailMBB.succ_size() <= 1)
1218 continue;
1219
1220 auto EdgeFreq = EdgeFreqLs.begin();
1221
1222 for (auto SuccI = TailMBB.succ_begin(), SuccE = TailMBB.succ_end();
1223 SuccI != SuccE; ++SuccI, ++EdgeFreq)
1224 *EdgeFreq += BlockFreq * MBPI.getEdgeProbability(SrcMBB, *SuccI);
1225 }
1226
1227 MBBFreqInfo.setBlockFreq(&TailMBB, AccumulatedMBBFreq);
1228
1229 if (TailMBB.succ_size() <= 1)
1230 return;
1231
1232 auto SumEdgeFreq =
1233 std::accumulate(EdgeFreqLs.begin(), EdgeFreqLs.end(), BlockFrequency(0))
1234 .getFrequency();
1235 auto EdgeFreq = EdgeFreqLs.begin();
1236
1237 if (SumEdgeFreq > 0) {
1238 for (auto SuccI = TailMBB.succ_begin(), SuccE = TailMBB.succ_end();
1239 SuccI != SuccE; ++SuccI, ++EdgeFreq) {
1241 EdgeFreq->getFrequency(), SumEdgeFreq);
1242 TailMBB.setSuccProbability(SuccI, Prob);
1243 }
1244 }
1245}
1246
1247//===----------------------------------------------------------------------===//
1248// Branch Optimization
1249//===----------------------------------------------------------------------===//
1250
1251bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
1252 bool MadeChange = false;
1253
1254 // Make sure blocks are numbered in order
1255 MF.RenumberBlocks();
1256 // Renumbering blocks alters EH scope membership, recalculate it.
1257 EHScopeMembership = getEHScopeMembership(MF);
1258
1259 for (MachineBasicBlock &MBB :
1261 MadeChange |= OptimizeBlock(&MBB);
1262
1263 // If it is dead, remove it.
1265 !MBB.isEHPad()) {
1266 RemoveDeadBlock(&MBB);
1267 MadeChange = true;
1268 ++NumDeadBlocks;
1269 }
1270 }
1271
1272 return MadeChange;
1273}
1274
1275// Blocks should be considered empty if they contain only debug info;
1276// else the debug info would affect codegen.
1278 return MBB->getFirstNonDebugInstr(true) == MBB->end();
1279}
1280
1281// Blocks with only debug info and branches should be considered the same
1282// as blocks with only branches.
1284 MachineBasicBlock::iterator I = MBB->getFirstNonDebugInstr();
1285 assert(I != MBB->end() && "empty block!");
1286 return I->isBranch();
1287}
1288
1289/// IsBetterFallthrough - Return true if it would be clearly better to
1290/// fall-through to MBB1 than to fall through into MBB2. This has to return
1291/// a strict ordering, returning true for both (MBB1,MBB2) and (MBB2,MBB1) will
1292/// result in infinite loops.
1294 MachineBasicBlock *MBB2) {
1295 assert(MBB1 && MBB2 && "Unknown MachineBasicBlock");
1296
1297 // Right now, we use a simple heuristic. If MBB2 ends with a call, and
1298 // MBB1 doesn't, we prefer to fall through into MBB1. This allows us to
1299 // optimize branches that branch to either a return block or an assert block
1300 // into a fallthrough to the return.
1303 if (MBB1I == MBB1->end() || MBB2I == MBB2->end())
1304 return false;
1305
1306 // If there is a clear successor ordering we make sure that one block
1307 // will fall through to the next
1308 if (MBB1->isSuccessor(MBB2)) return true;
1309 if (MBB2->isSuccessor(MBB1)) return false;
1310
1311 return MBB2I->isCall() && !MBB1I->isCall();
1312}
1313
1316 MachineBasicBlock &PredMBB) {
1317 auto InsertBefore = PredMBB.getFirstTerminator();
1318 for (MachineInstr &MI : MBB.instrs())
1319 if (MI.isDebugInstr()) {
1320 TII->duplicate(PredMBB, InsertBefore, MI);
1321 LLVM_DEBUG(dbgs() << "Copied debug entity from empty block to pred: "
1322 << MI);
1323 }
1324}
1325
1328 MachineBasicBlock &SuccMBB) {
1329 auto InsertBefore = SuccMBB.SkipPHIsAndLabels(SuccMBB.begin());
1330 for (MachineInstr &MI : MBB.instrs())
1331 if (MI.isDebugInstr()) {
1332 TII->duplicate(SuccMBB, InsertBefore, MI);
1333 LLVM_DEBUG(dbgs() << "Copied debug entity from empty block to succ: "
1334 << MI);
1335 }
1336}
1337
1338// Try to salvage DBG_VALUE instructions from an otherwise empty block. If such
1339// a basic block is removed we would lose the debug information unless we have
1340// copied the information to a predecessor/successor.
1341//
1342// TODO: This function only handles some simple cases. An alternative would be
1343// to run a heavier analysis, such as the LiveDebugValues pass, before we do
1344// branch folding.
1347 assert(IsEmptyBlock(&MBB) && "Expected an empty block (except debug info).");
1348 // If this MBB is the only predecessor of a successor it is legal to copy
1349 // DBG_VALUE instructions to the beginning of the successor.
1350 for (MachineBasicBlock *SuccBB : MBB.successors())
1351 if (SuccBB->pred_size() == 1)
1352 copyDebugInfoToSuccessor(TII, MBB, *SuccBB);
1353 // If this MBB is the only successor of a predecessor it is legal to copy the
1354 // DBG_VALUE instructions to the end of the predecessor (just before the
1355 // terminators, assuming that the terminator isn't affecting the DBG_VALUE).
1356 for (MachineBasicBlock *PredBB : MBB.predecessors())
1357 if (PredBB->succ_size() == 1)
1359}
1360
1361bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
1362 bool MadeChange = false;
1363 MachineFunction &MF = *MBB->getParent();
1364ReoptimizeBlock:
1365
1366 MachineFunction::iterator FallThrough = MBB->getIterator();
1367 ++FallThrough;
1368
1369 // Make sure MBB and FallThrough belong to the same EH scope.
1370 bool SameEHScope = true;
1371 if (!EHScopeMembership.empty() && FallThrough != MF.end()) {
1372 auto MBBEHScope = EHScopeMembership.find(MBB);
1373 assert(MBBEHScope != EHScopeMembership.end());
1374 auto FallThroughEHScope = EHScopeMembership.find(&*FallThrough);
1375 assert(FallThroughEHScope != EHScopeMembership.end());
1376 SameEHScope = MBBEHScope->second == FallThroughEHScope->second;
1377 }
1378
1379 // Analyze the branch in the current block. As a side-effect, this may cause
1380 // the block to become empty.
1381 MachineBasicBlock *CurTBB = nullptr, *CurFBB = nullptr;
1383 bool CurUnAnalyzable =
1384 TII->analyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true);
1385
1386 // If this block is empty, make everyone use its fall-through, not the block
1387 // explicitly. Landing pads should not do this since the landing-pad table
1388 // points to this block. Blocks with their addresses taken shouldn't be
1389 // optimized away.
1390 if (IsEmptyBlock(MBB) && !MBB->isEHPad() && !MBB->hasAddressTaken() &&
1391 SameEHScope) {
1393 // Dead block? Leave for cleanup later.
1394 if (MBB->pred_empty()) return MadeChange;
1395
1396 if (FallThrough == MF.end()) {
1397 // TODO: Simplify preds to not branch here if possible!
1398 } else if (FallThrough->isEHPad()) {
1399 // Don't rewrite to a landing pad fallthough. That could lead to the case
1400 // where a BB jumps to more than one landing pad.
1401 // TODO: Is it ever worth rewriting predecessors which don't already
1402 // jump to a landing pad, and so can safely jump to the fallthrough?
1403 } else if (MBB->isSuccessor(&*FallThrough)) {
1404 // Rewrite all predecessors of the old block to go to the fallthrough
1405 // instead.
1406 while (!MBB->pred_empty()) {
1407 MachineBasicBlock *Pred = *(MBB->pred_end()-1);
1408 Pred->ReplaceUsesOfBlockWith(MBB, &*FallThrough);
1409 }
1410 // Add rest successors of MBB to successors of FallThrough. Those
1411 // successors are not directly reachable via MBB, so it should be
1412 // landing-pad.
1413 for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI)
1414 if (*SI != &*FallThrough && !FallThrough->isSuccessor(*SI)) {
1415 assert((*SI)->isEHPad() && "Bad CFG");
1416 FallThrough->copySuccessor(MBB, SI);
1417 }
1418 // If MBB was the target of a jump table, update jump tables to go to the
1419 // fallthrough instead.
1420 if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo())
1421 MJTI->ReplaceMBBInJumpTables(MBB, &*FallThrough);
1422 MadeChange = true;
1423 }
1424 return MadeChange;
1425 }
1426
1427 // Check to see if we can simplify the terminator of the block before this
1428 // one.
1429 MachineBasicBlock &PrevBB = *std::prev(MachineFunction::iterator(MBB));
1430
1431 MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
1433 bool PriorUnAnalyzable =
1434 TII->analyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true);
1435 if (!PriorUnAnalyzable) {
1436 // If the previous branch is conditional and both conditions go to the same
1437 // destination, remove the branch, replacing it with an unconditional one or
1438 // a fall-through.
1439 if (PriorTBB && PriorTBB == PriorFBB) {
1440 DebugLoc Dl = PrevBB.findBranchDebugLoc();
1441 TII->removeBranch(PrevBB);
1442 PriorCond.clear();
1443 if (PriorTBB != MBB)
1444 TII->insertBranch(PrevBB, PriorTBB, nullptr, PriorCond, Dl);
1445 MadeChange = true;
1446 ++NumBranchOpts;
1447 goto ReoptimizeBlock;
1448 }
1449
1450 // If the previous block unconditionally falls through to this block and
1451 // this block has no other predecessors, move the contents of this block
1452 // into the prior block. This doesn't usually happen when SimplifyCFG
1453 // has been used, but it can happen if tail merging splits a fall-through
1454 // predecessor of a block.
1455 // This has to check PrevBB->succ_size() because EH edges are ignored by
1456 // analyzeBranch.
1457 if (PriorCond.empty() && !PriorTBB && MBB->pred_size() == 1 &&
1458 PrevBB.succ_size() == 1 && PrevBB.isSuccessor(MBB) &&
1459 !MBB->hasAddressTaken() && !MBB->isEHPad()) {
1460 LLVM_DEBUG(dbgs() << "\nMerging into block: " << PrevBB
1461 << "From MBB: " << *MBB);
1462 // Remove redundant DBG_VALUEs first.
1463 if (!PrevBB.empty()) {
1464 MachineBasicBlock::iterator PrevBBIter = PrevBB.end();
1465 --PrevBBIter;
1467 // Check if DBG_VALUE at the end of PrevBB is identical to the
1468 // DBG_VALUE at the beginning of MBB.
1469 while (PrevBBIter != PrevBB.begin() && MBBIter != MBB->end()
1470 && PrevBBIter->isDebugInstr() && MBBIter->isDebugInstr()) {
1471 if (!MBBIter->isIdenticalTo(*PrevBBIter))
1472 break;
1473 MachineInstr &DuplicateDbg = *MBBIter;
1474 ++MBBIter; -- PrevBBIter;
1475 DuplicateDbg.eraseFromParent();
1476 }
1477 }
1478 PrevBB.splice(PrevBB.end(), MBB, MBB->begin(), MBB->end());
1479 PrevBB.removeSuccessor(PrevBB.succ_begin());
1480 assert(PrevBB.succ_empty());
1481 PrevBB.transferSuccessors(MBB);
1482 MadeChange = true;
1483 return MadeChange;
1484 }
1485
1486 // If the previous branch *only* branches to *this* block (conditional or
1487 // not) remove the branch.
1488 if (PriorTBB == MBB && !PriorFBB) {
1489 TII->removeBranch(PrevBB);
1490 MadeChange = true;
1491 ++NumBranchOpts;
1492 goto ReoptimizeBlock;
1493 }
1494
1495 // If the prior block branches somewhere else on the condition and here if
1496 // the condition is false, remove the uncond second branch.
1497 if (PriorFBB == MBB) {
1498 DebugLoc Dl = PrevBB.findBranchDebugLoc();
1499 TII->removeBranch(PrevBB);
1500 TII->insertBranch(PrevBB, PriorTBB, nullptr, PriorCond, Dl);
1501 MadeChange = true;
1502 ++NumBranchOpts;
1503 goto ReoptimizeBlock;
1504 }
1505
1506 // If the prior block branches here on true and somewhere else on false, and
1507 // if the branch condition is reversible, reverse the branch to create a
1508 // fall-through.
1509 if (PriorTBB == MBB) {
1510 SmallVector<MachineOperand, 4> NewPriorCond(PriorCond);
1511 if (!TII->reverseBranchCondition(NewPriorCond)) {
1512 DebugLoc Dl = PrevBB.findBranchDebugLoc();
1513 TII->removeBranch(PrevBB);
1514 TII->insertBranch(PrevBB, PriorFBB, nullptr, NewPriorCond, Dl);
1515 MadeChange = true;
1516 ++NumBranchOpts;
1517 goto ReoptimizeBlock;
1518 }
1519 }
1520
1521 // If this block has no successors (e.g. it is a return block or ends with
1522 // a call to a no-return function like abort or __cxa_throw) and if the pred
1523 // falls through into this block, and if it would otherwise fall through
1524 // into the block after this, move this block to the end of the function.
1525 //
1526 // We consider it more likely that execution will stay in the function (e.g.
1527 // due to loops) than it is to exit it. This asserts in loops etc, moving
1528 // the assert condition out of the loop body.
1529 if (MBB->succ_empty() && !PriorCond.empty() && !PriorFBB &&
1530 MachineFunction::iterator(PriorTBB) == FallThrough &&
1531 !MBB->canFallThrough()) {
1532 bool DoTransform = true;
1533
1534 // We have to be careful that the succs of PredBB aren't both no-successor
1535 // blocks. If neither have successors and if PredBB is the second from
1536 // last block in the function, we'd just keep swapping the two blocks for
1537 // last. Only do the swap if one is clearly better to fall through than
1538 // the other.
1539 if (FallThrough == --MF.end() &&
1540 !IsBetterFallthrough(PriorTBB, MBB))
1541 DoTransform = false;
1542
1543 if (DoTransform) {
1544 // Reverse the branch so we will fall through on the previous true cond.
1545 SmallVector<MachineOperand, 4> NewPriorCond(PriorCond);
1546 if (!TII->reverseBranchCondition(NewPriorCond)) {
1547 LLVM_DEBUG(dbgs() << "\nMoving MBB: " << *MBB
1548 << "To make fallthrough to: " << *PriorTBB << "\n");
1549
1550 DebugLoc Dl = PrevBB.findBranchDebugLoc();
1551 TII->removeBranch(PrevBB);
1552 TII->insertBranch(PrevBB, MBB, nullptr, NewPriorCond, Dl);
1553
1554 // Move this block to the end of the function.
1555 MBB->moveAfter(&MF.back());
1556 MadeChange = true;
1557 ++NumBranchOpts;
1558 return MadeChange;
1559 }
1560 }
1561 }
1562 }
1563
1564 if (!IsEmptyBlock(MBB)) {
1565 MachineInstr &TailCall = *MBB->getFirstNonDebugInstr();
1566 if (TII->isUnconditionalTailCall(TailCall)) {
1568 for (auto &Pred : MBB->predecessors()) {
1569 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
1571 bool PredAnalyzable =
1572 !TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true);
1573
1574 // Only eliminate if MBB == TBB (Taken Basic Block)
1575 if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB &&
1576 PredTBB != PredFBB) {
1577 // The predecessor has a conditional branch to this block which
1578 // consists of only a tail call. Try to fold the tail call into the
1579 // conditional branch.
1580 if (TII->canMakeTailCallConditional(PredCond, TailCall)) {
1581 // TODO: It would be nice if analyzeBranch() could provide a pointer
1582 // to the branch instruction so replaceBranchWithTailCall() doesn't
1583 // have to search for it.
1584 TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall);
1585 PredsChanged.push_back(Pred);
1586 }
1587 }
1588 // If the predecessor is falling through to this block, we could reverse
1589 // the branch condition and fold the tail call into that. However, after
1590 // that we might have to re-arrange the CFG to fall through to the other
1591 // block and there is a high risk of regressing code size rather than
1592 // improving it.
1593 }
1594 if (!PredsChanged.empty()) {
1595 NumTailCalls += PredsChanged.size();
1596 for (auto &Pred : PredsChanged)
1597 Pred->removeSuccessor(MBB);
1598
1599 return true;
1600 }
1601 }
1602 }
1603
1604 if (!CurUnAnalyzable) {
1605 // If this is a two-way branch, and the FBB branches to this block, reverse
1606 // the condition so the single-basic-block loop is faster. Instead of:
1607 // Loop: xxx; jcc Out; jmp Loop
1608 // we want:
1609 // Loop: xxx; jncc Loop; jmp Out
1610 if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) {
1611 SmallVector<MachineOperand, 4> NewCond(CurCond);
1612 if (!TII->reverseBranchCondition(NewCond)) {
1614 TII->removeBranch(*MBB);
1615 TII->insertBranch(*MBB, CurFBB, CurTBB, NewCond, Dl);
1616 MadeChange = true;
1617 ++NumBranchOpts;
1618 goto ReoptimizeBlock;
1619 }
1620 }
1621
1622 // If this branch is the only thing in its block, see if we can forward
1623 // other blocks across it.
1624 if (CurTBB && CurCond.empty() && !CurFBB &&
1625 IsBranchOnlyBlock(MBB) && CurTBB != MBB &&
1626 !MBB->hasAddressTaken() && !MBB->isEHPad()) {
1628 // This block may contain just an unconditional branch. Because there can
1629 // be 'non-branch terminators' in the block, try removing the branch and
1630 // then seeing if the block is empty.
1631 TII->removeBranch(*MBB);
1632 // If the only things remaining in the block are debug info, remove these
1633 // as well, so this will behave the same as an empty block in non-debug
1634 // mode.
1635 if (IsEmptyBlock(MBB)) {
1636 // Make the block empty, losing the debug info (we could probably
1637 // improve this in some cases.)
1638 MBB->erase(MBB->begin(), MBB->end());
1639 }
1640 // If this block is just an unconditional branch to CurTBB, we can
1641 // usually completely eliminate the block. The only case we cannot
1642 // completely eliminate the block is when the block before this one
1643 // falls through into MBB and we can't understand the prior block's branch
1644 // condition.
1645 if (MBB->empty()) {
1646 bool PredHasNoFallThrough = !PrevBB.canFallThrough();
1647 if (PredHasNoFallThrough || !PriorUnAnalyzable ||
1648 !PrevBB.isSuccessor(MBB)) {
1649 // If the prior block falls through into us, turn it into an
1650 // explicit branch to us to make updates simpler.
1651 if (!PredHasNoFallThrough && PrevBB.isSuccessor(MBB) &&
1652 PriorTBB != MBB && PriorFBB != MBB) {
1653 if (!PriorTBB) {
1654 assert(PriorCond.empty() && !PriorFBB &&
1655 "Bad branch analysis");
1656 PriorTBB = MBB;
1657 } else {
1658 assert(!PriorFBB && "Machine CFG out of date!");
1659 PriorFBB = MBB;
1660 }
1661 DebugLoc PrevDl = PrevBB.findBranchDebugLoc();
1662 TII->removeBranch(PrevBB);
1663 TII->insertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, PrevDl);
1664 }
1665
1666 // Iterate through all the predecessors, revectoring each in-turn.
1667 size_t PI = 0;
1668 bool DidChange = false;
1669 bool HasBranchToSelf = false;
1670 while(PI != MBB->pred_size()) {
1671 MachineBasicBlock *PMBB = *(MBB->pred_begin() + PI);
1672 if (PMBB == MBB) {
1673 // If this block has an uncond branch to itself, leave it.
1674 ++PI;
1675 HasBranchToSelf = true;
1676 } else {
1677 DidChange = true;
1678 PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB);
1679 // Add rest successors of MBB to successors of CurTBB. Those
1680 // successors are not directly reachable via MBB, so it should be
1681 // landing-pad.
1682 for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE;
1683 ++SI)
1684 if (*SI != CurTBB && !CurTBB->isSuccessor(*SI)) {
1685 assert((*SI)->isEHPad() && "Bad CFG");
1686 CurTBB->copySuccessor(MBB, SI);
1687 }
1688 // If this change resulted in PMBB ending in a conditional
1689 // branch where both conditions go to the same destination,
1690 // change this to an unconditional branch.
1691 MachineBasicBlock *NewCurTBB = nullptr, *NewCurFBB = nullptr;
1693 bool NewCurUnAnalyzable = TII->analyzeBranch(
1694 *PMBB, NewCurTBB, NewCurFBB, NewCurCond, true);
1695 if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) {
1696 DebugLoc PrevDl = PMBB->findBranchDebugLoc();
1697 TII->removeBranch(*PMBB);
1698 NewCurCond.clear();
1699 TII->insertBranch(*PMBB, NewCurTBB, nullptr, NewCurCond,
1700 PrevDl);
1701 MadeChange = true;
1702 ++NumBranchOpts;
1703 }
1704 }
1705 }
1706
1707 // Change any jumptables to go to the new MBB.
1708 if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo())
1709 MJTI->ReplaceMBBInJumpTables(MBB, CurTBB);
1710 if (DidChange) {
1711 ++NumBranchOpts;
1712 MadeChange = true;
1713 if (!HasBranchToSelf) return MadeChange;
1714 }
1715 }
1716 }
1717
1718 // Add the branch back if the block is more than just an uncond branch.
1719 TII->insertBranch(*MBB, CurTBB, nullptr, CurCond, Dl);
1720 }
1721 }
1722
1723 // If the prior block doesn't fall through into this block, and if this
1724 // block doesn't fall through into some other block, see if we can find a
1725 // place to move this block where a fall-through will happen.
1726 if (!PrevBB.canFallThrough()) {
1727 // Now we know that there was no fall-through into this block, check to
1728 // see if it has a fall-through into its successor.
1729 bool CurFallsThru = MBB->canFallThrough();
1730
1731 if (!MBB->isEHPad()) {
1732 // Check all the predecessors of this block. If one of them has no fall
1733 // throughs, and analyzeBranch thinks it _could_ fallthrough to this
1734 // block, move this block right after it.
1735 for (MachineBasicBlock *PredBB : MBB->predecessors()) {
1736 // Analyze the branch at the end of the pred.
1737 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
1739 if (PredBB != MBB && !PredBB->canFallThrough() &&
1740 !TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true) &&
1741 (PredTBB == MBB || PredFBB == MBB) &&
1742 (!CurFallsThru || !CurTBB || !CurFBB) &&
1743 (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) {
1744 // If the current block doesn't fall through, just move it.
1745 // If the current block can fall through and does not end with a
1746 // conditional branch, we need to append an unconditional jump to
1747 // the (current) next block. To avoid a possible compile-time
1748 // infinite loop, move blocks only backward in this case.
1749 // Also, if there are already 2 branches here, we cannot add a third;
1750 // this means we have the case
1751 // Bcc next
1752 // B elsewhere
1753 // next:
1754 if (CurFallsThru) {
1755 MachineBasicBlock *NextBB = &*std::next(MBB->getIterator());
1756 CurCond.clear();
1757 TII->insertBranch(*MBB, NextBB, nullptr, CurCond, DebugLoc());
1758 }
1759 MBB->moveAfter(PredBB);
1760 MadeChange = true;
1761 goto ReoptimizeBlock;
1762 }
1763 }
1764 }
1765
1766 if (!CurFallsThru) {
1767 // Check analyzable branch-successors to see if we can move this block
1768 // before one.
1769 if (!CurUnAnalyzable) {
1770 for (MachineBasicBlock *SuccBB : {CurFBB, CurTBB}) {
1771 if (!SuccBB)
1772 continue;
1773 // Analyze the branch at the end of the block before the succ.
1774 MachineFunction::iterator SuccPrev = --SuccBB->getIterator();
1775
1776 // If this block doesn't already fall-through to that successor, and
1777 // if the succ doesn't already have a block that can fall through into
1778 // it, we can arrange for the fallthrough to happen.
1779 if (SuccBB != MBB && &*SuccPrev != MBB &&
1780 !SuccPrev->canFallThrough()) {
1781 MBB->moveBefore(SuccBB);
1782 MadeChange = true;
1783 goto ReoptimizeBlock;
1784 }
1785 }
1786 }
1787
1788 // Okay, there is no really great place to put this block. If, however,
1789 // the block before this one would be a fall-through if this block were
1790 // removed, move this block to the end of the function. There is no real
1791 // advantage in "falling through" to an EH block, so we don't want to
1792 // perform this transformation for that case.
1793 //
1794 // Also, Windows EH introduced the possibility of an arbitrary number of
1795 // successors to a given block. The analyzeBranch call does not consider
1796 // exception handling and so we can get in a state where a block
1797 // containing a call is followed by multiple EH blocks that would be
1798 // rotated infinitely at the end of the function if the transformation
1799 // below were performed for EH "FallThrough" blocks. Therefore, even if
1800 // that appears not to be happening anymore, we should assume that it is
1801 // possible and not remove the "!FallThrough()->isEHPad" condition below.
1802 //
1803 // Similarly, the analyzeBranch call does not consider callbr, which also
1804 // introduces the possibility of infinite rotation, as there may be
1805 // multiple successors of PrevBB. Thus we check such case by
1806 // FallThrough->isInlineAsmBrIndirectTarget().
1807 // NOTE: Checking if PrevBB contains callbr is more precise, but much
1808 // more expensive.
1809 MachineBasicBlock *PrevTBB = nullptr, *PrevFBB = nullptr;
1811
1812 if (FallThrough != MF.end() && !FallThrough->isEHPad() &&
1813 !FallThrough->isInlineAsmBrIndirectTarget() &&
1814 !TII->analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond, true) &&
1815 PrevBB.isSuccessor(&*FallThrough)) {
1816 MBB->moveAfter(&MF.back());
1817 MadeChange = true;
1818 return MadeChange;
1819 }
1820 }
1821 }
1822
1823 return MadeChange;
1824}
1825
1826//===----------------------------------------------------------------------===//
1827// Hoist Common Code
1828//===----------------------------------------------------------------------===//
1829
1830bool BranchFolder::HoistCommonCode(MachineFunction &MF) {
1831 bool MadeChange = false;
1832 for (MachineBasicBlock &MBB : llvm::make_early_inc_range(MF))
1833 MadeChange |= HoistCommonCodeInSuccs(&MBB);
1834
1835 return MadeChange;
1836}
1837
1838/// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
1839/// its 'true' successor.
1841 MachineBasicBlock *TrueBB) {
1842 for (MachineBasicBlock *SuccBB : BB->successors())
1843 if (SuccBB != TrueBB)
1844 return SuccBB;
1845 return nullptr;
1846}
1847
1848template <class Container>
1850 Container &Set) {
1851 if (Reg.isPhysical()) {
1852 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
1853 Set.insert(*AI);
1854 } else {
1855 Set.insert(Reg);
1856 }
1857}
1858
1859/// findHoistingInsertPosAndDeps - Find the location to move common instructions
1860/// in successors to. The location is usually just before the terminator,
1861/// however if the terminator is a conditional branch and its previous
1862/// instruction is the flag setting instruction, the previous instruction is
1863/// the preferred location. This function also gathers uses and defs of the
1864/// instructions from the insertion point to the end of the block. The data is
1865/// used by HoistCommonCodeInSuccs to ensure safety.
1866static
1868 const TargetInstrInfo *TII,
1869 const TargetRegisterInfo *TRI,
1871 SmallSet<Register, 4> &Defs) {
1872 MachineBasicBlock::iterator Loc = MBB->getFirstTerminator();
1873 if (!TII->isUnpredicatedTerminator(*Loc))
1874 return MBB->end();
1875
1876 for (const MachineOperand &MO : Loc->operands()) {
1877 if (!MO.isReg())
1878 continue;
1879 Register Reg = MO.getReg();
1880 if (!Reg)
1881 continue;
1882 if (MO.isUse()) {
1884 } else {
1885 if (!MO.isDead())
1886 // Don't try to hoist code in the rare case the terminator defines a
1887 // register that is later used.
1888 return MBB->end();
1889
1890 // If the terminator defines a register, make sure we don't hoist
1891 // the instruction whose def might be clobbered by the terminator.
1892 addRegAndItsAliases(Reg, TRI, Defs);
1893 }
1894 }
1895
1896 if (Uses.empty())
1897 return Loc;
1898 // If the terminator is the only instruction in the block and Uses is not
1899 // empty (or we would have returned above), we can still safely hoist
1900 // instructions just before the terminator as long as the Defs/Uses are not
1901 // violated (which is checked in HoistCommonCodeInSuccs).
1902 if (Loc == MBB->begin())
1903 return Loc;
1904
1905 // The terminator is probably a conditional branch, try not to separate the
1906 // branch from condition setting instruction.
1908
1909 bool IsDef = false;
1910 for (const MachineOperand &MO : PI->operands()) {
1911 // If PI has a regmask operand, it is probably a call. Separate away.
1912 if (MO.isRegMask())
1913 return Loc;
1914 if (!MO.isReg() || MO.isUse())
1915 continue;
1916 Register Reg = MO.getReg();
1917 if (!Reg)
1918 continue;
1919 if (Uses.count(Reg)) {
1920 IsDef = true;
1921 break;
1922 }
1923 }
1924 if (!IsDef)
1925 // The condition setting instruction is not just before the conditional
1926 // branch.
1927 return Loc;
1928
1929 // Be conservative, don't insert instruction above something that may have
1930 // side-effects. And since it's potentially bad to separate flag setting
1931 // instruction from the conditional branch, just abort the optimization
1932 // completely.
1933 // Also avoid moving code above predicated instruction since it's hard to
1934 // reason about register liveness with predicated instruction.
1935 bool DontMoveAcrossStore = true;
1936 if (!PI->isSafeToMove(DontMoveAcrossStore) || TII->isPredicated(*PI))
1937 return MBB->end();
1938
1939 // Find out what registers are live. Note this routine is ignoring other live
1940 // registers which are only used by instructions in successor blocks.
1941 for (const MachineOperand &MO : PI->operands()) {
1942 if (!MO.isReg())
1943 continue;
1944 Register Reg = MO.getReg();
1945 if (!Reg)
1946 continue;
1947 if (MO.isUse()) {
1949 } else {
1950 if (Uses.erase(Reg)) {
1951 if (Reg.isPhysical()) {
1952 for (MCPhysReg SubReg : TRI->subregs(Reg))
1953 Uses.erase(SubReg); // Use sub-registers to be conservative
1954 }
1955 }
1956 addRegAndItsAliases(Reg, TRI, Defs);
1957 }
1958 }
1959
1960 return PI;
1961}
1962
1963bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
1964 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1966 if (TII->analyzeBranch(*MBB, TBB, FBB, Cond, true) || !TBB || Cond.empty())
1967 return false;
1968
1969 if (!FBB) FBB = findFalseBlock(MBB, TBB);
1970 if (!FBB)
1971 // Malformed bcc? True and false blocks are the same?
1972 return false;
1973
1974 // Restrict the optimization to cases where MBB is the only predecessor,
1975 // it is an obvious win.
1976 if (TBB->pred_size() > 1 || FBB->pred_size() > 1)
1977 return false;
1978
1979 // Find a suitable position to hoist the common instructions to. Also figure
1980 // out which registers are used or defined by instructions from the insertion
1981 // point to the end of the block.
1982 SmallSet<Register, 4> Uses, Defs;
1984 findHoistingInsertPosAndDeps(MBB, TII, TRI, Uses, Defs);
1985 if (Loc == MBB->end())
1986 return false;
1987
1988 bool HasDups = false;
1989 SmallSet<Register, 4> ActiveDefsSet, AllDefsSet;
1991 MachineBasicBlock::iterator FIB = FBB->begin();
1993 MachineBasicBlock::iterator FIE = FBB->end();
1994 MachineFunction &MF = *TBB->getParent();
1995 while (TIB != TIE && FIB != FIE) {
1996 // Skip dbg_value instructions. These do not count.
1997 TIB = skipDebugInstructionsForward(TIB, TIE, false);
1998 FIB = skipDebugInstructionsForward(FIB, FIE, false);
1999 if (TIB == TIE || FIB == FIE)
2000 break;
2001
2002 if (!TIB->isIdenticalTo(*FIB, MachineInstr::CheckKillDead))
2003 break;
2004
2005 if (TII->isPredicated(*TIB))
2006 // Hard to reason about register liveness with predicated instruction.
2007 break;
2008
2009 if (!TII->isSafeToMove(*TIB, TBB, MF))
2010 // Don't hoist the instruction if it isn't safe to move.
2011 break;
2012
2013 bool IsSafe = true;
2014 for (MachineOperand &MO : TIB->operands()) {
2015 // Don't attempt to hoist instructions with register masks.
2016 if (MO.isRegMask()) {
2017 IsSafe = false;
2018 break;
2019 }
2020 if (!MO.isReg())
2021 continue;
2022 Register Reg = MO.getReg();
2023 if (!Reg)
2024 continue;
2025 if (MO.isDef()) {
2026 if (Uses.count(Reg)) {
2027 // Avoid clobbering a register that's used by the instruction at
2028 // the point of insertion.
2029 IsSafe = false;
2030 break;
2031 }
2032
2033 if (Defs.count(Reg) && !MO.isDead()) {
2034 // Don't hoist the instruction if the def would be clobber by the
2035 // instruction at the point insertion. FIXME: This is overly
2036 // conservative. It should be possible to hoist the instructions
2037 // in BB2 in the following example:
2038 // BB1:
2039 // r1, eflag = op1 r2, r3
2040 // brcc eflag
2041 //
2042 // BB2:
2043 // r1 = op2, ...
2044 // = op3, killed r1
2045 IsSafe = false;
2046 break;
2047 }
2048 } else if (!ActiveDefsSet.count(Reg)) {
2049 if (Defs.count(Reg)) {
2050 // Use is defined by the instruction at the point of insertion.
2051 IsSafe = false;
2052 break;
2053 }
2054
2055 if (MO.isKill() && Uses.count(Reg))
2056 // Kills a register that's read by the instruction at the point of
2057 // insertion. Remove the kill marker.
2058 MO.setIsKill(false);
2059 }
2060 }
2061 if (!IsSafe)
2062 break;
2063
2064 bool DontMoveAcrossStore = true;
2065 if (!TIB->isSafeToMove(DontMoveAcrossStore))
2066 break;
2067
2068 // Remove kills from ActiveDefsSet, these registers had short live ranges.
2069 for (const MachineOperand &MO : TIB->all_uses()) {
2070 if (!MO.isKill())
2071 continue;
2072 Register Reg = MO.getReg();
2073 if (!Reg)
2074 continue;
2075 if (!AllDefsSet.count(Reg)) {
2076 continue;
2077 }
2078 if (Reg.isPhysical()) {
2079 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
2080 ActiveDefsSet.erase(*AI);
2081 } else {
2082 ActiveDefsSet.erase(Reg);
2083 }
2084 }
2085
2086 // Track local defs so we can update liveins.
2087 for (const MachineOperand &MO : TIB->all_defs()) {
2088 if (MO.isDead())
2089 continue;
2090 Register Reg = MO.getReg();
2091 if (!Reg || Reg.isVirtual())
2092 continue;
2093 addRegAndItsAliases(Reg, TRI, ActiveDefsSet);
2094 addRegAndItsAliases(Reg, TRI, AllDefsSet);
2095 }
2096
2097 HasDups = true;
2098 ++TIB;
2099 ++FIB;
2100 }
2101
2102 if (!HasDups)
2103 return false;
2104
2105 // Hoist the instructions from [T.begin, TIB) and then delete [F.begin, FIB).
2106 // If we're hoisting from a single block then just splice. Else step through
2107 // and merge the debug locations.
2108 if (TBB == FBB) {
2109 MBB->splice(Loc, TBB, TBB->begin(), TIB);
2110 } else {
2111 // Merge the debug locations, and hoist and kill the debug instructions from
2112 // both branches. FIXME: We could probably try harder to preserve some debug
2113 // instructions (but at least this isn't producing wrong locations).
2114 MachineInstrBuilder MIRBuilder(*MBB->getParent(), Loc);
2115 auto HoistAndKillDbgInstr = [MBB, Loc](MachineBasicBlock::iterator DI) {
2116 assert(DI->isDebugInstr() && "Expected a debug instruction");
2117 if (DI->isDebugRef()) {
2118 const TargetInstrInfo *TII =
2120 const MCInstrDesc &DBGV = TII->get(TargetOpcode::DBG_VALUE);
2121 DI = BuildMI(*MBB->getParent(), DI->getDebugLoc(), DBGV, false, 0,
2122 DI->getDebugVariable(), DI->getDebugExpression());
2123 MBB->insert(Loc, &*DI);
2124 return;
2125 }
2126 // Deleting a DBG_PHI results in an undef at the referenced DBG_INSTR_REF.
2127 if (DI->isDebugPHI()) {
2128 DI->eraseFromParent();
2129 return;
2130 }
2131 // Move DBG_LABELs without modifying them. Set DBG_VALUEs undef.
2132 if (!DI->isDebugLabel())
2133 DI->setDebugValueUndef();
2134 DI->moveBefore(&*Loc);
2135 };
2136
2137 // TIB and FIB point to the end of the regions to hoist/merge in TBB and
2138 // FBB.
2140 MachineBasicBlock::iterator FI = FBB->begin();
2143 // Hoist and kill debug instructions from FBB. After this loop FI points
2144 // to the next non-debug instruction to hoist (checked in assert after the
2145 // TBB debug instruction handling code).
2146 while (FI != FE && FI->isDebugInstr())
2147 HoistAndKillDbgInstr(FI++);
2148
2149 // Kill debug instructions before moving.
2150 if (TI->isDebugInstr()) {
2151 HoistAndKillDbgInstr(TI);
2152 continue;
2153 }
2154
2155 // FI and TI now point to identical non-debug instructions.
2156 assert(FI != FE && "Unexpected end of FBB range");
2157 // Pseudo probes are excluded from the range when identifying foldable
2158 // instructions, so we don't expect to see one now.
2159 assert(!TI->isPseudoProbe() && "Unexpected pseudo probe in range");
2160 // NOTE: The loop above checks CheckKillDead but we can't do that here as
2161 // it modifies some kill markers after the check.
2162 assert(TI->isIdenticalTo(*FI, MachineInstr::CheckDefs) &&
2163 "Expected non-debug lockstep");
2164
2165 // Drop undef flag on the hoisted instruction if it was not present in
2166 // both of the original ones.
2167 mergeUndefFlag(*TI, *FI);
2168
2169 // Merge debug locs on hoisted instructions.
2170 TI->setDebugLoc(
2171 DILocation::getMergedLocation(TI->getDebugLoc(), FI->getDebugLoc()));
2172 TI->moveBefore(&*Loc);
2173 ++FI;
2174 }
2175 }
2176
2177 FBB->erase(FBB->begin(), FIB);
2178
2179 if (UpdateLiveIns)
2180 fullyRecomputeLiveIns({TBB, FBB});
2181
2182 ++NumHoist;
2183 return true;
2184}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file implements the BitVector class.
static unsigned EstimateRuntime(MachineBasicBlock::iterator I, MachineBasicBlock::iterator E)
EstimateRuntime - Make a rough estimate for how long it will take to run the specified code.
static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2, MachineBasicBlock::iterator &I1, MachineBasicBlock::iterator &I2)
Given two machine basic blocks, return the number of instructions they actually have in common togeth...
static void mergeUndefFlag(MachineInstr &Merged, const MachineInstr &Other)
Ensure undef flag is preserved only when it is present in both instructions.
static MachineBasicBlock * findFalseBlock(MachineBasicBlock *BB, MachineBasicBlock *TrueBB)
findFalseBlock - BB has a fallthrough.
static void copyDebugInfoToPredecessor(const TargetInstrInfo *TII, MachineBasicBlock &MBB, MachineBasicBlock &PredMBB)
static unsigned HashMachineInstr(const MachineInstr &MI)
HashMachineInstr - Compute a hash value for MI and its operands.
static bool countsAsInstruction(const MachineInstr &MI)
Whether MI should be counted as an instruction when calculating common tail.
static cl::opt< cl::boolOrDefault > FlagEnableTailMerge("enable-tail-merge", cl::init(cl::boolOrDefault::BOU_UNSET), cl::Hidden)
static unsigned CountTerminators(MachineBasicBlock *MBB, MachineBasicBlock::iterator &I)
CountTerminators - Count the number of terminators in the given block and set I to the position of th...
static bool blockEndsInUnreachable(const MachineBasicBlock *MBB)
A no successor, non-return block probably ends in unreachable and is cold.
static void salvageDebugInfoFromEmptyBlock(const TargetInstrInfo *TII, MachineBasicBlock &MBB)
static MachineBasicBlock::iterator skipBackwardPastNonInstructions(MachineBasicBlock::iterator I, MachineBasicBlock *MBB)
Iterate backwards from the given iterator I, towards the beginning of the block.
static cl::opt< unsigned > TailMergeThreshold("tail-merge-threshold", cl::desc("Max number of predecessors to consider tail merging"), cl::init(150), cl::Hidden)
static void addRegAndItsAliases(Register Reg, const TargetRegisterInfo *TRI, Container &Set)
static cl::opt< unsigned > TailMergeSize("tail-merge-size", cl::desc("Min number of instructions to consider tail merging"), cl::init(3), cl::Hidden)
static bool IsEmptyBlock(MachineBasicBlock *MBB)
static bool ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2, unsigned MinCommonTailLength, unsigned &CommonTailLen, MachineBasicBlock::iterator &I1, MachineBasicBlock::iterator &I2, MachineBasicBlock *SuccBB, MachineBasicBlock *PredBB, DenseMap< const MachineBasicBlock *, int > &EHScopeMembership, bool AfterPlacement, MBFIWrapper &MBBFreqInfo, ProfileSummaryInfo *PSI)
ProfitableToMerge - Check if two machine basic blocks have a common tail and decide if it would be pr...
static void copyDebugInfoToSuccessor(const TargetInstrInfo *TII, MachineBasicBlock &MBB, MachineBasicBlock &SuccMBB)
static bool IsBranchOnlyBlock(MachineBasicBlock *MBB)
static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB, const TargetInstrInfo *TII, const DebugLoc &BranchDL)
static bool IsBetterFallthrough(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2)
IsBetterFallthrough - Return true if it would be clearly better to fall-through to MBB1 than to fall ...
static unsigned HashEndOfMBB(const MachineBasicBlock &MBB)
HashEndOfMBB - Hash the last instruction in the MBB.
static void mergeOperations(MachineBasicBlock::iterator MBBIStartPos, MachineBasicBlock &MBBCommon)
static MachineBasicBlock::iterator findHoistingInsertPosAndDeps(MachineBasicBlock *MBB, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, SmallSet< Register, 4 > &Uses, SmallSet< Register, 4 > &Defs)
findHoistingInsertPosAndDeps - Find the location to move common instructions in successors to.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
A common definition of LaneBitmask for use in TableGen and CodeGen.
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
Target-Independent Code Generator Pass Configuration Options pass.
AnalysisUsage & addRequired()
LLVM Basic Block Representation.
Definition BasicBlock.h:62
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
size_type size() const
Returns the number of bits in this bitvector.
Definition BitVector.h:178
bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, const TargetRegisterInfo *tri, MachineLoopInfo *mli=nullptr, bool AfterPlacement=false)
Perhaps branch folding, tail merging and other CFG optimizations on the given function.
BranchFolder(bool DefaultEnableTailMerge, bool CommonHoist, MBFIWrapper &FreqInfo, const MachineBranchProbabilityInfo &ProbInfo, ProfileSummaryInfo *PSI, unsigned MinTailLength=0)
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static LLVM_ABI DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
Attempts to merge LocA and LocB into a single location; see DebugLoc::getMergedLocation for more deta...
A debug info location.
Definition DebugLoc.h:126
static LLVM_ABI DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
Definition DebugLoc.cpp:172
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
bool empty() const
Definition DenseMap.h:173
iterator end()
Definition DenseMap.h:143
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:90
MCRegAliasIterator enumerates all registers aliasing Reg.
An RAII based helper class to modify MachineFunctionProperties when running pass.
bool isEHPad() const
Returns true if the block is a landing pad.
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI void moveBefore(MachineBasicBlock *NewAfter)
Move 'this' block before or after the specified block.
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
iterator_range< livein_iterator > liveins() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI iterator getFirstNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the first non-debug instruction in the basic block, or end().
LLVM_ABI void clearLiveIns()
Clear live in list.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
bool hasAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI void copySuccessor(const MachineBasicBlock *Orig, succ_iterator I)
Copy a successor (and any probability info) from original block to this block's.
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_ABI iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
LLVM_ABI void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Given a machine basic block that branched to 'Old', change the code and CFG so that it branches to 'N...
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
LLVM_ABI DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
reverse_iterator rbegin()
bool isMachineBlockAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI void moveAfter(MachineBasicBlock *NewBefore)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & back() const
BasicBlockListType::iterator iterator
void eraseAdditionalCallInfo(const MachineInstr *MI)
Following functions update call site info.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void erase(iterator MBBI)
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
bool isBarrier(QueryType Type=AnyInBundle) const
Returns true if the specified instruction stops control flow from executing the instruction immediate...
unsigned getNumOperands() const
Retuns the total number of operands.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
void RemoveJumpTable(unsigned Idx)
RemoveJumpTable - Mark the specific index as being dead.
const std::vector< MachineJumpTableEntry > & getJumpTables() const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsUndef(bool Val=true)
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_FrameIndex
Abstract Stack Frame Index.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool tracksLiveness() const
tracksLiveness - Returns true when tracking register liveness accurately.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Analysis providing profile information.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
bool erase(const T &V)
Definition SmallSet.h:200
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
bool requiresStructuredCFG() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
self_iterator getIterator()
Definition ilist_node.h:123
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
initializer< Ty > init(const Ty &Val)
constexpr double e
iterator end() const
Definition BasicBlock.h:89
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Other
Any other memory.
Definition ModRef.h:68
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition STLExtras.h:1596
LLVM_ABI void computeLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB)
Computes registers live-in to MBB assuming all of its successors live-in lists are up-to-date.
LLVM_ABI char & BranchFolderPassID
BranchFolding - This pass performs machine code CFG based optimizations to delete branches to branche...
IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It, then continue decrementing it while it points to a debug instruction.
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
LLVM_ABI void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.
LLVM_ABI DenseMap< const MachineBasicBlock *, int > getEHScopeMembership(const MachineFunction &MF)
Definition Analysis.cpp:757
static constexpr LaneBitmask getAll()
Definition LaneBitmask.h:82