LLVM 23.0.0git
TailDuplicator.cpp
Go to the documentation of this file.
1//===- TailDuplicator.cpp - Duplicate blocks into predecessors' tails -----===//
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 utility class duplicates basic blocks ending in unconditional branches
10// into the tails of their predecessors.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SetVector.h"
21#include "llvm/ADT/Statistic.h"
34#include "llvm/IR/DebugLoc.h"
35#include "llvm/IR/Function.h"
37#include "llvm/Support/Debug.h"
41#include <cassert>
42#include <iterator>
43#include <utility>
44
45using namespace llvm;
46
47#define DEBUG_TYPE "tailduplication"
48
49STATISTIC(NumTails, "Number of tails duplicated");
50STATISTIC(NumTailDups, "Number of tail duplicated blocks");
51STATISTIC(NumTailDupAdded,
52 "Number of instructions added due to tail duplication");
53STATISTIC(NumTailDupRemoved,
54 "Number of instructions removed due to tail duplication");
55STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
56STATISTIC(NumAddedPHIs, "Number of phis added");
57
58// Heuristic for tail duplication.
60 "tail-dup-size",
61 cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2),
63
65 "tail-dup-indirect-size",
66 cl::desc("Maximum instructions to consider tail duplicating blocks that "
67 "end with indirect branches."), cl::init(20),
69
71 TailDupPredSize("tail-dup-pred-size",
72 cl::desc("Maximum predecessors (maximum successors at the "
73 "same time) to consider tail duplicating blocks."),
74 cl::init(16), cl::Hidden);
75
77 TailDupSuccSize("tail-dup-succ-size",
78 cl::desc("Maximum successors (maximum predecessors at the "
79 "same time) to consider tail duplicating blocks."),
80 cl::init(16), cl::Hidden);
81
82static cl::opt<bool>
83 TailDupVerify("tail-dup-verify",
84 cl::desc("Verify sanity of PHI instructions during taildup"),
85 cl::init(false), cl::Hidden);
86
87static cl::opt<unsigned> TailDupLimit("tail-dup-limit", cl::init(~0U),
89
90void TailDuplicator::initMF(MachineFunction &MFin, bool PreRegAlloc,
91 const MachineBranchProbabilityInfo *MBPIin,
92 MBFIWrapper *MBFIin,
93 ProfileSummaryInfo *PSIin,
94 bool LayoutModeIn, unsigned TailDupSizeIn) {
95 MF = &MFin;
96 TII = MF->getSubtarget().getInstrInfo();
97 TRI = MF->getSubtarget().getRegisterInfo();
98 MRI = &MF->getRegInfo();
99 MBPI = MBPIin;
100 MBFI = MBFIin;
101 PSI = PSIin;
102 TailDupSize = TailDupSizeIn;
103
104 assert(MBPI != nullptr && "Machine Branch Probability Info required");
105
106 LayoutMode = LayoutModeIn;
107 this->PreRegAlloc = PreRegAlloc;
108}
109
110static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) {
113 MBB.pred_end());
115 while (MI != MBB.end()) {
116 if (!MI->isPHI())
117 break;
118 for (MachineBasicBlock *PredBB : Preds) {
119 bool Found = false;
120 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
121 MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
122 if (PHIBB == PredBB) {
123 Found = true;
124 break;
125 }
126 }
127 if (!Found) {
128 dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
129 << *MI;
130 dbgs() << " missing input from predecessor "
131 << printMBBReference(*PredBB) << '\n';
132 llvm_unreachable(nullptr);
133 }
134 }
135
136 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
137 MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
138 if (CheckExtra && !Preds.count(PHIBB)) {
139 dbgs() << "Warning: malformed PHI in " << printMBBReference(MBB)
140 << ": " << *MI;
141 dbgs() << " extra input from predecessor "
142 << printMBBReference(*PHIBB) << '\n';
143 llvm_unreachable(nullptr);
144 }
145 if (PHIBB->getNumber() < 0) {
146 dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
147 << *MI;
148 dbgs() << " non-existing " << printMBBReference(*PHIBB) << '\n';
149 llvm_unreachable(nullptr);
150 }
151 }
152 ++MI;
153 }
154 }
155}
156
157/// Tail duplicate the block and cleanup.
158/// \p IsSimple - return value of isSimpleBB
159/// \p MBB - block to be duplicated
160/// \p ForcedLayoutPred - If non-null, treat this block as the layout
161/// predecessor, instead of using the ordering in MF
162/// \p DuplicatedPreds - if non-null, \p DuplicatedPreds will contain a list of
163/// all Preds that received a copy of \p MBB.
164/// \p RemovalCallback - if non-null, called just before MBB is deleted.
166 bool IsSimple, MachineBasicBlock *MBB,
167 MachineBasicBlock *ForcedLayoutPred,
169 function_ref<void(MachineBasicBlock *)> *RemovalCallback,
171 // Save the successors list.
173 MBB->succ_end());
174
177 if (!tailDuplicate(IsSimple, MBB, ForcedLayoutPred,
178 TDBBs, Copies, CandidatePtr))
179 return false;
180
181 ++NumTails;
182
184 MachineSSAUpdater SSAUpdate(*MF, &NewPHIs);
185
186 // TailBB's immediate successors are now successors of those predecessors
187 // which duplicated TailBB. Add the predecessors as sources to the PHI
188 // instructions.
189 bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken();
190 if (PreRegAlloc)
191 updateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
192
193 // If it is dead, remove it.
194 if (isDead) {
195 NumTailDupRemoved += MBB->size();
196 removeDeadBlock(MBB, RemovalCallback);
197 ++NumDeadBlocks;
198 }
199
200 // Update SSA form.
201 if (!SSAUpdateVRs.empty()) {
202 for (Register VReg : SSAUpdateVRs) {
203 SSAUpdate.Initialize(VReg);
204
205 // If the original definition is still around, add it as an available
206 // value.
207 MachineInstr *DefMI = MRI->getVRegDef(VReg);
208 MachineBasicBlock *DefBB = nullptr;
209 if (DefMI) {
210 DefBB = DefMI->getParent();
211 SSAUpdate.AddAvailableValue(DefBB, VReg);
212 }
213
214 // Add the new vregs as available values.
216 SSAUpdateVals.find(VReg);
217 for (std::pair<MachineBasicBlock *, Register> &J : LI->second) {
218 MachineBasicBlock *SrcBB = J.first;
219 Register SrcReg = J.second;
220 SSAUpdate.AddAvailableValue(SrcBB, SrcReg);
221 }
222
224 // Rewrite uses that are outside of the original def's block.
225 for (MachineOperand &UseMO :
226 llvm::make_early_inc_range(MRI->use_operands(VReg))) {
227 MachineInstr *UseMI = UseMO.getParent();
228 // Rewrite debug uses last so that they can take advantage of any
229 // register mappings introduced by other users in its BB, since we
230 // cannot create new register definitions specifically for the debug
231 // instruction (as debug instructions should not affect CodeGen).
232 if (UseMI->isDebugValue()) {
233 DebugUses.push_back(&UseMO);
234 continue;
235 }
236 if (UseMI->getParent() == DefBB && !UseMI->isPHI())
237 continue;
238 SSAUpdate.RewriteUse(UseMO);
239 }
240 for (auto *UseMO : DebugUses) {
241 MachineInstr *UseMI = UseMO->getParent();
242 UseMO->setReg(
243 SSAUpdate.GetValueInMiddleOfBlock(UseMI->getParent(), true));
244 }
245 }
246
247 SSAUpdateVRs.clear();
248 SSAUpdateVals.clear();
249 }
250
251 // Eliminate some of the copies inserted by tail duplication to maintain
252 // SSA form.
253 for (MachineInstr *Copy : Copies) {
254 if (!Copy->isCopy())
255 continue;
256 Register Dst = Copy->getOperand(0).getReg();
257 Register Src = Copy->getOperand(1).getReg();
258 if (MRI->hasOneNonDBGUse(Src) &&
259 MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) {
260 // Copy is the only use. Do trivial copy propagation here.
261 MRI->replaceRegWith(Dst, Src);
262 Copy->eraseFromParent();
263 }
264 }
265
266 if (NewPHIs.size())
267 NumAddedPHIs += NewPHIs.size();
268
269 if (DuplicatedPreds)
270 *DuplicatedPreds = std::move(TDBBs);
271
272 return true;
273}
274
275/// Look for small blocks that are unconditionally branched to and do not fall
276/// through. Tail-duplicate their instructions into their predecessors to
277/// eliminate (dynamic) branches.
279 bool MadeChange = false;
280
281 if (PreRegAlloc && TailDupVerify) {
282 LLVM_DEBUG(dbgs() << "\n*** Before tail-duplicating\n");
283 VerifyPHIs(*MF, true);
284 }
285
286 for (MachineBasicBlock &MBB :
288 if (NumTails == TailDupLimit)
289 break;
290
291 bool IsSimple = isSimpleBB(&MBB);
292
293 if (!shouldTailDuplicate(IsSimple, MBB))
294 continue;
295
296 MadeChange |= tailDuplicateAndUpdate(IsSimple, &MBB, nullptr);
297 }
298
299 if (PreRegAlloc && TailDupVerify)
300 VerifyPHIs(*MF, false);
301
302 return MadeChange;
303}
304
306 const MachineRegisterInfo *MRI) {
307 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
308 if (UseMI.isDebugValue())
309 continue;
310 if (UseMI.getParent() != BB)
311 return true;
312 }
313 return false;
314}
315
317 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
318 if (MI->getOperand(i + 1).getMBB() == SrcBB)
319 return i;
320 return 0;
321}
322
323// Remember which registers are used by phis in this block. This is
324// used to determine which registers are liveout while modifying the
325// block (which is why we need to copy the information).
327 DenseSet<Register> *UsedByPhi) {
328 for (const auto &MI : BB) {
329 if (!MI.isPHI())
330 break;
331 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
332 Register SrcReg = MI.getOperand(i).getReg();
333 UsedByPhi->insert(SrcReg);
334 }
335 }
336}
337
338/// Add a definition and source virtual registers pair for SSA update.
339void TailDuplicator::addSSAUpdateEntry(Register OrigReg, Register NewReg,
340 MachineBasicBlock *BB) {
342 SSAUpdateVals.find(OrigReg);
343 if (LI != SSAUpdateVals.end())
344 LI->second.push_back(std::make_pair(BB, NewReg));
345 else {
346 AvailableValsTy Vals;
347 Vals.push_back(std::make_pair(BB, NewReg));
348 SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
349 SSAUpdateVRs.push_back(OrigReg);
350 }
351}
352
353/// Process PHI node in TailBB by turning it into a copy in PredBB. Remember the
354/// source register that's contributed by PredBB and update SSA update map.
355void TailDuplicator::processPHI(
358 SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies,
359 const DenseSet<Register> &RegsUsedByPhi, bool Remove) {
360 Register DefReg = MI->getOperand(0).getReg();
361 unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
362 assert(SrcOpIdx && "Unable to find matching PHI source?");
363 Register SrcReg = MI->getOperand(SrcOpIdx).getReg();
364 unsigned SrcSubReg = MI->getOperand(SrcOpIdx).getSubReg();
365 const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
366 LocalVRMap.try_emplace(DefReg, SrcReg, SrcSubReg);
367
368 // Insert a copy from source to the end of the block. The def register is the
369 // available value liveout of the block.
370 Register NewDef = MRI->createVirtualRegister(RC);
371 Copies.push_back(std::make_pair(NewDef, RegSubRegPair(SrcReg, SrcSubReg)));
372 if (!Remove) {
373 // Informing MachineSSAUpdater that DefReg -> NewDef in PredBB is not
374 // correct, because it could be used to update on other PHI. But the DefReg
375 // in the COPY will be properly updated by MachineSSAUpdater.
376 MI->getOperand(SrcOpIdx).setReg(NewDef);
377 MI->getOperand(SrcOpIdx).setSubReg(0);
378 return;
379 }
380 if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg))
381 addSSAUpdateEntry(DefReg, NewDef, PredBB);
382
383 MI->removePHIIncomingValueFor(*PredBB);
384
385 if (MI->getNumOperands() == 1 && !TailBB->hasAddressTaken())
386 MI->eraseFromParent();
387 else if (MI->getNumOperands() == 1)
388 MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
389}
390
391/// Duplicate a TailBB instruction to PredBB and update
392/// the source operands due to earlier PHI translation.
393void TailDuplicator::duplicateInstruction(
396 const DenseSet<Register> &UsedByPhi) {
397 // Allow duplication of CFI instructions.
398 if (MI->isCFIInstruction()) {
399 BuildMI(*PredBB, PredBB->end(), PredBB->findDebugLoc(PredBB->begin()),
400 TII->get(TargetOpcode::CFI_INSTRUCTION))
401 .addCFIIndex(MI->getOperand(0).getCFIIndex())
402 .setMIFlags(MI->getFlags());
403 return;
404 }
405 MachineInstr &NewMI = TII->duplicate(*PredBB, PredBB->end(), *MI);
406 if (!PreRegAlloc)
407 return;
408 for (unsigned i = 0, e = NewMI.getNumOperands(); i != e; ++i) {
409 MachineOperand &MO = NewMI.getOperand(i);
410 if (!MO.isReg())
411 continue;
412 Register Reg = MO.getReg();
413 if (!Reg.isVirtual())
414 continue;
415 if (MO.isDef()) {
416 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
417 Register NewReg = MRI->createVirtualRegister(RC);
418 MO.setReg(NewReg);
419 LocalVRMap.try_emplace(Reg, NewReg, 0);
420 if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg))
421 addSSAUpdateEntry(Reg, NewReg, PredBB);
422 continue;
423 }
424 auto VI = LocalVRMap.find(Reg);
425 if (VI == LocalVRMap.end())
426 continue;
427 // Need to make sure that the register class of the mapped register
428 // will satisfy the constraints of the class of the register being
429 // replaced.
430 auto *OrigRC = MRI->getRegClass(Reg);
431 auto *MappedRC = MRI->getRegClass(VI->second.Reg);
432 const TargetRegisterClass *ConstrRC;
433 if (VI->second.SubReg != 0) {
434 ConstrRC =
435 TRI->getMatchingSuperRegClass(MappedRC, OrigRC, VI->second.SubReg);
436 if (ConstrRC) {
437 // The actual constraining (as in "find appropriate new class")
438 // is done by getMatchingSuperRegClass, so now we only need to
439 // change the class of the mapped register.
440 MRI->setRegClass(VI->second.Reg, ConstrRC);
441 }
442 } else {
443 // For mapped registers that do not have sub-registers, simply
444 // restrict their class to match the original one.
445
446 // We don't want debug instructions affecting the resulting code so
447 // if we're cloning a debug instruction then just use MappedRC
448 // rather than constraining the register class further.
449 ConstrRC = NewMI.isDebugInstr()
450 ? MappedRC
451 : MRI->constrainRegClass(VI->second.Reg, OrigRC);
452 }
453
454 if (ConstrRC) {
455 // If the class constraining succeeded, we can simply replace
456 // the old register with the mapped one.
457 MO.setReg(VI->second.Reg);
458 // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a
459 // sub-register, we need to compose the sub-register indices.
460 MO.setSubReg(
461 TRI->composeSubRegIndices(VI->second.SubReg, MO.getSubReg()));
462 } else {
463 // The direct replacement is not possible, due to failing register
464 // class constraints. An explicit COPY is necessary. Create one
465 // that can be reused.
466 Register NewReg = MRI->createVirtualRegister(OrigRC);
467 BuildMI(*PredBB, NewMI, NewMI.getDebugLoc(), TII->get(TargetOpcode::COPY),
468 NewReg)
469 .addReg(VI->second.Reg, {}, VI->second.SubReg);
470 LocalVRMap.erase(VI);
471 LocalVRMap.try_emplace(Reg, NewReg, 0);
472 MO.setReg(NewReg);
473 // The composed VI.Reg:VI.SubReg is replaced with NewReg, which
474 // is equivalent to the whole register Reg. Hence, Reg:subreg
475 // is same as NewReg:subreg, so keep the sub-register index
476 // unchanged.
477 }
478 // Clear any kill flags from this operand. The new register could
479 // have uses after this one, so kills are not valid here.
480 MO.setIsKill(false);
481 }
482}
483
484/// After FromBB is tail duplicated into its predecessor blocks, the successors
485/// have gained new predecessors. Update the PHI instructions in them
486/// accordingly.
487void TailDuplicator::updateSuccessorsPHIs(
488 MachineBasicBlock *FromBB, bool isDead,
491 for (MachineBasicBlock *SuccBB : Succs) {
492 for (MachineInstr &MI : *SuccBB) {
493 if (!MI.isPHI())
494 break;
495 MachineInstrBuilder MIB(*FromBB->getParent(), MI);
496 unsigned Idx = 0;
497 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
498 MachineOperand &MO = MI.getOperand(i + 1);
499 if (MO.getMBB() == FromBB) {
500 Idx = i;
501 break;
502 }
503 }
504
505 assert(Idx != 0);
506 MachineOperand &MO0 = MI.getOperand(Idx);
507 Register Reg = MO0.getReg();
508 if (isDead) {
509 // Folded into the previous BB.
510 // There could be duplicate phi source entries. FIXME: Should sdisel
511 // or earlier pass fixed this?
512 for (unsigned i = MI.getNumOperands() - 2; i != Idx; i -= 2) {
513 MachineOperand &MO = MI.getOperand(i + 1);
514 if (MO.getMBB() == FromBB) {
515 MI.removeOperand(i + 1);
516 MI.removeOperand(i);
517 }
518 }
519 } else
520 Idx = 0;
521
522 // If Idx is set, the operands at Idx and Idx+1 must be removed.
523 // We reuse the location to avoid expensive removeOperand calls.
524
526 SSAUpdateVals.find(Reg);
527 if (LI != SSAUpdateVals.end()) {
528 // This register is defined in the tail block.
529 for (const std::pair<MachineBasicBlock *, Register> &J : LI->second) {
530 MachineBasicBlock *SrcBB = J.first;
531 // If we didn't duplicate a bb into a particular predecessor, we
532 // might still have added an entry to SSAUpdateVals to correcly
533 // recompute SSA. If that case, avoid adding a dummy extra argument
534 // this PHI.
535 if (!SrcBB->isSuccessor(SuccBB))
536 continue;
537
538 Register SrcReg = J.second;
539 if (Idx != 0) {
540 MI.getOperand(Idx).setReg(SrcReg);
541 MI.getOperand(Idx + 1).setMBB(SrcBB);
542 Idx = 0;
543 } else {
544 MIB.addReg(SrcReg).addMBB(SrcBB);
545 }
546 }
547 } else {
548 // Live in tail block, must also be live in predecessors.
549 for (MachineBasicBlock *SrcBB : TDBBs) {
550 if (Idx != 0) {
551 MI.getOperand(Idx).setReg(Reg);
552 MI.getOperand(Idx + 1).setMBB(SrcBB);
553 Idx = 0;
554 } else {
555 MIB.addReg(Reg).addMBB(SrcBB);
556 }
557 }
558 }
559 if (Idx != 0) {
560 MI.removeOperand(Idx + 1);
561 MI.removeOperand(Idx);
562 }
563 }
564 }
565}
566
567/// Determine if it is profitable to duplicate this block.
569 MachineBasicBlock &TailBB) {
570 // When doing tail-duplication during layout, the block ordering is in flux,
571 // so canFallThrough returns a result based on incorrect information and
572 // should just be ignored.
573 if (!LayoutMode && TailBB.canFallThrough())
574 return false;
575
576 // Don't try to tail-duplicate single-block loops.
577 if (TailBB.isSuccessor(&TailBB))
578 return false;
579
580 // Set the limit on the cost to duplicate. When optimizing for size,
581 // duplicate only one, because one branch instruction can be eliminated to
582 // compensate for the duplication.
583 unsigned MaxDuplicateCount;
584 if (TailDupSize == 0)
585 MaxDuplicateCount = TailDuplicateSize;
586 else
587 MaxDuplicateCount = TailDupSize;
588 if (llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI))
589 MaxDuplicateCount = 1;
590
591 // If the block to be duplicated ends in an unanalyzable fallthrough, don't
592 // duplicate it.
593 // A similar check is necessary in MachineBlockPlacement to make sure pairs of
594 // blocks with unanalyzable fallthrough get layed out contiguously.
595 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
597 if (TII->analyzeBranch(TailBB, PredTBB, PredFBB, PredCond) &&
598 TailBB.canFallThrough())
599 return false;
600
601 // If the target has hardware branch prediction that can handle indirect
602 // branches, duplicating them can often make them predictable when there
603 // are common paths through the code. The limit needs to be high enough
604 // to allow undoing the effects of tail merging and other optimizations
605 // that rearrange the predecessors of the indirect branch.
606
607 bool HasIndirectbr = false;
608 bool HasComputedGoto = false;
609 if (!TailBB.empty()) {
610 HasIndirectbr = TailBB.back().isIndirectBranch();
611 HasComputedGoto = TailBB.terminatorIsComputedGotoWithSuccessors();
612 }
613
614 if (HasIndirectbr && PreRegAlloc)
615 MaxDuplicateCount = TailDupIndirectBranchSize;
616
617 // Allow higher limits when the block has computed-gotos and running after
618 // register allocation. NB. This basically unfactors computed gotos that were
619 // factored early on in the compilation process to speed up edge based data
620 // flow. If we do not unfactor them again, it can seriously pessimize code
621 // with many computed jumps in the source code, such as interpreters.
622 // Therefore we do not restrict the computed gotos.
623 if (HasComputedGoto && !PreRegAlloc)
624 MaxDuplicateCount = std::max(MaxDuplicateCount, 10u);
625
626 // Check the instructions in the block to determine whether tail-duplication
627 // is invalid or unlikely to be profitable.
628 unsigned InstrCount = 0;
629 unsigned NumPhis = 0;
630 for (MachineInstr &MI : TailBB) {
631 // Non-duplicable things shouldn't be tail-duplicated.
632 // CFI instructions are marked as non-duplicable, because Darwin compact
633 // unwind info emission can't handle multiple prologue setups. In case of
634 // DWARF, allow them be duplicated, so that their existence doesn't prevent
635 // tail duplication of some basic blocks, that would be duplicated otherwise.
636 if (MI.isNotDuplicable() &&
638 !MI.isCFIInstruction()))
639 return false;
640
641 // Convergent instructions can be duplicated only if doing so doesn't add
642 // new control dependencies, which is what we're going to do here.
643 if (MI.isConvergent())
644 return false;
645
646 // Do not duplicate 'return' instructions if this is a pre-regalloc run.
647 // A return may expand into a lot more instructions (e.g. reload of callee
648 // saved registers) after PEI.
649 if (PreRegAlloc && MI.isReturn())
650 return false;
651
652 // Avoid duplicating calls before register allocation. Calls presents a
653 // barrier to register allocation so duplicating them may end up increasing
654 // spills.
655 if (PreRegAlloc && MI.isCall())
656 return false;
657
658 // TailDuplicator::appendCopies will erroneously place COPYs after
659 // INLINEASM_BR instructions after 4b0aa5724fea, which demonstrates the same
660 // bug that was fixed in f7a53d82c090.
661 // FIXME: Use findPHICopyInsertPoint() to find the correct insertion point
662 // for the COPY when replacing PHIs.
663 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
664 return false;
665
666 if (MI.isBundle())
667 InstrCount += MI.getBundleSize();
668 else if (!MI.isPHI() && !MI.isMetaInstruction())
669 InstrCount += 1;
670
671 if (InstrCount > MaxDuplicateCount)
672 return false;
673 NumPhis += MI.isPHI();
674 }
675
676 // Duplicating a BB which has both multiple predecessors and successors will
677 // may cause huge amount of PHI nodes. If we want to remove this limitation,
678 // we have to address https://github.com/llvm/llvm-project/issues/78578.
679 if (PreRegAlloc && TailBB.pred_size() > TailDupPredSize &&
680 TailBB.succ_size() > TailDupSuccSize) {
681 // If TailBB or any of its successors contains a phi, we may have to add a
682 // large number of additional phis with additional incoming values.
683 if (NumPhis != 0 || any_of(TailBB.successors(), [](MachineBasicBlock *MBB) {
684 return any_of(*MBB, [](MachineInstr &MI) { return MI.isPHI(); });
685 }))
686 return false;
687 }
688
689 // Check if any of the successors of TailBB has a PHI node in which the
690 // value corresponding to TailBB uses a subregister.
691 // If a phi node uses a register paired with a subregister, the actual
692 // "value type" of the phi may differ from the type of the register without
693 // any subregisters. Due to a bug, tail duplication may add a new operand
694 // without a necessary subregister, producing an invalid code. This is
695 // demonstrated by test/CodeGen/Hexagon/tail-dup-subreg-abort.ll.
696 // Disable tail duplication for this case for now, until the problem is
697 // fixed.
698 for (auto *SB : TailBB.successors()) {
699 for (auto &I : *SB) {
700 if (!I.isPHI())
701 break;
702 unsigned Idx = getPHISrcRegOpIdx(&I, &TailBB);
703 assert(Idx != 0);
704 MachineOperand &PU = I.getOperand(Idx);
705 if (PU.getSubReg() != 0)
706 return false;
707 }
708 }
709
710 if (HasIndirectbr && PreRegAlloc)
711 return true;
712
713 if (IsSimple)
714 return true;
715
716 if (!PreRegAlloc)
717 return true;
718
719 return canCompletelyDuplicateBB(TailBB);
720}
721
722/// True if this BB has only one unconditional jump.
724 if (TailBB->succ_size() != 1)
725 return false;
726 if (TailBB->pred_empty())
727 return false;
729 if (I == TailBB->end())
730 return true;
731 return I->isUnconditionalBranch();
732}
733
736 for (MachineBasicBlock *BB : A.successors())
737 if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI())
738 return true;
739
740 return false;
741}
742
743bool TailDuplicator::canCompletelyDuplicateBB(MachineBasicBlock &BB) {
744 for (MachineBasicBlock *PredBB : BB.predecessors()) {
745 if (PredBB->succ_size() > 1)
746 return false;
747
748 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
750 if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
751 return false;
752
753 if (!PredCond.empty())
754 return false;
755 }
756 return true;
757}
758
759bool TailDuplicator::duplicateSimpleBB(
761 const DenseSet<Register> &UsedByPhi) {
762 SmallPtrSet<MachineBasicBlock *, 8> Succs(llvm::from_range,
763 TailBB->successors());
765 bool Changed = false;
766 for (MachineBasicBlock *PredBB : Preds) {
767 if (PredBB->hasEHPadSuccessor() || PredBB->mayHaveInlineAsmBr())
768 continue;
769
770 if (bothUsedInPHI(*PredBB, Succs))
771 continue;
772
773 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
775 if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
776 continue;
777
778 Changed = true;
779 LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
780 << "From simple Succ: " << *TailBB);
781
782 MachineBasicBlock *NewTarget = *TailBB->succ_begin();
783 MachineBasicBlock *NextBB = PredBB->getNextNode();
784
785 // Make PredFBB explicit.
786 if (PredCond.empty())
787 PredFBB = PredTBB;
788
789 // Make fall through explicit.
790 if (!PredTBB)
791 PredTBB = NextBB;
792 if (!PredFBB)
793 PredFBB = NextBB;
794
795 // Redirect
796 if (PredFBB == TailBB)
797 PredFBB = NewTarget;
798 if (PredTBB == TailBB)
799 PredTBB = NewTarget;
800
801 // Make the branch unconditional if possible
802 if (PredTBB == PredFBB) {
803 PredCond.clear();
804 PredFBB = nullptr;
805 }
806
807 // Avoid adding fall through branches.
808 if (PredFBB == NextBB)
809 PredFBB = nullptr;
810 if (PredTBB == NextBB && PredFBB == nullptr)
811 PredTBB = nullptr;
812
813 auto DL = PredBB->findBranchDebugLoc();
814 TII->removeBranch(*PredBB);
815
816 if (!PredBB->isSuccessor(NewTarget))
817 PredBB->replaceSuccessor(TailBB, NewTarget);
818 else {
819 PredBB->removeSuccessor(TailBB, true);
820 assert(PredBB->succ_size() <= 1);
821 }
822
823 if (PredTBB)
824 TII->insertBranch(*PredBB, PredTBB, PredFBB, PredCond, DL);
825
826 TDBBs.push_back(PredBB);
827 }
828 return Changed;
829}
830
832 MachineBasicBlock *PredBB) {
833 // EH edges are ignored by analyzeBranch.
834 if (PredBB->succ_size() > 1)
835 return false;
836
837 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
839 if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
840 return false;
841 if (!PredCond.empty())
842 return false;
843 // FIXME: This is overly conservative; it may be ok to relax this in the
844 // future under more specific conditions. If TailBB is an INLINEASM_BR
845 // indirect target, we need to see if the edge from PredBB to TailBB is from
846 // an INLINEASM_BR in PredBB, and then also if that edge was from the
847 // indirect target list, fallthrough/default target, or potentially both. If
848 // it's both, TailDuplicator::tailDuplicate will remove the edge, corrupting
849 // the successor list in PredBB and predecessor list in TailBB.
850 if (TailBB->isInlineAsmBrIndirectTarget())
851 return false;
852 return true;
853}
854
855/// If it is profitable, duplicate TailBB's contents in each
856/// of its predecessors.
857/// \p IsSimple result of isSimpleBB
858/// \p TailBB Block to be duplicated.
859/// \p ForcedLayoutPred When non-null, use this block as the layout predecessor
860/// instead of the previous block in MF's order.
861/// \p TDBBs A vector to keep track of all blocks tail-duplicated
862/// into.
863/// \p Copies A vector of copy instructions inserted. Used later to
864/// walk all the inserted copies and remove redundant ones.
865bool TailDuplicator::tailDuplicate(bool IsSimple, MachineBasicBlock *TailBB,
866 MachineBasicBlock *ForcedLayoutPred,
870 LLVM_DEBUG(dbgs() << "\n*** Tail-duplicating " << printMBBReference(*TailBB)
871 << '\n');
872
873 bool ShouldUpdateTerminators = TailBB->canFallThrough();
874
875 DenseSet<Register> UsedByPhi;
876 getRegsUsedByPHIs(*TailBB, &UsedByPhi);
877
878 if (IsSimple)
879 return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi);
880
881 // Iterate through all the unique predecessors and tail-duplicate this
882 // block into them, if possible. Copying the list ahead of time also
883 // avoids trouble with the predecessor list reallocating.
884 bool Changed = false;
886 if (CandidatePtr)
887 Preds.insert_range(*CandidatePtr);
888 else
889 Preds.insert_range(TailBB->predecessors());
890
891 for (MachineBasicBlock *PredBB : Preds) {
892 assert(TailBB != PredBB &&
893 "Single-block loop should have been rejected earlier!");
894
895 if (!canTailDuplicate(TailBB, PredBB))
896 continue;
897
898 // Don't duplicate into a fall-through predecessor (at least for now).
899 // If profile is available, findDuplicateCandidates can choose better
900 // fall-through predecessor.
901 if (!(MF->getFunction().hasProfileData() && LayoutMode)) {
902 bool IsLayoutSuccessor = false;
903 if (ForcedLayoutPred)
904 IsLayoutSuccessor = (ForcedLayoutPred == PredBB);
905 else if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
906 IsLayoutSuccessor = true;
907 if (IsLayoutSuccessor)
908 continue;
909 }
910
911 LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
912 << "From Succ: " << *TailBB);
913
914 TDBBs.push_back(PredBB);
915
916 // Remove PredBB's unconditional branch.
917 TII->removeBranch(*PredBB);
918
919 // Clone the contents of TailBB into PredBB.
922 for (MachineInstr &MI : llvm::make_early_inc_range(*TailBB)) {
923 if (MI.isPHI()) {
924 // Replace the uses of the def of the PHI with the register coming
925 // from PredBB.
926 processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true);
927 } else {
928 // Replace def of virtual registers with new registers, and update
929 // uses with PHI source register or the new registers.
930 duplicateInstruction(&MI, TailBB, PredBB, LocalVRMap, UsedByPhi);
931 }
932 }
933 appendCopies(PredBB, CopyInfos, Copies);
934
935 NumTailDupAdded += TailBB->size() - 1; // subtract one for removed branch
936
937 // Update the CFG.
938 PredBB->removeSuccessor(PredBB->succ_begin());
939 assert(PredBB->succ_empty() &&
940 "TailDuplicate called on block with multiple successors!");
941 for (MachineBasicBlock *Succ : TailBB->successors())
942 PredBB->addSuccessor(Succ, MBPI->getEdgeProbability(TailBB, Succ));
943
944 // Update branches in pred to jump to tail's layout successor if needed.
945 if (ShouldUpdateTerminators)
946 PredBB->updateTerminator(TailBB->getNextNode());
947
948 Changed = true;
949 ++NumTailDups;
950 }
951
952 // If TailBB was duplicated into all its predecessors except for the prior
953 // block, which falls through unconditionally, move the contents of this
954 // block into the prior block.
955 MachineBasicBlock *PrevBB = ForcedLayoutPred;
956 if (!PrevBB)
957 PrevBB = &*std::prev(TailBB->getIterator());
958 MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
960 // This has to check PrevBB->succ_size() because EH edges are ignored by
961 // analyzeBranch.
962 if (PrevBB->succ_size() == 1 &&
963 // Layout preds are not always CFG preds. Check.
964 *PrevBB->succ_begin() == TailBB &&
965 !TII->analyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond) &&
966 PriorCond.empty() &&
967 (!PriorTBB || PriorTBB == TailBB) &&
968 TailBB->pred_size() == 1 &&
969 !TailBB->hasAddressTaken()) {
970 LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
971 << "From MBB: " << *TailBB);
972 // There may be a branch to the layout successor. This is unlikely but it
973 // happens. The correct thing to do is to remove the branch before
974 // duplicating the instructions in all cases.
975 bool RemovedBranches = TII->removeBranch(*PrevBB) != 0;
976
977 // If there are still tail instructions, abort the merge
978 if (PrevBB->getFirstTerminator() == PrevBB->end()) {
979 if (PreRegAlloc) {
980 DenseMap<Register, RegSubRegPair> LocalVRMap;
983 // Process PHI instructions first.
984 while (I != TailBB->end() && I->isPHI()) {
985 // Replace the uses of the def of the PHI with the register coming
986 // from PredBB.
987 MachineInstr *MI = &*I++;
988 processPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi,
989 true);
990 }
991
992 // Now copy the non-PHI instructions.
993 while (I != TailBB->end()) {
994 // Replace def of virtual registers with new registers, and update
995 // uses with PHI source register or the new registers.
996 MachineInstr *MI = &*I++;
997 assert(!MI->isBundle() && "Not expecting bundles before regalloc!");
998 duplicateInstruction(MI, TailBB, PrevBB, LocalVRMap, UsedByPhi);
999 MI->eraseFromParent();
1000 }
1001 appendCopies(PrevBB, CopyInfos, Copies);
1002 } else {
1003 TII->removeBranch(*PrevBB);
1004 // No PHIs to worry about, just splice the instructions over.
1005 PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
1006 }
1007 PrevBB->removeSuccessor(PrevBB->succ_begin());
1008 assert(PrevBB->succ_empty());
1009 PrevBB->transferSuccessors(TailBB);
1010
1011 // Update branches in PrevBB based on Tail's layout successor.
1012 if (ShouldUpdateTerminators)
1013 PrevBB->updateTerminator(TailBB->getNextNode());
1014
1015 TDBBs.push_back(PrevBB);
1016 Changed = true;
1017 } else {
1018 LLVM_DEBUG(dbgs() << "Abort merging blocks, the predecessor still "
1019 "contains terminator instructions");
1020 // Return early if no changes were made
1021 if (!Changed)
1022 return RemovedBranches;
1023 }
1024 Changed |= RemovedBranches;
1025 }
1026
1027 // If this is after register allocation, there are no phis to fix.
1028 if (!PreRegAlloc)
1029 return Changed;
1030
1031 // If we made no changes so far, we are safe.
1032 if (!Changed)
1033 return Changed;
1034
1035 // Handle the nasty case in that we duplicated a block that is part of a loop
1036 // into some but not all of its predecessors. For example:
1037 // 1 -> 2 <-> 3 |
1038 // \ |
1039 // \---> rest |
1040 // if we duplicate 2 into 1 but not into 3, we end up with
1041 // 12 -> 3 <-> 2 -> rest |
1042 // \ / |
1043 // \----->-----/ |
1044 // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced
1045 // with a phi in 3 (which now dominates 2).
1046 // What we do here is introduce a copy in 3 of the register defined by the
1047 // phi, just like when we are duplicating 2 into 3, but we don't copy any
1048 // real instructions or remove the 3 -> 2 edge from the phi in 2.
1049 for (MachineBasicBlock *PredBB : Preds) {
1050 if (is_contained(TDBBs, PredBB))
1051 continue;
1052
1053 // EH edges
1054 if (PredBB->succ_size() != 1)
1055 continue;
1056
1057 DenseMap<Register, RegSubRegPair> LocalVRMap;
1059 // Process PHI instructions first.
1060 for (MachineInstr &MI : make_early_inc_range(TailBB->phis())) {
1061 // Replace the uses of the def of the PHI with the register coming
1062 // from PredBB.
1063 processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false);
1064 }
1065 appendCopies(PredBB, CopyInfos, Copies);
1066 }
1067
1068 return Changed;
1069}
1070
1071/// At the end of the block \p MBB generate COPY instructions between registers
1072/// described by \p CopyInfos. Append resulting instructions to \p Copies.
1073void TailDuplicator::appendCopies(MachineBasicBlock *MBB,
1074 SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos,
1077 const MCInstrDesc &CopyD = TII->get(TargetOpcode::COPY);
1078 for (auto &CI : CopyInfos) {
1079 auto C = BuildMI(*MBB, Loc, DebugLoc(), CopyD, CI.first)
1080 .addReg(CI.second.Reg, {}, CI.second.SubReg);
1081 Copies.push_back(C);
1082 }
1083}
1084
1085/// Remove the specified dead machine basic block from the function, updating
1086/// the CFG.
1087void TailDuplicator::removeDeadBlock(
1089 function_ref<void(MachineBasicBlock *)> *RemovalCallback) {
1090 assert(MBB->pred_empty() && "MBB must be dead!");
1091 LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
1092
1093 MachineFunction *MF = MBB->getParent();
1094 // Update the call info.
1095 for (const MachineInstr &MI : *MBB)
1096 if (MI.shouldUpdateAdditionalCallInfo())
1098
1099 if (RemovalCallback)
1100 (*RemovalCallback)(MBB);
1101
1102 // Remove all successors.
1103 while (!MBB->succ_empty())
1105
1106 // Remove the block.
1108}
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT, const SimplifyQuery &SQ)
static unsigned InstrCount
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
TargetInstrInfo::RegSubRegPair RegSubRegPair
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
SI Lower i1 Copies
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet 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
static cl::opt< unsigned > TailDuplicateSize("tail-dup-size", cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2), cl::Hidden)
static cl::opt< unsigned > TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden)
static cl::opt< unsigned > TailDupPredSize("tail-dup-pred-size", cl::desc("Maximum predecessors (maximum successors at the " "same time) to consider tail duplicating blocks."), cl::init(16), cl::Hidden)
static cl::opt< unsigned > TailDupSuccSize("tail-dup-succ-size", cl::desc("Maximum successors (maximum predecessors at the " "same time) to consider tail duplicating blocks."), cl::init(16), cl::Hidden)
static cl::opt< bool > TailDupVerify("tail-dup-verify", cl::desc("Verify sanity of PHI instructions during taildup"), cl::init(false), cl::Hidden)
static void VerifyPHIs(MachineFunction &MF, bool CheckExtra)
static bool bothUsedInPHI(const MachineBasicBlock &A, const SmallPtrSet< MachineBasicBlock *, 8 > &SuccsB)
static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB)
static void getRegsUsedByPHIs(const MachineBasicBlock &BB, DenseSet< Register > *UsedByPhi)
static cl::opt< unsigned > TailDupIndirectBranchSize("tail-dup-indirect-size", cl::desc("Maximum instructions to consider tail duplicating blocks that " "end with indirect branches."), cl::init(20), cl::Hidden)
static bool isDefLiveOut(Register Reg, MachineBasicBlock *BB, const MachineRegisterInfo *MRI)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:256
bool erase(const KeyT &Val)
Definition DenseMap.h:330
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
bool hasProfileData(bool IncludeSynthetic=false) const
Return true if the function is annotated with profile data.
Definition Function.h:336
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
LLVM_ABI bool hasEHPadSuccessor() const
LLVM_ABI void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
iterator_range< iterator > phis()
Returns a range that iterates over the phis in the basic block.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor)
Update the terminator instructions in block to account for changes to block layout which may have bee...
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 iterator getFirstNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the first non-debug instruction in the basic block, or end().
bool terminatorIsComputedGotoWithSuccessors() const
Returns true if the original IR terminator is an indirectbr with successor blocks.
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 removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
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...
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
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 bool mayHaveInlineAsmBr() const
Returns true if this block may have an INLINEASM_BR (overestimate, by checking if any of the successo...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
void eraseAdditionalCallInfo(const MachineInstr *MI)
Following functions update call site info.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
bool isDebugInstr() const
unsigned getNumOperands() const
Retuns the total number of operands.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
bool isIndirectBranch(QueryType Type=AnyInBundle) const
Return true if this is an indirect branch, such as a branch through a register.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
MachineSSAUpdater - This class updates SSA form for a set of virtual registers defined in multiple bl...
void Initialize(Register V)
Initialize - Reset this object to get ready for a new set of SSA updates.
Register GetValueInMiddleOfBlock(MachineBasicBlock *BB, bool ExistingValueOnly=false)
GetValueInMiddleOfBlock - Construct SSA form, materializing a value that is live in the middle of the...
void RewriteUse(MachineOperand &U)
RewriteUse - Rewrite a use of the symbolic value.
void AddAvailableValue(MachineBasicBlock *BB, Register V)
AddAvailableValue - Indicate that a rewritten value is available at the end of the specified block wi...
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
void insert_range(Range &&R)
Definition SetVector.h:176
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
void initMF(MachineFunction &MF, bool PreRegAlloc, const MachineBranchProbabilityInfo *MBPI, MBFIWrapper *MBFI, ProfileSummaryInfo *PSI, bool LayoutMode, unsigned TailDupSize=0)
Prepare to run on a specific machine function.
bool tailDuplicateBlocks()
Look for small blocks that are unconditionally branched to and do not fall through.
bool tailDuplicateAndUpdate(bool IsSimple, MachineBasicBlock *MBB, MachineBasicBlock *ForcedLayoutPred, SmallVectorImpl< MachineBasicBlock * > *DuplicatedPreds=nullptr, function_ref< void(MachineBasicBlock *)> *RemovalCallback=nullptr, SmallVectorImpl< MachineBasicBlock * > *CandidatePtr=nullptr)
Tail duplicate a single basic block into its predecessors, and then clean up.
static bool isSimpleBB(MachineBasicBlock *TailBB)
True if this BB has only one unconditional jump.
bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB)
Returns true if TailBB can successfully be duplicated into PredBB.
bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB)
Determine if it is profitable to duplicate this block.
const TargetRegisterInfo & getRegisterInfo() const
const Triple & getTargetTriple() const
virtual const TargetInstrInfo * getInstrInfo() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:644
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto successors(const MachineBasicBlock *BB)
constexpr from_range_t from_range
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
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:1745
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.