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