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