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