LLVM 23.0.0git
MachineSink.cpp
Go to the documentation of this file.
1//===- MachineSink.cpp - Sinking for machine instructions -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass moves instructions into successor blocks when possible, so that
10// they aren't executed on paths where their results aren't needed.
11//
12// This pass is not intended to be a replacement or a complete alternative
13// for an LLVM-IR-level sinking pass. It is only designed to sink simple
14// constructs that are not exposed before lowering and instruction selection.
15//
16//===----------------------------------------------------------------------===//
17
19#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/MapVector.h"
23#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/SmallSet.h"
26#include "llvm/ADT/Statistic.h"
28#include "llvm/Analysis/CFG.h"
55#include "llvm/IR/BasicBlock.h"
57#include "llvm/IR/LLVMContext.h"
59#include "llvm/Pass.h"
62#include "llvm/Support/Debug.h"
64#include <cassert>
65#include <cstdint>
66#include <utility>
67#include <vector>
68
69using namespace llvm;
70
71#define DEBUG_TYPE "machine-sink"
72
73static cl::opt<bool>
74 SplitEdges("machine-sink-split",
75 cl::desc("Split critical edges during machine sinking"),
76 cl::init(true), cl::Hidden);
77
79 "machine-sink-bfi",
80 cl::desc("Use block frequency info to find successors to sink"),
81 cl::init(true), cl::Hidden);
82
84 "machine-sink-split-probability-threshold",
86 "Percentage threshold for splitting single-instruction critical edge. "
87 "If the branch threshold is higher than this threshold, we allow "
88 "speculative execution of up to 1 instruction to avoid branching to "
89 "splitted critical edge"),
90 cl::init(40), cl::Hidden);
91
93 "machine-sink-load-instrs-threshold",
94 cl::desc("Do not try to find alias store for a load if there is a in-path "
95 "block whose instruction number is higher than this threshold."),
96 cl::init(2000), cl::Hidden);
97
99 "machine-sink-load-blocks-threshold",
100 cl::desc("Do not try to find alias store for a load if the block number in "
101 "the straight line is higher than this threshold."),
102 cl::init(20), cl::Hidden);
103
104static cl::opt<bool>
105 SinkInstsIntoCycle("sink-insts-to-avoid-spills",
106 cl::desc("Sink instructions into cycles to avoid "
107 "register spills"),
108 cl::init(false), cl::Hidden);
109
111 "machine-sink-cycle-limit",
112 cl::desc(
113 "The maximum number of instructions considered for cycle sinking."),
114 cl::init(50), cl::Hidden);
115
116STATISTIC(NumSunk, "Number of machine instructions sunk");
117STATISTIC(NumCycleSunk, "Number of machine instructions sunk into a cycle");
118STATISTIC(NumSplit, "Number of critical edges split");
119STATISTIC(NumCoalesces, "Number of copies coalesced");
120STATISTIC(NumPostRACopySink, "Number of copies sunk after RA");
121
123
124namespace {
125
126class MachineSinking {
127 const TargetSubtargetInfo *STI = nullptr;
128 const TargetInstrInfo *TII = nullptr;
129 const TargetRegisterInfo *TRI = nullptr;
130 MachineRegisterInfo *MRI = nullptr; // Machine register information
131 MachineDominatorTree *DT = nullptr; // Machine dominator tree
132 MachinePostDominatorTree *PDT = nullptr; // Machine post dominator tree
133 MachineCycleInfo *CI = nullptr;
134 ProfileSummaryInfo *PSI = nullptr;
135 MachineBlockFrequencyInfo *MBFI = nullptr;
136 const MachineBranchProbabilityInfo *MBPI = nullptr;
137 AliasAnalysis *AA = nullptr;
138 RegisterClassInfo RegClassInfo;
139 TargetSchedModel SchedModel;
140 // Required for split critical edge
141 LiveIntervals *LIS;
143 LiveVariables *LV;
144 MachineLoopInfo *MLI;
145
146 // Remember which edges have been considered for breaking.
148 CEBCandidates;
149 // Memorize the register that also wanted to sink into the same block along
150 // a different critical edge.
151 // {register to sink, sink-to block} -> the first sink-from block.
152 // We're recording the first sink-from block because that (critical) edge
153 // was deferred until we see another register that's going to sink into the
154 // same block.
156 CEMergeCandidates;
157 // Remember which edges we are about to split.
158 // This is different from CEBCandidates since those edges
159 // will be split.
161
162 DenseSet<Register> RegsToClearKillFlags;
163
164 using AllSuccsCache =
166
167 /// DBG_VALUE pointer and flag. The flag is true if this DBG_VALUE is
168 /// post-dominated by another DBG_VALUE of the same variable location.
169 /// This is necessary to detect sequences such as:
170 /// %0 = someinst
171 /// DBG_VALUE %0, !123, !DIExpression()
172 /// %1 = anotherinst
173 /// DBG_VALUE %1, !123, !DIExpression()
174 /// Where if %0 were to sink, the DBG_VAUE should not sink with it, as that
175 /// would re-order assignments.
176 using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
177
178 using SinkItem = std::pair<MachineInstr *, MachineBasicBlock *>;
179
180 /// Record of DBG_VALUE uses of vregs in a block, so that we can identify
181 /// debug instructions to sink.
183
184 /// Record of debug variables that have had their locations set in the
185 /// current block.
186 DenseSet<DebugVariable> SeenDbgVars;
187
189 HasStoreCache;
190
193 StoreInstrCache;
194
195 /// Cached BB's register pressure.
197 CachedRegisterPressure;
198
199 bool EnableSinkAndFold;
200
201public:
202 MachineSinking(bool EnableSinkAndFold, MachineDominatorTree *DT,
208 : DT(DT), PDT(PDT), CI(CI), PSI(PSI), MBFI(MBFI), MBPI(MBPI), AA(AA),
209 LIS(LIS), SI(SI), LV(LV), MLI(MLI),
210 EnableSinkAndFold(EnableSinkAndFold) {}
211
212 bool run(MachineFunction &MF);
213
214 void releaseMemory() {
215 CEBCandidates.clear();
216 CEMergeCandidates.clear();
217 }
218
219private:
221 void ProcessDbgInst(MachineInstr &MI);
222 bool isLegalToBreakCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
223 MachineBasicBlock *To, bool BreakPHIEdge);
224 bool isWorthBreakingCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
226 MachineBasicBlock *&DeferredFromBlock);
227
228 bool hasStoreBetween(MachineBasicBlock *From, MachineBasicBlock *To,
230
231 /// Postpone the splitting of the given critical
232 /// edge (\p From, \p To).
233 ///
234 /// We do not split the edges on the fly. Indeed, this invalidates
235 /// the dominance information and thus triggers a lot of updates
236 /// of that information underneath.
237 /// Instead, we postpone all the splits after each iteration of
238 /// the main loop. That way, the information is at least valid
239 /// for the lifetime of an iteration.
240 ///
241 /// \return True if the edge is marked as toSplit, false otherwise.
242 /// False can be returned if, for instance, this is not profitable.
243 bool PostponeSplitCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
244 MachineBasicBlock *To, bool BreakPHIEdge);
245 bool SinkInstruction(MachineInstr &MI, bool &SawStore,
246 AllSuccsCache &AllSuccessors);
247
248 /// If we sink a COPY inst, some debug users of it's destination may no
249 /// longer be dominated by the COPY, and will eventually be dropped.
250 /// This is easily rectified by forwarding the non-dominated debug uses
251 /// to the copy source.
252 void SalvageUnsunkDebugUsersOfCopy(MachineInstr &,
253 MachineBasicBlock *TargetBlock);
254 bool AllUsesDominatedByBlock(Register Reg, MachineBasicBlock *MBB,
255 MachineBasicBlock *DefMBB, bool &BreakPHIEdge,
256 bool &LocalUse) const;
258 bool &BreakPHIEdge,
259 AllSuccsCache &AllSuccessors);
260
261 void FindCycleSinkCandidates(MachineCycle *Cycle, MachineBasicBlock *BB,
263
264 bool
265 aggressivelySinkIntoCycle(MachineCycle *Cycle, MachineInstr &I,
267
268 bool isProfitableToSinkTo(Register Reg, MachineInstr &MI,
270 MachineBasicBlock *SuccToSinkTo,
271 AllSuccsCache &AllSuccessors);
272
273 bool PerformTrivialForwardCoalescing(MachineInstr &MI,
275
276 bool PerformSinkAndFold(MachineInstr &MI, MachineBasicBlock *MBB);
277
279 GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
280 AllSuccsCache &AllSuccessors) const;
281
282 std::vector<unsigned> &getBBRegisterPressure(const MachineBasicBlock &MBB,
283 bool UseCache = true);
284
285 bool registerPressureSetExceedsLimit(unsigned NRegs,
286 const TargetRegisterClass *RC,
287 const MachineBasicBlock &MBB);
288
289 bool registerPressureExceedsLimit(const MachineBasicBlock &MBB);
290};
291
292class MachineSinkingLegacy : public MachineFunctionPass {
293public:
294 static char ID;
295
296 MachineSinkingLegacy() : MachineFunctionPass(ID) {
298 }
299
300 bool runOnMachineFunction(MachineFunction &MF) override;
301
302 void getAnalysisUsage(AnalysisUsage &AU) const override {
312 if (UseBlockFreqInfo) {
315 }
317 }
318};
319
320} // end anonymous namespace
321
322char MachineSinkingLegacy::ID = 0;
323
324char &llvm::MachineSinkingLegacyID = MachineSinkingLegacy::ID;
325
326INITIALIZE_PASS_BEGIN(MachineSinkingLegacy, DEBUG_TYPE, "Machine code sinking",
327 false, false)
333INITIALIZE_PASS_END(MachineSinkingLegacy, DEBUG_TYPE, "Machine code sinking",
335
336/// Return true if a target defined block prologue instruction interferes
337/// with a sink candidate.
344 for (MachineBasicBlock::const_iterator PI = BB->getFirstNonPHI(); PI != End;
345 ++PI) {
346 // Only check target defined prologue instructions
347 if (!TII->isBasicBlockPrologue(*PI))
348 continue;
349 for (auto &MO : MI.operands()) {
350 if (!MO.isReg())
351 continue;
352 Register Reg = MO.getReg();
353 if (!Reg)
354 continue;
355 if (MO.isUse()) {
356 if (Reg.isPhysical() &&
357 (TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
358 continue;
359 if (PI->modifiesRegister(Reg, TRI))
360 return true;
361 } else {
362 if (PI->readsRegister(Reg, TRI))
363 return true;
364 // Check for interference with non-dead defs
365 auto *DefOp = PI->findRegisterDefOperand(Reg, TRI, false, true);
366 if (DefOp && !DefOp->isDead())
367 return true;
368 }
369 }
370 }
371
372 return false;
373}
374
375bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
377 if (!MI.isCopy())
378 return false;
379
380 Register SrcReg = MI.getOperand(1).getReg();
381 Register DstReg = MI.getOperand(0).getReg();
382 if (!SrcReg.isVirtual() || !DstReg.isVirtual() ||
383 !MRI->hasOneNonDBGUse(SrcReg))
384 return false;
385
386 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
387 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
388 if (SRC != DRC)
389 return false;
390
391 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
392 if (DefMI->isCopyLike())
393 return false;
394 LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
395 LLVM_DEBUG(dbgs() << "*** to: " << MI);
396 MRI->replaceRegWith(DstReg, SrcReg);
397 MI.eraseFromParent();
398
399 // Conservatively, clear any kill flags, since it's possible that they are no
400 // longer correct.
401 MRI->clearKillFlags(SrcReg);
402
403 ++NumCoalesces;
404 return true;
405}
406
407bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
408 MachineBasicBlock *MBB) {
409 if (MI.isCopy() || MI.mayLoadOrStore() ||
410 MI.getOpcode() == TargetOpcode::REG_SEQUENCE)
411 return false;
412
413 // Don't sink instructions that the target prefers not to sink.
414 if (!TII->shouldSink(MI))
415 return false;
416
417 // Check if it's safe to move the instruction.
418 bool SawStore = true;
419 if (!MI.isSafeToMove(SawStore))
420 return false;
421
422 // Convergent operations may not be made control-dependent on additional
423 // values.
424 if (MI.isConvergent())
425 return false;
426
427 // Don't sink defs/uses of hard registers or if the instruction defines more
428 // than one register.
429 // Don't sink more than two register uses - it'll cover most of the cases and
430 // greatly simplifies the register pressure checks.
431 Register DefReg;
432 Register UsedRegA, UsedRegB;
433 for (const MachineOperand &MO : MI.operands()) {
434 if (MO.isImm() || MO.isRegMask() || MO.isRegLiveOut() || MO.isMetadata() ||
435 MO.isMCSymbol() || MO.isDbgInstrRef() || MO.isCFIIndex() ||
436 MO.isIntrinsicID() || MO.isPredicate() || MO.isShuffleMask())
437 continue;
438 if (!MO.isReg())
439 return false;
440
441 Register Reg = MO.getReg();
442 if (Reg == 0)
443 continue;
444
445 if (Reg.isVirtual()) {
446 if (MO.isDef()) {
447 if (DefReg)
448 return false;
449 DefReg = Reg;
450 continue;
451 }
452
453 if (UsedRegA == 0)
454 UsedRegA = Reg;
455 else if (UsedRegB == 0)
456 UsedRegB = Reg;
457 else
458 return false;
459 continue;
460 }
461
462 if (Reg.isPhysical() && MO.isUse() &&
463 (MRI->isConstantPhysReg(Reg) || TII->isIgnorableUse(MO)))
464 continue;
465
466 return false;
467 }
468
469 // Scan uses of the destination register. Every use, except the last, must be
470 // a copy, with a chain of copies terminating with either a copy into a hard
471 // register, or a load/store instruction where the use is part of the
472 // address (*not* the stored value).
473 using SinkInfo = std::pair<MachineInstr *, ExtAddrMode>;
474 SmallVector<SinkInfo> SinkInto;
475 SmallVector<Register> Worklist;
476
477 const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
478 const TargetRegisterClass *RCA =
479 UsedRegA == 0 ? nullptr : MRI->getRegClass(UsedRegA);
480 const TargetRegisterClass *RCB =
481 UsedRegB == 0 ? nullptr : MRI->getRegClass(UsedRegB);
482
483 Worklist.push_back(DefReg);
484 while (!Worklist.empty()) {
485 Register Reg = Worklist.pop_back_val();
486
487 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
488 ExtAddrMode MaybeAM;
489 MachineInstr &UseInst = *MO.getParent();
490 if (UseInst.isCopy()) {
491 Register DstReg;
492 if (const MachineOperand &O = UseInst.getOperand(0); O.isReg())
493 DstReg = O.getReg();
494 if (DstReg == 0)
495 return false;
496 if (DstReg.isVirtual()) {
497 Worklist.push_back(DstReg);
498 continue;
499 }
500 // If we are going to replace a copy, the original instruction must be
501 // as cheap as a copy.
502 if (!TII->isAsCheapAsAMove(MI))
503 return false;
504 // The hard register must be in the register class of the original
505 // instruction's destination register.
506 if (!RC->contains(DstReg))
507 return false;
508 } else if (UseInst.mayLoadOrStore()) {
509 ExtAddrMode AM;
510 if (!TII->canFoldIntoAddrMode(UseInst, Reg, MI, AM))
511 return false;
512 MaybeAM = AM;
513 } else {
514 return false;
515 }
516
517 if (UseInst.getParent() != MI.getParent()) {
518 // If the register class of the register we are replacing is a superset
519 // of any of the register classes of the operands of the materialized
520 // instruction don't consider that live range extended.
521 const TargetRegisterClass *RCS = MRI->getRegClass(Reg);
522 if (RCA && RCA->hasSuperClassEq(RCS))
523 RCA = nullptr;
524 else if (RCB && RCB->hasSuperClassEq(RCS))
525 RCB = nullptr;
526 if (RCA || RCB) {
527 if (RCA == nullptr) {
528 RCA = RCB;
529 RCB = nullptr;
530 }
531
532 unsigned NRegs = !!RCA + !!RCB;
533 if (RCA == RCB)
534 RCB = nullptr;
535
536 // Check we don't exceed register pressure at the destination.
537 const MachineBasicBlock &MBB = *UseInst.getParent();
538 if (RCB == nullptr) {
539 if (registerPressureSetExceedsLimit(NRegs, RCA, MBB))
540 return false;
541 } else if (registerPressureSetExceedsLimit(1, RCA, MBB) ||
542 registerPressureSetExceedsLimit(1, RCB, MBB)) {
543 return false;
544 }
545 }
546 }
547
548 SinkInto.emplace_back(&UseInst, MaybeAM);
549 }
550 }
551
552 if (SinkInto.empty())
553 return false;
554
555 // Now we know we can fold the instruction in all its users.
556 for (auto &[SinkDst, MaybeAM] : SinkInto) {
557 MachineInstr *New = nullptr;
558 LLVM_DEBUG(dbgs() << "Sinking copy of"; MI.dump(); dbgs() << "into";
559 SinkDst->dump());
560 if (SinkDst->isCopy()) {
561 // TODO: After performing the sink-and-fold, the original instruction is
562 // deleted. Its value is still available (in a hard register), so if there
563 // are debug instructions which refer to the (now deleted) virtual
564 // register they could be updated to refer to the hard register, in
565 // principle. However, it's not clear how to do that, moreover in some
566 // cases the debug instructions may need to be replicated proportionally
567 // to the number of the COPY instructions replaced and in some extreme
568 // cases we can end up with quadratic increase in the number of debug
569 // instructions.
570
571 // Sink a copy of the instruction, replacing a COPY instruction.
572 MachineBasicBlock::iterator InsertPt = SinkDst->getIterator();
573 Register DstReg = SinkDst->getOperand(0).getReg();
574 TII->reMaterialize(*SinkDst->getParent(), InsertPt, DstReg, 0, MI);
575 New = &*std::prev(InsertPt);
576 if (!New->getDebugLoc())
577 New->setDebugLoc(SinkDst->getDebugLoc());
578
579 // The operand registers of the "sunk" instruction have their live range
580 // extended and their kill flags may no longer be correct. Conservatively
581 // clear the kill flags.
582 if (UsedRegA)
583 MRI->clearKillFlags(UsedRegA);
584 if (UsedRegB)
585 MRI->clearKillFlags(UsedRegB);
586 } else {
587 // Fold instruction into the addressing mode of a memory instruction.
588 New = TII->emitLdStWithAddr(*SinkDst, MaybeAM);
589
590 // The registers of the addressing mode may have their live range extended
591 // and their kill flags may no longer be correct. Conservatively clear the
592 // kill flags.
593 if (Register R = MaybeAM.BaseReg; R.isValid() && R.isVirtual())
594 MRI->clearKillFlags(R);
595 if (Register R = MaybeAM.ScaledReg; R.isValid() && R.isVirtual())
596 MRI->clearKillFlags(R);
597 }
598 LLVM_DEBUG(dbgs() << "yielding"; New->dump());
599 // Clear the StoreInstrCache, since we may invalidate it by erasing.
600 if (SinkDst->mayStore() && !SinkDst->hasOrderedMemoryRef())
601 StoreInstrCache.clear();
602 SinkDst->eraseFromParent();
603 }
604
605 // Collect operands that need to be cleaned up because the registers no longer
606 // exist (in COPYs and debug instructions). We cannot delete instructions or
607 // clear operands while traversing register uses.
609 Worklist.push_back(DefReg);
610 while (!Worklist.empty()) {
611 Register Reg = Worklist.pop_back_val();
612 for (MachineOperand &MO : MRI->use_operands(Reg)) {
613 MachineInstr *U = MO.getParent();
614 assert((U->isCopy() || U->isDebugInstr()) &&
615 "Only debug uses and copies must remain");
616 if (U->isCopy())
617 Worklist.push_back(U->getOperand(0).getReg());
618 Cleanup.push_back(&MO);
619 }
620 }
621
622 // Delete the dead COPYs and clear operands in debug instructions
623 for (MachineOperand *MO : Cleanup) {
624 MachineInstr *I = MO->getParent();
625 if (I->isCopy()) {
626 I->eraseFromParent();
627 } else {
628 MO->setReg(0);
629 MO->setSubReg(0);
630 }
631 }
632
633 MI.eraseFromParent();
634 return true;
635}
636
637/// AllUsesDominatedByBlock - Return true if all uses of the specified register
638/// occur in blocks dominated by the specified block. If any use is in the
639/// definition block, then return false since it is never legal to move def
640/// after uses.
641bool MachineSinking::AllUsesDominatedByBlock(Register Reg,
642 MachineBasicBlock *MBB,
643 MachineBasicBlock *DefMBB,
644 bool &BreakPHIEdge,
645 bool &LocalUse) const {
646 assert(Reg.isVirtual() && "Only makes sense for vregs");
647
648 // Ignore debug uses because debug info doesn't affect the code.
649 if (MRI->use_nodbg_empty(Reg))
650 return true;
651
652 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
653 // into and they are all PHI nodes. In this case, machine-sink must break
654 // the critical edge first. e.g.
655 //
656 // %bb.1:
657 // Predecessors according to CFG: %bb.0
658 // ...
659 // %def = DEC64_32r %x, implicit-def dead %eflags
660 // ...
661 // JE_4 <%bb.37>, implicit %eflags
662 // Successors according to CFG: %bb.37 %bb.2
663 //
664 // %bb.2:
665 // %p = PHI %y, %bb.0, %def, %bb.1
666 if (all_of(MRI->use_nodbg_operands(Reg), [&](MachineOperand &MO) {
667 MachineInstr *UseInst = MO.getParent();
668 unsigned OpNo = MO.getOperandNo();
669 MachineBasicBlock *UseBlock = UseInst->getParent();
670 return UseBlock == MBB && UseInst->isPHI() &&
671 UseInst->getOperand(OpNo + 1).getMBB() == DefMBB;
672 })) {
673 BreakPHIEdge = true;
674 return true;
675 }
676
677 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
678 // Determine the block of the use.
679 MachineInstr *UseInst = MO.getParent();
680 unsigned OpNo = &MO - &UseInst->getOperand(0);
681 MachineBasicBlock *UseBlock = UseInst->getParent();
682 if (UseInst->isPHI()) {
683 // PHI nodes use the operand in the predecessor block, not the block with
684 // the PHI.
685 UseBlock = UseInst->getOperand(OpNo + 1).getMBB();
686 } else if (UseBlock == DefMBB) {
687 LocalUse = true;
688 return false;
689 }
690
691 // Check that it dominates.
692 if (!DT->dominates(MBB, UseBlock))
693 return false;
694 }
695
696 return true;
697}
698
699/// Return true if this machine instruction loads from global offset table or
700/// constant pool.
702 assert(MI.mayLoad() && "Expected MI that loads!");
703
704 // If we lost memory operands, conservatively assume that the instruction
705 // reads from everything..
706 if (MI.memoperands_empty())
707 return true;
708
709 for (MachineMemOperand *MemOp : MI.memoperands())
710 if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
711 if (PSV->isGOT() || PSV->isConstantPool())
712 return true;
713
714 return false;
715}
716
717void MachineSinking::FindCycleSinkCandidates(
718 MachineCycle *Cycle, MachineBasicBlock *BB,
719 SmallVectorImpl<MachineInstr *> &Candidates) {
720 for (auto &MI : *BB) {
721 LLVM_DEBUG(dbgs() << "CycleSink: Analysing candidate: " << MI);
722 if (MI.isMetaInstruction()) {
723 LLVM_DEBUG(dbgs() << "CycleSink: not sinking meta instruction\n");
724 continue;
725 }
726 if (!TII->shouldSink(MI)) {
727 LLVM_DEBUG(dbgs() << "CycleSink: Instruction not a candidate for this "
728 "target\n");
729 continue;
730 }
731 if (!isCycleInvariant(Cycle, MI)) {
732 LLVM_DEBUG(dbgs() << "CycleSink: Instruction is not cycle invariant\n");
733 continue;
734 }
735 bool DontMoveAcrossStore = true;
736 if (!MI.isSafeToMove(DontMoveAcrossStore)) {
737 LLVM_DEBUG(dbgs() << "CycleSink: Instruction not safe to move.\n");
738 continue;
739 }
740 if (MI.mayLoad() && !mayLoadFromGOTOrConstantPool(MI)) {
741 LLVM_DEBUG(dbgs() << "CycleSink: Dont sink GOT or constant pool loads\n");
742 continue;
743 }
744 if (MI.isConvergent())
745 continue;
746
747 const MachineOperand &MO = MI.getOperand(0);
748 if (!MO.isReg() || !MO.getReg() || !MO.isDef())
749 continue;
750 if (!MRI->hasOneDef(MO.getReg()))
751 continue;
752
753 LLVM_DEBUG(dbgs() << "CycleSink: Instruction added as candidate.\n");
754 Candidates.push_back(&MI);
755 }
756}
757
758PreservedAnalyses
761 auto *DT = &MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
762 auto *PDT = &MFAM.getResult<MachinePostDominatorTreeAnalysis>(MF);
763 auto *CI = &MFAM.getResult<MachineCycleAnalysis>(MF);
765 .getCachedResult<ProfileSummaryAnalysis>(
766 *MF.getFunction().getParent());
767 auto *MBFI = UseBlockFreqInfo
769 : nullptr;
770 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
772 .getManager()
773 .getResult<AAManager>(MF.getFunction());
774 auto *LIS = MFAM.getCachedResult<LiveIntervalsAnalysis>(MF);
775 auto *SI = MFAM.getCachedResult<SlotIndexesAnalysis>(MF);
776 auto *LV = MFAM.getCachedResult<LiveVariablesAnalysis>(MF);
777 auto *MLI = MFAM.getCachedResult<MachineLoopAnalysis>(MF);
778 MachineSinking Impl(EnableSinkAndFold, DT, PDT, LV, MLI, SI, LIS, CI, PSI,
779 MBFI, MBPI, AA);
780 bool Changed = Impl.run(MF);
781 if (!Changed)
782 return PreservedAnalyses::all();
784 PA.preserve<MachineCycleAnalysis>();
785 PA.preserve<MachineLoopAnalysis>();
787 PA.preserve<MachineBlockFrequencyAnalysis>();
788 return PA;
789}
790
792 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
793 OS << MapClassName2PassName(name()); // ideally machine-sink
794 if (EnableSinkAndFold)
795 OS << "<enable-sink-fold>";
796}
797
798bool MachineSinkingLegacy::runOnMachineFunction(MachineFunction &MF) {
799 if (skipFunction(MF.getFunction()))
800 return false;
801
802 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
803 bool EnableSinkAndFold = PassConfig->getEnableSinkAndFold();
804
805 auto *DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
806 auto *PDT =
807 &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
808 auto *CI = &getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo();
809 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
810 auto *MBFI =
812 ? &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI()
813 : nullptr;
814 auto *MBPI =
815 &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
816 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
817 // Get analyses for split critical edge.
818 auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
819 auto *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
820 auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
821 auto *SI = SIWrapper ? &SIWrapper->getSI() : nullptr;
822 auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
823 auto *LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
824 auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
825 auto *MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
826
827 MachineSinking Impl(EnableSinkAndFold, DT, PDT, LV, MLI, SI, LIS, CI, PSI,
828 MBFI, MBPI, AA);
829 return Impl.run(MF);
830}
831
832bool MachineSinking::run(MachineFunction &MF) {
833 LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n");
834
835 STI = &MF.getSubtarget();
836 TII = STI->getInstrInfo();
837 TRI = STI->getRegisterInfo();
838 MRI = &MF.getRegInfo();
839
840 RegClassInfo.runOnMachineFunction(MF);
841
842 bool EverMadeChange = false;
843
844 while (true) {
845 bool MadeChange = false;
846
847 // Process all basic blocks.
848 CEBCandidates.clear();
849 CEMergeCandidates.clear();
850 ToSplit.clear();
851 for (auto &MBB : MF)
852 MadeChange |= ProcessBlock(MBB);
853
854 // If we have anything we marked as toSplit, split it now.
855 MachineDomTreeUpdater MDTU(DT, PDT,
856 MachineDomTreeUpdater::UpdateStrategy::Lazy);
857 for (const auto &Pair : ToSplit) {
858 auto NewSucc = Pair.first->SplitCriticalEdge(
859 Pair.second, {LIS, SI, LV, MLI}, nullptr, &MDTU);
860 if (NewSucc != nullptr) {
861 LLVM_DEBUG(dbgs() << " *** Splitting critical edge: "
862 << printMBBReference(*Pair.first) << " -- "
863 << printMBBReference(*NewSucc) << " -- "
864 << printMBBReference(*Pair.second) << '\n');
865 if (MBFI)
866 MBFI->onEdgeSplit(*Pair.first, *NewSucc, *MBPI);
867
868 MadeChange = true;
869 ++NumSplit;
870 CI->splitCriticalEdge(Pair.first, Pair.second, NewSucc);
871 } else
872 LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n");
873 }
874 // If this iteration over the code changed anything, keep iterating.
875 if (!MadeChange)
876 break;
877 EverMadeChange = true;
878 }
879
880 if (SinkInstsIntoCycle) {
882 SchedModel.init(STI);
883 bool HasHighPressure;
884
885 DenseMap<SinkItem, MachineInstr *> SunkInstrs;
886
887 enum CycleSinkStage { COPY, LOW_LATENCY, AGGRESSIVE, END };
888 for (unsigned Stage = CycleSinkStage::COPY; Stage != CycleSinkStage::END;
889 ++Stage, SunkInstrs.clear()) {
890 HasHighPressure = false;
891
892 for (auto *Cycle : Cycles) {
893 MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
894 if (!Preheader) {
895 LLVM_DEBUG(dbgs() << "CycleSink: Can't find preheader\n");
896 continue;
897 }
898 SmallVector<MachineInstr *, 8> Candidates;
899 FindCycleSinkCandidates(Cycle, Preheader, Candidates);
900
901 unsigned i = 0;
902
903 // Walk the candidates in reverse order so that we start with the use
904 // of a def-use chain, if there is any.
905 // TODO: Sort the candidates using a cost-model.
906 for (MachineInstr *I : llvm::reverse(Candidates)) {
907 // CycleSinkStage::COPY: Sink a limited number of copies
908 if (Stage == CycleSinkStage::COPY) {
909 if (i++ == SinkIntoCycleLimit) {
911 << "CycleSink: Limit reached of instructions to "
912 "be analyzed.");
913 break;
914 }
915
916 if (!I->isCopy())
917 continue;
918 }
919
920 // CycleSinkStage::LOW_LATENCY: sink unlimited number of instructions
921 // which the target specifies as low-latency
922 if (Stage == CycleSinkStage::LOW_LATENCY &&
923 !TII->hasLowDefLatency(SchedModel, *I, 0))
924 continue;
925
926 if (!aggressivelySinkIntoCycle(Cycle, *I, SunkInstrs))
927 continue;
928 EverMadeChange = true;
929 ++NumCycleSunk;
930 }
931
932 // Recalculate the pressure after sinking
933 if (!HasHighPressure)
934 HasHighPressure = registerPressureExceedsLimit(*Preheader);
935 }
936 if (!HasHighPressure)
937 break;
938 }
939 }
940
941 HasStoreCache.clear();
942 StoreInstrCache.clear();
943
944 // Now clear any kill flags for recorded registers.
945 for (auto I : RegsToClearKillFlags)
946 MRI->clearKillFlags(I);
947 RegsToClearKillFlags.clear();
948
949 releaseMemory();
950 return EverMadeChange;
951}
952
953bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
954 if ((!EnableSinkAndFold && MBB.succ_size() <= 1) || MBB.empty())
955 return false;
956
957 // Don't bother sinking code out of unreachable blocks. In addition to being
958 // unprofitable, it can also lead to infinite looping, because in an
959 // unreachable cycle there may be nowhere to stop.
960 if (!DT->isReachableFromEntry(&MBB))
961 return false;
962
963 bool MadeChange = false;
964
965 // Cache all successors, sorted by frequency info and cycle depth.
966 AllSuccsCache AllSuccessors;
967
968 // Walk the basic block bottom-up. Remember if we saw a store.
970 --I;
971 bool ProcessedBegin, SawStore = false;
972 do {
973 MachineInstr &MI = *I; // The instruction to sink.
974
975 // Predecrement I (if it's not begin) so that it isn't invalidated by
976 // sinking.
977 ProcessedBegin = I == MBB.begin();
978 if (!ProcessedBegin)
979 --I;
980
981 if (MI.isDebugOrPseudoInstr() || MI.isFakeUse()) {
982 if (MI.isDebugValue())
983 ProcessDbgInst(MI);
984 continue;
985 }
986
987 if (EnableSinkAndFold && PerformSinkAndFold(MI, &MBB)) {
988 MadeChange = true;
989 continue;
990 }
991
992 // Can't sink anything out of a block that has less than two successors.
993 if (MBB.succ_size() <= 1)
994 continue;
995
996 if (PerformTrivialForwardCoalescing(MI, &MBB)) {
997 MadeChange = true;
998 continue;
999 }
1000
1001 if (SinkInstruction(MI, SawStore, AllSuccessors)) {
1002 ++NumSunk;
1003 MadeChange = true;
1004 }
1005
1006 // If we just processed the first instruction in the block, we're done.
1007 } while (!ProcessedBegin);
1008
1009 SeenDbgUsers.clear();
1010 SeenDbgVars.clear();
1011 // recalculate the bb register pressure after sinking one BB.
1012 CachedRegisterPressure.clear();
1013 return MadeChange;
1014}
1015
1016void MachineSinking::ProcessDbgInst(MachineInstr &MI) {
1017 // When we see DBG_VALUEs for registers, record any vreg it reads, so that
1018 // we know what to sink if the vreg def sinks.
1019 assert(MI.isDebugValue() && "Expected DBG_VALUE for processing");
1020
1021 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
1022 MI.getDebugLoc()->getInlinedAt());
1023 bool SeenBefore = SeenDbgVars.contains(Var);
1024
1025 for (MachineOperand &MO : MI.debug_operands()) {
1026 if (MO.isReg() && MO.getReg().isVirtual())
1027 SeenDbgUsers[MO.getReg()].push_back(SeenDbgUser(&MI, SeenBefore));
1028 }
1029
1030 // Record the variable for any DBG_VALUE, to avoid re-ordering any of them.
1031 SeenDbgVars.insert(Var);
1032}
1033
1034bool MachineSinking::isWorthBreakingCriticalEdge(
1035 MachineInstr &MI, MachineBasicBlock *From, MachineBasicBlock *To,
1036 MachineBasicBlock *&DeferredFromBlock) {
1037 // FIXME: Need much better heuristics.
1038
1039 // If the pass has already considered breaking this edge (during this pass
1040 // through the function), then let's go ahead and break it. This means
1041 // sinking multiple "cheap" instructions into the same block.
1042 if (!CEBCandidates.insert(std::make_pair(From, To)).second)
1043 return true;
1044
1045 if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
1046 return true;
1047
1048 // Check and record the register and the destination block we want to sink
1049 // into. Note that we want to do the following before the next check on branch
1050 // probability. Because we want to record the initial candidate even if it's
1051 // on hot edge, so that other candidates that might not on hot edges can be
1052 // sinked as well.
1053 for (const auto &MO : MI.all_defs()) {
1054 Register Reg = MO.getReg();
1055 if (!Reg)
1056 continue;
1057 Register SrcReg = Reg.isVirtual() ? TRI->lookThruCopyLike(Reg, MRI) : Reg;
1058 auto Key = std::make_pair(SrcReg, To);
1059 auto Res = CEMergeCandidates.try_emplace(Key, From);
1060 // We wanted to sink the same register into the same block, consider it to
1061 // be profitable.
1062 if (!Res.second) {
1063 // Return the source block that was previously held off.
1064 DeferredFromBlock = Res.first->second;
1065 return true;
1066 }
1067 }
1068
1069 if (From->isSuccessor(To) &&
1070 MBPI->getEdgeProbability(From, To) <=
1071 BranchProbability(SplitEdgeProbabilityThreshold, 100))
1072 return true;
1073
1074 // MI is cheap, we probably don't want to break the critical edge for it.
1075 // However, if this would allow some definitions of its source operands
1076 // to be sunk then it's probably worth it.
1077 for (const MachineOperand &MO : MI.all_uses()) {
1078 Register Reg = MO.getReg();
1079 if (Reg == 0)
1080 continue;
1081
1082 // We don't move live definitions of physical registers,
1083 // so sinking their uses won't enable any opportunities.
1084 if (Reg.isPhysical())
1085 continue;
1086
1087 // If this instruction is the only user of a virtual register,
1088 // check if breaking the edge will enable sinking
1089 // both this instruction and the defining instruction.
1090 if (MRI->hasOneNonDBGUse(Reg)) {
1091 // If the definition resides in same MBB,
1092 // claim it's likely we can sink these together.
1093 // If definition resides elsewhere, we aren't
1094 // blocking it from being sunk so don't break the edge.
1095 MachineInstr *DefMI = MRI->getVRegDef(Reg);
1096 if (DefMI->getParent() == MI.getParent())
1097 return true;
1098 }
1099 }
1100
1101 // Let the target decide if it's worth breaking this
1102 // critical edge for a "cheap" instruction.
1103 return TII->shouldBreakCriticalEdgeToSink(MI);
1104}
1105
1106bool MachineSinking::isLegalToBreakCriticalEdge(MachineInstr &MI,
1107 MachineBasicBlock *FromBB,
1108 MachineBasicBlock *ToBB,
1109 bool BreakPHIEdge) {
1110 // Avoid breaking back edge. From == To means backedge for single BB cycle.
1111 if (!SplitEdges || FromBB == ToBB || !FromBB->isSuccessor(ToBB))
1112 return false;
1113
1114 MachineCycle *FromCycle = CI->getCycle(FromBB);
1115 MachineCycle *ToCycle = CI->getCycle(ToBB);
1116
1117 // Check for backedges of more "complex" cycles.
1118 if (FromCycle == ToCycle && FromCycle &&
1119 (!FromCycle->isReducible() || FromCycle->getHeader() == ToBB))
1120 return false;
1121
1122 // It's not always legal to break critical edges and sink the computation
1123 // to the edge.
1124 //
1125 // %bb.1:
1126 // v1024
1127 // Beq %bb.3
1128 // <fallthrough>
1129 // %bb.2:
1130 // ... no uses of v1024
1131 // <fallthrough>
1132 // %bb.3:
1133 // ...
1134 // = v1024
1135 //
1136 // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted:
1137 //
1138 // %bb.1:
1139 // ...
1140 // Bne %bb.2
1141 // %bb.4:
1142 // v1024 =
1143 // B %bb.3
1144 // %bb.2:
1145 // ... no uses of v1024
1146 // <fallthrough>
1147 // %bb.3:
1148 // ...
1149 // = v1024
1150 //
1151 // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3
1152 // flow. We need to ensure the new basic block where the computation is
1153 // sunk to dominates all the uses.
1154 // It's only legal to break critical edge and sink the computation to the
1155 // new block if all the predecessors of "To", except for "From", are
1156 // not dominated by "From". Given SSA property, this means these
1157 // predecessors are dominated by "To".
1158 //
1159 // There is no need to do this check if all the uses are PHI nodes. PHI
1160 // sources are only defined on the specific predecessor edges.
1161 if (!BreakPHIEdge) {
1162 for (MachineBasicBlock *Pred : ToBB->predecessors())
1163 if (Pred != FromBB && !DT->dominates(ToBB, Pred))
1164 return false;
1165 }
1166
1167 return true;
1168}
1169
1170bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
1171 MachineBasicBlock *FromBB,
1172 MachineBasicBlock *ToBB,
1173 bool BreakPHIEdge) {
1174 bool Status = false;
1175 MachineBasicBlock *DeferredFromBB = nullptr;
1176 if (isWorthBreakingCriticalEdge(MI, FromBB, ToBB, DeferredFromBB)) {
1177 // If there is a DeferredFromBB, we consider FromBB only if _both_
1178 // of them are legal to split.
1179 if ((!DeferredFromBB ||
1180 ToSplit.count(std::make_pair(DeferredFromBB, ToBB)) ||
1181 isLegalToBreakCriticalEdge(MI, DeferredFromBB, ToBB, BreakPHIEdge)) &&
1182 isLegalToBreakCriticalEdge(MI, FromBB, ToBB, BreakPHIEdge)) {
1183 ToSplit.insert(std::make_pair(FromBB, ToBB));
1184 if (DeferredFromBB)
1185 ToSplit.insert(std::make_pair(DeferredFromBB, ToBB));
1186 Status = true;
1187 }
1188 }
1189
1190 return Status;
1191}
1192
1193std::vector<unsigned> &
1194MachineSinking::getBBRegisterPressure(const MachineBasicBlock &MBB,
1195 bool UseCache) {
1196 // Currently to save compiling time, MBB's register pressure will not change
1197 // in one ProcessBlock iteration because of CachedRegisterPressure. but MBB's
1198 // register pressure is changed after sinking any instructions into it.
1199 // FIXME: need a accurate and cheap register pressure estiminate model here.
1200
1201 auto RP = CachedRegisterPressure.find(&MBB);
1202 if (UseCache && RP != CachedRegisterPressure.end())
1203 return RP->second;
1204
1205 RegionPressure Pressure;
1206 RegPressureTracker RPTracker(Pressure);
1207
1208 // Initialize the register pressure tracker.
1209 RPTracker.init(MBB.getParent(), &RegClassInfo, nullptr, &MBB, MBB.end(),
1210 /*TrackLaneMasks*/ false, /*TrackUntiedDefs=*/true);
1211
1213 MIE = MBB.instr_begin();
1214 MII != MIE; --MII) {
1215 const MachineInstr &MI = *std::prev(MII);
1216 if (MI.isDebugOrPseudoInstr())
1217 continue;
1218 RegisterOperands RegOpers;
1219 RegOpers.collect(MI, *TRI, *MRI, false, false);
1220 RPTracker.recedeSkipDebugValues();
1221 assert(&*RPTracker.getPos() == &MI && "RPTracker sync error!");
1222 RPTracker.recede(RegOpers);
1223 }
1224
1225 RPTracker.closeRegion();
1226
1227 if (RP != CachedRegisterPressure.end()) {
1228 CachedRegisterPressure[&MBB] = RPTracker.getPressure().MaxSetPressure;
1229 return CachedRegisterPressure[&MBB];
1230 }
1231
1232 auto It = CachedRegisterPressure.insert(
1233 std::make_pair(&MBB, RPTracker.getPressure().MaxSetPressure));
1234 return It.first->second;
1235}
1236
1237bool MachineSinking::registerPressureSetExceedsLimit(
1238 unsigned NRegs, const TargetRegisterClass *RC,
1239 const MachineBasicBlock &MBB) {
1240 unsigned Weight = NRegs * TRI->getRegClassWeight(RC).RegWeight;
1241 const int *PS = TRI->getRegClassPressureSets(RC);
1242 std::vector<unsigned> BBRegisterPressure = getBBRegisterPressure(MBB);
1243 for (; *PS != -1; PS++)
1244 if (Weight + BBRegisterPressure[*PS] >=
1245 RegClassInfo.getRegPressureSetLimit(*PS))
1246 return true;
1247 return false;
1248}
1249
1250// Recalculate RP and check if any pressure set exceeds the set limit.
1251bool MachineSinking::registerPressureExceedsLimit(
1252 const MachineBasicBlock &MBB) {
1253 std::vector<unsigned> BBRegisterPressure = getBBRegisterPressure(MBB, false);
1254
1255 for (unsigned PS = 0; PS < BBRegisterPressure.size(); ++PS) {
1256 if (BBRegisterPressure[PS] >=
1257 TRI->getRegPressureSetLimit(*MBB.getParent(), PS)) {
1258 return true;
1259 }
1260 }
1261
1262 return false;
1263}
1264
1265/// isProfitableToSinkTo - Return true if it is profitable to sink MI.
1266bool MachineSinking::isProfitableToSinkTo(Register Reg, MachineInstr &MI,
1267 MachineBasicBlock *MBB,
1268 MachineBasicBlock *SuccToSinkTo,
1269 AllSuccsCache &AllSuccessors) {
1270 assert(SuccToSinkTo && "Invalid SinkTo Candidate BB");
1271
1272 if (MBB == SuccToSinkTo)
1273 return false;
1274
1275 // It is profitable if SuccToSinkTo does not post dominate current block.
1276 if (!PDT->dominates(SuccToSinkTo, MBB))
1277 return true;
1278
1279 // It is profitable to sink an instruction from a deeper cycle to a shallower
1280 // cycle, even if the latter post-dominates the former (PR21115).
1281 if (CI->getCycleDepth(MBB) > CI->getCycleDepth(SuccToSinkTo))
1282 return true;
1283
1284 // Check if only use in post dominated block is PHI instruction.
1285 bool NonPHIUse = false;
1286 for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
1287 MachineBasicBlock *UseBlock = UseInst.getParent();
1288 if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
1289 NonPHIUse = true;
1290 }
1291 if (!NonPHIUse)
1292 return true;
1293
1294 // If SuccToSinkTo post dominates then also it may be profitable if MI
1295 // can further profitably sinked into another block in next round.
1296 bool BreakPHIEdge = false;
1297 // FIXME - If finding successor is compile time expensive then cache results.
1298 if (MachineBasicBlock *MBB2 =
1299 FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
1300 return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
1301
1302 MachineCycle *MCycle = CI->getCycle(MBB);
1303
1304 // If the instruction is not inside a cycle, it is not profitable to sink MI
1305 // to a post dominate block SuccToSinkTo.
1306 if (!MCycle)
1307 return false;
1308
1309 // If this instruction is inside a Cycle and sinking this instruction can make
1310 // more registers live range shorten, it is still prifitable.
1311 for (const MachineOperand &MO : MI.operands()) {
1312 // Ignore non-register operands.
1313 if (!MO.isReg())
1314 continue;
1315 Register Reg = MO.getReg();
1316 if (Reg == 0)
1317 continue;
1318
1319 if (Reg.isPhysical()) {
1320 // Don't handle non-constant and non-ignorable physical register uses.
1321 if (MO.isUse() && !MRI->isConstantPhysReg(Reg) &&
1322 !TII->isIgnorableUse(MO))
1323 return false;
1324 continue;
1325 }
1326
1327 // Users for the defs are all dominated by SuccToSinkTo.
1328 if (MO.isDef()) {
1329 // This def register's live range is shortened after sinking.
1330 bool LocalUse = false;
1331 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
1332 LocalUse))
1333 return false;
1334 } else {
1335 MachineInstr *DefMI = MRI->getVRegDef(Reg);
1336 if (!DefMI)
1337 continue;
1339 // DefMI is defined outside of cycle. There should be no live range
1340 // impact for this operand. Defination outside of cycle means:
1341 // 1: defination is outside of cycle.
1342 // 2: defination is in this cycle, but it is a PHI in the cycle header.
1343 if (Cycle != MCycle || (DefMI->isPHI() && Cycle && Cycle->isReducible() &&
1344 Cycle->getHeader() == DefMI->getParent()))
1345 continue;
1346 // The DefMI is defined inside the cycle.
1347 // If sinking this operand makes some register pressure set exceed limit,
1348 // it is not profitable.
1349 if (registerPressureSetExceedsLimit(1, MRI->getRegClass(Reg),
1350 *SuccToSinkTo)) {
1351 LLVM_DEBUG(dbgs() << "register pressure exceed limit, not profitable.");
1352 return false;
1353 }
1354 }
1355 }
1356
1357 // If MI is in cycle and all its operands are alive across the whole cycle or
1358 // if no operand sinking make register pressure set exceed limit, it is
1359 // profitable to sink MI.
1360 return true;
1361}
1362
1363/// Get the sorted sequence of successors for this MachineBasicBlock, possibly
1364/// computing it if it was not already cached.
1365SmallVector<MachineBasicBlock *, 4> &
1366MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
1367 AllSuccsCache &AllSuccessors) const {
1368 // Do we have the sorted successors in cache ?
1369 auto Succs = AllSuccessors.find(MBB);
1370 if (Succs != AllSuccessors.end())
1371 return Succs->second;
1372
1373 SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->successors());
1374
1375 // Handle cases where sinking can happen but where the sink point isn't a
1376 // successor. For example:
1377 //
1378 // x = computation
1379 // if () {} else {}
1380 // use x
1381 //
1382 for (MachineDomTreeNode *DTChild : DT->getNode(MBB)->children()) {
1383 // DomTree children of MBB that have MBB as immediate dominator are added.
1384 if (DTChild->getIDom()->getBlock() == MI.getParent() &&
1385 // Skip MBBs already added to the AllSuccs vector above.
1386 !MBB->isSuccessor(DTChild->getBlock()))
1387 AllSuccs.push_back(DTChild->getBlock());
1388 }
1389
1390 // Sort Successors according to their cycle depth or block frequency info.
1392 AllSuccs, [&](const MachineBasicBlock *L, const MachineBasicBlock *R) {
1393 uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
1394 uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
1395 if (llvm::shouldOptimizeForSize(MBB, PSI, MBFI) ||
1396 (!LHSFreq && !RHSFreq))
1397 return CI->getCycleDepth(L) < CI->getCycleDepth(R);
1398 return LHSFreq < RHSFreq;
1399 });
1400
1401 auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
1402
1403 return it.first->second;
1404}
1405
1406/// FindSuccToSinkTo - Find a successor to sink this instruction to.
1407MachineBasicBlock *
1408MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
1409 bool &BreakPHIEdge,
1410 AllSuccsCache &AllSuccessors) {
1411 assert(MBB && "Invalid MachineBasicBlock!");
1412
1413 // loop over all the operands of the specified instruction. If there is
1414 // anything we can't handle, bail out.
1415
1416 // SuccToSinkTo - This is the successor to sink this instruction to, once we
1417 // decide.
1418 MachineBasicBlock *SuccToSinkTo = nullptr;
1419 for (const MachineOperand &MO : MI.operands()) {
1420 if (!MO.isReg())
1421 continue; // Ignore non-register operands.
1422
1423 Register Reg = MO.getReg();
1424 if (Reg == 0)
1425 continue;
1426
1427 if (Reg.isPhysical()) {
1428 if (MO.isUse()) {
1429 // If the physreg has no defs anywhere, it's just an ambient register
1430 // and we can freely move its uses. Alternatively, if it's allocatable,
1431 // it could get allocated to something with a def during allocation.
1432 if (!MRI->isConstantPhysReg(Reg) && !TII->isIgnorableUse(MO))
1433 return nullptr;
1434 } else if (!MO.isDead()) {
1435 // A def that isn't dead. We can't move it.
1436 return nullptr;
1437 }
1438 } else {
1439 // Virtual register uses are always safe to sink.
1440 if (MO.isUse())
1441 continue;
1442
1443 // If it's not safe to move defs of the register class, then abort.
1444 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
1445 return nullptr;
1446
1447 // Virtual register defs can only be sunk if all their uses are in blocks
1448 // dominated by one of the successors.
1449 if (SuccToSinkTo) {
1450 // If a previous operand picked a block to sink to, then this operand
1451 // must be sinkable to the same block.
1452 bool LocalUse = false;
1453 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
1454 LocalUse))
1455 return nullptr;
1456
1457 continue;
1458 }
1459
1460 // Otherwise, we should look at all the successors and decide which one
1461 // we should sink to. If we have reliable block frequency information
1462 // (frequency != 0) available, give successors with smaller frequencies
1463 // higher priority, otherwise prioritize smaller cycle depths.
1464 for (MachineBasicBlock *SuccBlock :
1465 GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
1466 bool LocalUse = false;
1467 if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB, BreakPHIEdge,
1468 LocalUse)) {
1469 SuccToSinkTo = SuccBlock;
1470 break;
1471 }
1472 if (LocalUse)
1473 // Def is used locally, it's never safe to move this def.
1474 return nullptr;
1475 }
1476
1477 // If we couldn't find a block to sink to, ignore this instruction.
1478 if (!SuccToSinkTo)
1479 return nullptr;
1480 if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
1481 return nullptr;
1482 }
1483 }
1484
1485 // It is not possible to sink an instruction into its own block. This can
1486 // happen with cycles.
1487 if (MBB == SuccToSinkTo)
1488 return nullptr;
1489
1490 // It's not safe to sink instructions to EH landing pad. Control flow into
1491 // landing pad is implicitly defined.
1492 if (SuccToSinkTo && SuccToSinkTo->isEHPad())
1493 return nullptr;
1494
1495 // It ought to be okay to sink instructions into an INLINEASM_BR target, but
1496 // only if we make sure that MI occurs _before_ an INLINEASM_BR instruction in
1497 // the source block (which this code does not yet do). So for now, forbid
1498 // doing so.
1499 if (SuccToSinkTo && SuccToSinkTo->isInlineAsmBrIndirectTarget())
1500 return nullptr;
1501
1502 if (SuccToSinkTo && !TII->isSafeToSink(MI, SuccToSinkTo, CI))
1503 return nullptr;
1504
1505 return SuccToSinkTo;
1506}
1507
1508/// Return true if MI is likely to be usable as a memory operation by the
1509/// implicit null check optimization.
1510///
1511/// This is a "best effort" heuristic, and should not be relied upon for
1512/// correctness. This returning true does not guarantee that the implicit null
1513/// check optimization is legal over MI, and this returning false does not
1514/// guarantee MI cannot possibly be used to do a null check.
1516 const TargetInstrInfo *TII,
1517 const TargetRegisterInfo *TRI) {
1518 using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate;
1519
1520 auto *MBB = MI.getParent();
1521 if (MBB->pred_size() != 1)
1522 return false;
1523
1524 auto *PredMBB = *MBB->pred_begin();
1525 auto *PredBB = PredMBB->getBasicBlock();
1526
1527 // Frontends that don't use implicit null checks have no reason to emit
1528 // branches with make.implicit metadata, and this function should always
1529 // return false for them.
1530 if (!PredBB ||
1531 !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
1532 return false;
1533
1534 const MachineOperand *BaseOp;
1535 int64_t Offset;
1536 bool OffsetIsScalable;
1537 if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI))
1538 return false;
1539
1540 if (!BaseOp->isReg())
1541 return false;
1542
1543 if (!(MI.mayLoad() && !MI.isPredicable()))
1544 return false;
1545
1546 MachineBranchPredicate MBP;
1547 if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
1548 return false;
1549
1550 return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
1551 (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
1552 MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
1553 MBP.LHS.getReg() == BaseOp->getReg();
1554}
1555
1556/// If the sunk instruction is a copy, try to forward the copy instead of
1557/// leaving an 'undef' DBG_VALUE in the original location. Don't do this if
1558/// there's any subregister weirdness involved. Returns true if copy
1559/// propagation occurred.
1560static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI,
1561 Register Reg) {
1562 const MachineRegisterInfo &MRI = SinkInst.getMF()->getRegInfo();
1563 const TargetInstrInfo &TII = *SinkInst.getMF()->getSubtarget().getInstrInfo();
1564
1565 // Copy DBG_VALUE operand and set the original to undef. We then check to
1566 // see whether this is something that can be copy-forwarded. If it isn't,
1567 // continue around the loop.
1568
1569 const MachineOperand *SrcMO = nullptr, *DstMO = nullptr;
1570 auto CopyOperands = TII.isCopyInstr(SinkInst);
1571 if (!CopyOperands)
1572 return false;
1573 SrcMO = CopyOperands->Source;
1574 DstMO = CopyOperands->Destination;
1575
1576 // Check validity of forwarding this copy.
1577 bool PostRA = MRI.getNumVirtRegs() == 0;
1578
1579 // Trying to forward between physical and virtual registers is too hard.
1580 if (Reg.isVirtual() != SrcMO->getReg().isVirtual())
1581 return false;
1582
1583 // Only try virtual register copy-forwarding before regalloc, and physical
1584 // register copy-forwarding after regalloc.
1585 bool arePhysRegs = !Reg.isVirtual();
1586 if (arePhysRegs != PostRA)
1587 return false;
1588
1589 // Pre-regalloc, only forward if all subregisters agree (or there are no
1590 // subregs at all). More analysis might recover some forwardable copies.
1591 if (!PostRA)
1592 for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg))
1593 if (DbgMO.getSubReg() != SrcMO->getSubReg() ||
1594 DbgMO.getSubReg() != DstMO->getSubReg())
1595 return false;
1596
1597 // Post-regalloc, we may be sinking a DBG_VALUE of a sub or super-register
1598 // of this copy. Only forward the copy if the DBG_VALUE operand exactly
1599 // matches the copy destination.
1600 if (PostRA && Reg != DstMO->getReg())
1601 return false;
1602
1603 for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg)) {
1604 DbgMO.setReg(SrcMO->getReg());
1605 DbgMO.setSubReg(SrcMO->getSubReg());
1606 }
1607 return true;
1608}
1609
1610using MIRegs = std::pair<MachineInstr *, SmallVector<Register, 2>>;
1611/// Sink an instruction and its associated debug instructions.
1612static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
1614 ArrayRef<MIRegs> DbgValuesToSink) {
1615 // If we cannot find a location to use (merge with), then we erase the debug
1616 // location to prevent debug-info driven tools from potentially reporting
1617 // wrong location information.
1618 if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
1619 MI.setDebugLoc(DebugLoc::getMergedLocation(MI.getDebugLoc(),
1620 InsertPos->getDebugLoc()));
1621 else
1622 MI.setDebugLoc(DebugLoc());
1623
1624 // Move the instruction.
1625 MachineBasicBlock *ParentBlock = MI.getParent();
1626 SuccToSinkTo.splice(InsertPos, ParentBlock, MI,
1628
1629 // Sink a copy of debug users to the insert position. Mark the original
1630 // DBG_VALUE location as 'undef', indicating that any earlier variable
1631 // location should be terminated as we've optimised away the value at this
1632 // point.
1633 for (const auto &DbgValueToSink : DbgValuesToSink) {
1634 MachineInstr *DbgMI = DbgValueToSink.first;
1635 MachineInstr *NewDbgMI = DbgMI->getMF()->CloneMachineInstr(DbgMI);
1636 SuccToSinkTo.insert(InsertPos, NewDbgMI);
1637
1638 bool PropagatedAllSunkOps = true;
1639 for (Register Reg : DbgValueToSink.second) {
1640 if (DbgMI->hasDebugOperandForReg(Reg)) {
1641 if (!attemptDebugCopyProp(MI, *DbgMI, Reg)) {
1642 PropagatedAllSunkOps = false;
1643 break;
1644 }
1645 }
1646 }
1647 if (!PropagatedAllSunkOps)
1648 DbgMI->setDebugValueUndef();
1649 }
1650}
1651
1652/// hasStoreBetween - check if there is store betweeen straight line blocks From
1653/// and To.
1654bool MachineSinking::hasStoreBetween(MachineBasicBlock *From,
1655 MachineBasicBlock *To, MachineInstr &MI) {
1656 // Make sure From and To are in straight line which means From dominates To
1657 // and To post dominates From.
1658 if (!DT->dominates(From, To) || !PDT->dominates(To, From))
1659 return true;
1660
1661 auto BlockPair = std::make_pair(From, To);
1662
1663 // Does these two blocks pair be queried before and have a definite cached
1664 // result?
1665 if (auto It = HasStoreCache.find(BlockPair); It != HasStoreCache.end())
1666 return It->second;
1667
1668 if (auto It = StoreInstrCache.find(BlockPair); It != StoreInstrCache.end())
1669 return llvm::any_of(It->second, [&](MachineInstr *I) {
1670 return I->mayAlias(AA, MI, false);
1671 });
1672
1673 bool SawStore = false;
1674 bool HasAliasedStore = false;
1675 DenseSet<MachineBasicBlock *> HandledBlocks;
1676 DenseSet<MachineBasicBlock *> HandledDomBlocks;
1677 // Go through all reachable blocks from From.
1678 for (MachineBasicBlock *BB : depth_first(From)) {
1679 // We insert the instruction at the start of block To, so no need to worry
1680 // about stores inside To.
1681 // Store in block From should be already considered when just enter function
1682 // SinkInstruction.
1683 if (BB == To || BB == From)
1684 continue;
1685
1686 // We already handle this BB in previous iteration.
1687 if (HandledBlocks.count(BB))
1688 continue;
1689
1690 HandledBlocks.insert(BB);
1691 // To post dominates BB, it must be a path from block From.
1692 if (PDT->dominates(To, BB)) {
1693 if (!HandledDomBlocks.count(BB))
1694 HandledDomBlocks.insert(BB);
1695
1696 // If this BB is too big or the block number in straight line between From
1697 // and To is too big, stop searching to save compiling time.
1698 if (BB->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold) ||
1699 HandledDomBlocks.size() > SinkLoadBlocksThreshold) {
1700 for (auto *DomBB : HandledDomBlocks) {
1701 if (DomBB != BB && DT->dominates(DomBB, BB))
1702 HasStoreCache[std::make_pair(DomBB, To)] = true;
1703 else if (DomBB != BB && DT->dominates(BB, DomBB))
1704 HasStoreCache[std::make_pair(From, DomBB)] = true;
1705 }
1706 HasStoreCache[BlockPair] = true;
1707 return true;
1708 }
1709
1710 for (MachineInstr &I : *BB) {
1711 // Treat as alias conservatively for a call or an ordered memory
1712 // operation.
1713 if (I.isCall() || I.hasOrderedMemoryRef()) {
1714 for (auto *DomBB : HandledDomBlocks) {
1715 if (DomBB != BB && DT->dominates(DomBB, BB))
1716 HasStoreCache[std::make_pair(DomBB, To)] = true;
1717 else if (DomBB != BB && DT->dominates(BB, DomBB))
1718 HasStoreCache[std::make_pair(From, DomBB)] = true;
1719 }
1720 HasStoreCache[BlockPair] = true;
1721 return true;
1722 }
1723
1724 if (I.mayStore()) {
1725 SawStore = true;
1726 // We still have chance to sink MI if all stores between are not
1727 // aliased to MI.
1728 // Cache all store instructions, so that we don't need to go through
1729 // all From reachable blocks for next load instruction.
1730 if (I.mayAlias(AA, MI, false))
1731 HasAliasedStore = true;
1732 StoreInstrCache[BlockPair].push_back(&I);
1733 }
1734 }
1735 }
1736 }
1737 // If there is no store at all, cache the result.
1738 if (!SawStore)
1739 HasStoreCache[BlockPair] = false;
1740 return HasAliasedStore;
1741}
1742
1743/// Aggressively sink instructions into cycles. This will aggressively try to
1744/// sink all instructions in the top-most preheaders in an attempt to reduce RP.
1745/// In particular, it will sink into multiple successor blocks without limits
1746/// based on the amount of sinking, or the type of ops being sunk (so long as
1747/// they are safe to sink).
1748bool MachineSinking::aggressivelySinkIntoCycle(
1749 MachineCycle *Cycle, MachineInstr &I,
1750 DenseMap<SinkItem, MachineInstr *> &SunkInstrs) {
1751 // TODO: support instructions with multiple defs
1752 if (I.getNumDefs() > 1)
1753 return false;
1754
1755 LLVM_DEBUG(dbgs() << "AggressiveCycleSink: Finding sink block for: " << I);
1756 assert(Cycle->getCyclePreheader() && "Cycle sink needs a preheader block");
1758
1759 MachineOperand &DefMO = I.getOperand(0);
1760 for (MachineInstr &MI : MRI->use_instructions(DefMO.getReg())) {
1761 Uses.push_back({{DefMO.getReg(), DefMO.getSubReg()}, &MI});
1762 }
1763
1764 for (std::pair<RegSubRegPair, MachineInstr *> Entry : Uses) {
1765 MachineInstr *MI = Entry.second;
1766 LLVM_DEBUG(dbgs() << "AggressiveCycleSink: Analysing use: " << MI);
1767 if (MI->isPHI()) {
1768 LLVM_DEBUG(
1769 dbgs() << "AggressiveCycleSink: Not attempting to sink for PHI.\n");
1770 continue;
1771 }
1772 // We cannot sink before the prologue
1773 if (MI->isPosition() || TII->isBasicBlockPrologue(*MI)) {
1774 LLVM_DEBUG(dbgs() << "AggressiveCycleSink: Use is BasicBlock prologue, "
1775 "can't sink.\n");
1776 continue;
1777 }
1778 if (!Cycle->contains(MI->getParent())) {
1779 LLVM_DEBUG(
1780 dbgs() << "AggressiveCycleSink: Use not in cycle, can't sink.\n");
1781 continue;
1782 }
1783
1784 MachineBasicBlock *SinkBlock = MI->getParent();
1785 MachineInstr *NewMI = nullptr;
1786 SinkItem MapEntry(&I, SinkBlock);
1787
1788 auto SI = SunkInstrs.find(MapEntry);
1789
1790 // Check for the case in which we have already sunk a copy of this
1791 // instruction into the user block.
1792 if (SI != SunkInstrs.end()) {
1793 LLVM_DEBUG(dbgs() << "AggressiveCycleSink: Already sunk to block: "
1794 << printMBBReference(*SinkBlock) << "\n");
1795 NewMI = SI->second;
1796 }
1797
1798 // Create a copy of the instruction in the use block.
1799 if (!NewMI) {
1800 LLVM_DEBUG(dbgs() << "AggressiveCycleSink: Sinking instruction to block: "
1801 << printMBBReference(*SinkBlock) << "\n");
1802
1803 NewMI = I.getMF()->CloneMachineInstr(&I);
1804 if (DefMO.getReg().isVirtual()) {
1805 const TargetRegisterClass *TRC = MRI->getRegClass(DefMO.getReg());
1806 Register DestReg = MRI->createVirtualRegister(TRC);
1807 NewMI->substituteRegister(DefMO.getReg(), DestReg, DefMO.getSubReg(),
1808 *TRI);
1809 }
1810 SinkBlock->insert(SinkBlock->SkipPHIsAndLabels(SinkBlock->begin()),
1811 NewMI);
1812 SunkInstrs.insert({MapEntry, NewMI});
1813 }
1814
1815 // Conservatively clear any kill flags on uses of sunk instruction
1816 for (MachineOperand &MO : NewMI->all_uses()) {
1817 assert(MO.isReg() && MO.isUse());
1818 RegsToClearKillFlags.insert(MO.getReg());
1819 }
1820
1821 // The instruction is moved from its basic block, so do not retain the
1822 // debug information.
1823 assert(!NewMI->isDebugInstr() && "Should not sink debug inst");
1824 NewMI->setDebugLoc(DebugLoc());
1825
1826 // Replace the use with the newly created virtual register.
1827 RegSubRegPair &UseReg = Entry.first;
1828 MI->substituteRegister(UseReg.Reg, NewMI->getOperand(0).getReg(),
1829 UseReg.SubReg, *TRI);
1830 }
1831 // If we have replaced all uses, then delete the dead instruction
1832 if (I.isDead(*MRI))
1833 I.eraseFromParent();
1834 return true;
1835}
1836
1837/// SinkInstruction - Determine whether it is safe to sink the specified machine
1838/// instruction out of its current block into a successor.
1839bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
1840 AllSuccsCache &AllSuccessors) {
1841 // Don't sink instructions that the target prefers not to sink.
1842 if (!TII->shouldSink(MI))
1843 return false;
1844
1845 // Check if it's safe to move the instruction.
1846 if (!MI.isSafeToMove(SawStore))
1847 return false;
1848
1849 // Convergent operations may not be made control-dependent on additional
1850 // values.
1851 if (MI.isConvergent())
1852 return false;
1853
1854 // Don't break implicit null checks. This is a performance heuristic, and not
1855 // required for correctness.
1857 return false;
1858
1859 // FIXME: This should include support for sinking instructions within the
1860 // block they are currently in to shorten the live ranges. We often get
1861 // instructions sunk into the top of a large block, but it would be better to
1862 // also sink them down before their first use in the block. This xform has to
1863 // be careful not to *increase* register pressure though, e.g. sinking
1864 // "x = y + z" down if it kills y and z would increase the live ranges of y
1865 // and z and only shrink the live range of x.
1866
1867 bool BreakPHIEdge = false;
1868 MachineBasicBlock *ParentBlock = MI.getParent();
1869 MachineBasicBlock *SuccToSinkTo =
1870 FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
1871
1872 // If there are no outputs, it must have side-effects.
1873 if (!SuccToSinkTo)
1874 return false;
1875
1876 // If the instruction to move defines a dead physical register which is live
1877 // when leaving the basic block, don't move it because it could turn into a
1878 // "zombie" define of that preg. E.g., EFLAGS.
1879 for (const MachineOperand &MO : MI.all_defs()) {
1880 Register Reg = MO.getReg();
1881 if (Reg == 0 || !Reg.isPhysical())
1882 continue;
1883 if (SuccToSinkTo->isLiveIn(Reg))
1884 return false;
1885 }
1886
1887 LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
1888
1889 // If the block has multiple predecessors, this is a critical edge.
1890 // Decide if we can sink along it or need to break the edge.
1891 if (SuccToSinkTo->pred_size() > 1) {
1892 // We cannot sink a load across a critical edge - there may be stores in
1893 // other code paths.
1894 bool TryBreak = false;
1895 bool Store =
1896 MI.mayLoad() ? hasStoreBetween(ParentBlock, SuccToSinkTo, MI) : true;
1897 if (!MI.isSafeToMove(Store)) {
1898 LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
1899 TryBreak = true;
1900 }
1901
1902 // We don't want to sink across a critical edge if we don't dominate the
1903 // successor. We could be introducing calculations to new code paths.
1904 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
1905 LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
1906 TryBreak = true;
1907 }
1908
1909 // Don't sink instructions into a cycle.
1910 if (!TryBreak && CI->getCycle(SuccToSinkTo) &&
1911 (!CI->getCycle(SuccToSinkTo)->isReducible() ||
1912 CI->getCycle(SuccToSinkTo)->getHeader() == SuccToSinkTo)) {
1913 LLVM_DEBUG(dbgs() << " *** NOTE: cycle header found\n");
1914 TryBreak = true;
1915 }
1916
1917 // Otherwise we are OK with sinking along a critical edge.
1918 if (!TryBreak)
1919 LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n");
1920 else {
1921 // Mark this edge as to be split.
1922 // If the edge can actually be split, the next iteration of the main loop
1923 // will sink MI in the newly created block.
1924 bool Status = PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo,
1925 BreakPHIEdge);
1926 if (!Status)
1927 LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
1928 "break critical edge\n");
1929 // The instruction will not be sunk this time.
1930 return false;
1931 }
1932 }
1933
1934 if (BreakPHIEdge) {
1935 // BreakPHIEdge is true if all the uses are in the successor MBB being
1936 // sunken into and they are all PHI nodes. In this case, machine-sink must
1937 // break the critical edge first.
1938 bool Status =
1939 PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
1940 if (!Status)
1941 LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
1942 "break critical edge\n");
1943 // The instruction will not be sunk this time.
1944 return false;
1945 }
1946
1947 // Determine where to insert into. Skip phi nodes.
1948 MachineBasicBlock::iterator InsertPos =
1949 SuccToSinkTo->SkipPHIsAndLabels(SuccToSinkTo->begin());
1950 if (blockPrologueInterferes(SuccToSinkTo, InsertPos, MI, TRI, TII, MRI)) {
1951 LLVM_DEBUG(dbgs() << " *** Not sinking: prologue interference\n");
1952 return false;
1953 }
1954
1955 // Collect debug users of any vreg that this inst defines.
1956 SmallVector<MIRegs, 4> DbgUsersToSink;
1957 for (auto &MO : MI.all_defs()) {
1958 if (!MO.getReg().isVirtual())
1959 continue;
1960 auto It = SeenDbgUsers.find(MO.getReg());
1961 if (It == SeenDbgUsers.end())
1962 continue;
1963
1964 // Sink any users that don't pass any other DBG_VALUEs for this variable.
1965 auto &Users = It->second;
1966 for (auto &User : Users) {
1967 MachineInstr *DbgMI = User.getPointer();
1968 if (User.getInt()) {
1969 // This DBG_VALUE would re-order assignments. If we can't copy-propagate
1970 // it, it can't be recovered. Set it undef.
1971 if (!attemptDebugCopyProp(MI, *DbgMI, MO.getReg()))
1972 DbgMI->setDebugValueUndef();
1973 } else {
1974 DbgUsersToSink.push_back(
1975 {DbgMI, SmallVector<Register, 2>(1, MO.getReg())});
1976 }
1977 }
1978 }
1979
1980 // After sinking, some debug users may not be dominated any more. If possible,
1981 // copy-propagate their operands. As it's expensive, don't do this if there's
1982 // no debuginfo in the program.
1983 if (MI.getMF()->getFunction().getSubprogram() && MI.isCopy())
1984 SalvageUnsunkDebugUsersOfCopy(MI, SuccToSinkTo);
1985
1986 performSink(MI, *SuccToSinkTo, InsertPos, DbgUsersToSink);
1987
1988 // Conservatively, clear any kill flags, since it's possible that they are no
1989 // longer correct.
1990 // Note that we have to clear the kill flags for any register this instruction
1991 // uses as we may sink over another instruction which currently kills the
1992 // used registers.
1993 for (MachineOperand &MO : MI.all_uses())
1994 RegsToClearKillFlags.insert(MO.getReg()); // Remember to clear kill flags.
1995
1996 return true;
1997}
1998
1999void MachineSinking::SalvageUnsunkDebugUsersOfCopy(
2000 MachineInstr &MI, MachineBasicBlock *TargetBlock) {
2001 assert(MI.isCopy());
2002 assert(MI.getOperand(1).isReg());
2003
2004 // Enumerate all users of vreg operands that are def'd. Skip those that will
2005 // be sunk. For the rest, if they are not dominated by the block we will sink
2006 // MI into, propagate the copy source to them.
2007 SmallVector<MachineInstr *, 4> DbgDefUsers;
2008 SmallVector<Register, 4> DbgUseRegs;
2009 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
2010 for (auto &MO : MI.all_defs()) {
2011 if (!MO.getReg().isVirtual())
2012 continue;
2013 DbgUseRegs.push_back(MO.getReg());
2014 for (auto &User : MRI.use_instructions(MO.getReg())) {
2015 if (!User.isDebugValue() || DT->dominates(TargetBlock, User.getParent()))
2016 continue;
2017
2018 // If is in same block, will either sink or be use-before-def.
2019 if (User.getParent() == MI.getParent())
2020 continue;
2021
2022 assert(User.hasDebugOperandForReg(MO.getReg()) &&
2023 "DBG_VALUE user of vreg, but has no operand for it?");
2024 DbgDefUsers.push_back(&User);
2025 }
2026 }
2027
2028 // Point the users of this copy that are no longer dominated, at the source
2029 // of the copy.
2030 for (auto *User : DbgDefUsers) {
2031 for (auto &Reg : DbgUseRegs) {
2032 for (auto &DbgOp : User->getDebugOperandsForReg(Reg)) {
2033 DbgOp.setReg(MI.getOperand(1).getReg());
2034 DbgOp.setSubReg(MI.getOperand(1).getSubReg());
2035 }
2036 }
2037 }
2038}
2039
2040//===----------------------------------------------------------------------===//
2041// This pass is not intended to be a replacement or a complete alternative
2042// for the pre-ra machine sink pass. It is only designed to sink COPY
2043// instructions which should be handled after RA.
2044//
2045// This pass sinks COPY instructions into a successor block, if the COPY is not
2046// used in the current block and the COPY is live-in to a single successor
2047// (i.e., doesn't require the COPY to be duplicated). This avoids executing the
2048// copy on paths where their results aren't needed. This also exposes
2049// additional opportunites for dead copy elimination and shrink wrapping.
2050//
2051// These copies were either not handled by or are inserted after the MachineSink
2052// pass. As an example of the former case, the MachineSink pass cannot sink
2053// COPY instructions with allocatable source registers; for AArch64 these type
2054// of copy instructions are frequently used to move function parameters (PhyReg)
2055// into virtual registers in the entry block.
2056//
2057// For the machine IR below, this pass will sink %w19 in the entry into its
2058// successor (%bb.1) because %w19 is only live-in in %bb.1.
2059// %bb.0:
2060// %wzr = SUBSWri %w1, 1
2061// %w19 = COPY %w0
2062// Bcc 11, %bb.2
2063// %bb.1:
2064// Live Ins: %w19
2065// BL @fun
2066// %w0 = ADDWrr %w0, %w19
2067// RET %w0
2068// %bb.2:
2069// %w0 = COPY %wzr
2070// RET %w0
2071// As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be
2072// able to see %bb.0 as a candidate.
2073//===----------------------------------------------------------------------===//
2074namespace {
2075
2076class PostRAMachineSinkingImpl {
2077 /// Track which register units have been modified and used.
2078 LiveRegUnits ModifiedRegUnits, UsedRegUnits;
2079
2080 /// Track DBG_VALUEs of (unmodified) register units. Each DBG_VALUE has an
2081 /// entry in this map for each unit it touches. The DBG_VALUE's entry
2082 /// consists of a pointer to the instruction itself, and a vector of registers
2083 /// referred to by the instruction that overlap the key register unit.
2084 DenseMap<MCRegUnit, SmallVector<MIRegs, 2>> SeenDbgInstrs;
2085
2086 /// Sink Copy instructions unused in the same block close to their uses in
2087 /// successors.
2088 bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF,
2089 const TargetRegisterInfo *TRI, const TargetInstrInfo *TII);
2090
2091public:
2092 bool run(MachineFunction &MF);
2093};
2094
2095class PostRAMachineSinkingLegacy : public MachineFunctionPass {
2096public:
2097 bool runOnMachineFunction(MachineFunction &MF) override;
2098
2099 static char ID;
2100 PostRAMachineSinkingLegacy() : MachineFunctionPass(ID) {}
2101 StringRef getPassName() const override { return "PostRA Machine Sink"; }
2102
2103 void getAnalysisUsage(AnalysisUsage &AU) const override {
2104 AU.setPreservesCFG();
2106 }
2107
2108 MachineFunctionProperties getRequiredProperties() const override {
2109 return MachineFunctionProperties().setNoVRegs();
2110 }
2111};
2112
2113} // namespace
2114
2115char PostRAMachineSinkingLegacy::ID = 0;
2116char &llvm::PostRAMachineSinkingID = PostRAMachineSinkingLegacy::ID;
2117
2118INITIALIZE_PASS(PostRAMachineSinkingLegacy, "postra-machine-sink",
2119 "PostRA Machine Sink", false, false)
2120
2121static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, Register Reg,
2123 LiveRegUnits LiveInRegUnits(*TRI);
2124 LiveInRegUnits.addLiveIns(MBB);
2125 return !LiveInRegUnits.available(Reg);
2126}
2127
2128static MachineBasicBlock *
2130 const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
2132 // Try to find a single sinkable successor in which Reg is live-in.
2133 MachineBasicBlock *BB = nullptr;
2134 for (auto *SI : SinkableBBs) {
2135 if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) {
2136 // If BB is set here, Reg is live-in to at least two sinkable successors,
2137 // so quit.
2138 if (BB)
2139 return nullptr;
2140 BB = SI;
2141 }
2142 }
2143 // Reg is not live-in to any sinkable successors.
2144 if (!BB)
2145 return nullptr;
2146
2147 // Check if any register aliased with Reg is live-in in other successors.
2148 for (auto *SI : CurBB.successors()) {
2149 if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI))
2150 return nullptr;
2151 }
2152 return BB;
2153}
2154
2155static MachineBasicBlock *
2157 const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
2158 ArrayRef<Register> DefedRegsInCopy,
2159 const TargetRegisterInfo *TRI) {
2160 MachineBasicBlock *SingleBB = nullptr;
2161 for (auto DefReg : DefedRegsInCopy) {
2162 MachineBasicBlock *BB =
2163 getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI);
2164 if (!BB || (SingleBB && SingleBB != BB))
2165 return nullptr;
2166 SingleBB = BB;
2167 }
2168 return SingleBB;
2169}
2170
2172 const SmallVectorImpl<unsigned> &UsedOpsInCopy,
2173 const LiveRegUnits &UsedRegUnits,
2174 const TargetRegisterInfo *TRI) {
2175 for (auto U : UsedOpsInCopy) {
2176 MachineOperand &MO = MI->getOperand(U);
2177 Register SrcReg = MO.getReg();
2178 if (!UsedRegUnits.available(SrcReg)) {
2179 MachineBasicBlock::iterator NI = std::next(MI->getIterator());
2180 for (MachineInstr &UI : make_range(NI, CurBB.end())) {
2181 if (UI.killsRegister(SrcReg, TRI)) {
2182 UI.clearRegisterKills(SrcReg, TRI);
2183 MO.setIsKill(true);
2184 break;
2185 }
2186 }
2187 }
2188 }
2189}
2190
2192 const SmallVectorImpl<unsigned> &UsedOpsInCopy,
2193 const SmallVectorImpl<Register> &DefedRegsInCopy) {
2194 for (Register DefReg : DefedRegsInCopy)
2195 SuccBB->removeLiveInOverlappedWith(DefReg);
2196
2197 for (auto U : UsedOpsInCopy)
2198 SuccBB->addLiveIn(MI->getOperand(U).getReg());
2199 SuccBB->sortUniqueLiveIns();
2200}
2201
2203 SmallVectorImpl<unsigned> &UsedOpsInCopy,
2204 SmallVectorImpl<Register> &DefedRegsInCopy,
2205 LiveRegUnits &ModifiedRegUnits,
2206 LiveRegUnits &UsedRegUnits) {
2207 bool HasRegDependency = false;
2208 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2209 MachineOperand &MO = MI->getOperand(i);
2210 if (!MO.isReg())
2211 continue;
2212 Register Reg = MO.getReg();
2213 if (!Reg)
2214 continue;
2215 if (MO.isDef()) {
2216 if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
2217 HasRegDependency = true;
2218 break;
2219 }
2220 DefedRegsInCopy.push_back(Reg);
2221
2222 // FIXME: instead of isUse(), readsReg() would be a better fix here,
2223 // For example, we can ignore modifications in reg with undef. However,
2224 // it's not perfectly clear if skipping the internal read is safe in all
2225 // other targets.
2226 } else if (MO.isUse()) {
2227 if (!ModifiedRegUnits.available(Reg)) {
2228 HasRegDependency = true;
2229 break;
2230 }
2231 UsedOpsInCopy.push_back(i);
2232 }
2233 }
2234 return HasRegDependency;
2235}
2236
2237bool PostRAMachineSinkingImpl::tryToSinkCopy(MachineBasicBlock &CurBB,
2238 MachineFunction &MF,
2239 const TargetRegisterInfo *TRI,
2240 const TargetInstrInfo *TII) {
2241 SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs;
2242 // FIXME: For now, we sink only to a successor which has a single predecessor
2243 // so that we can directly sink COPY instructions to the successor without
2244 // adding any new block or branch instruction.
2245 for (MachineBasicBlock *SI : CurBB.successors())
2246 if (!SI->livein_empty() && SI->pred_size() == 1)
2247 SinkableBBs.insert(SI);
2248
2249 if (SinkableBBs.empty())
2250 return false;
2251
2252 bool Changed = false;
2253
2254 // Track which registers have been modified and used between the end of the
2255 // block and the current instruction.
2256 ModifiedRegUnits.clear();
2257 UsedRegUnits.clear();
2258 SeenDbgInstrs.clear();
2259
2260 for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(CurBB))) {
2261 // Track the operand index for use in Copy.
2262 SmallVector<unsigned, 2> UsedOpsInCopy;
2263 // Track the register number defed in Copy.
2264 SmallVector<Register, 2> DefedRegsInCopy;
2265
2266 // We must sink this DBG_VALUE if its operand is sunk. To avoid searching
2267 // for DBG_VALUEs later, record them when they're encountered.
2268 if (MI.isDebugValue() && !MI.isDebugRef()) {
2269 SmallDenseMap<MCRegUnit, SmallVector<Register, 2>, 4> MIUnits;
2270 bool IsValid = true;
2271 for (MachineOperand &MO : MI.debug_operands()) {
2272 if (MO.isReg() && MO.getReg().isPhysical()) {
2273 // Bail if we can already tell the sink would be rejected, rather
2274 // than needlessly accumulating lots of DBG_VALUEs.
2275 if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2276 ModifiedRegUnits, UsedRegUnits)) {
2277 IsValid = false;
2278 break;
2279 }
2280
2281 // Record debug use of each reg unit.
2282 for (MCRegUnit Unit : TRI->regunits(MO.getReg()))
2283 MIUnits[Unit].push_back(MO.getReg());
2284 }
2285 }
2286 if (IsValid) {
2287 for (auto &RegOps : MIUnits)
2288 SeenDbgInstrs[RegOps.first].emplace_back(&MI,
2289 std::move(RegOps.second));
2290 }
2291 continue;
2292 }
2293
2294 // Don't postRASink instructions that the target prefers not to sink.
2295 if (!TII->shouldPostRASink(MI))
2296 continue;
2297
2298 if (MI.isDebugOrPseudoInstr())
2299 continue;
2300
2301 // Do not move any instruction across function call.
2302 if (MI.isCall())
2303 return false;
2304
2305 if (!MI.isCopy() || !MI.getOperand(0).isRenamable()) {
2306 LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2307 TRI);
2308 continue;
2309 }
2310
2311 // Don't sink the COPY if it would violate a register dependency.
2312 if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2313 ModifiedRegUnits, UsedRegUnits)) {
2314 LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2315 TRI);
2316 continue;
2317 }
2318 assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) &&
2319 "Unexpect SrcReg or DefReg");
2320 MachineBasicBlock *SuccBB =
2321 getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI);
2322 // Don't sink if we cannot find a single sinkable successor in which Reg
2323 // is live-in.
2324 if (!SuccBB) {
2325 LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2326 TRI);
2327 continue;
2328 }
2329 assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) &&
2330 "Unexpected predecessor");
2331
2332 // Collect DBG_VALUEs that must sink with this copy. We've previously
2333 // recorded which reg units that DBG_VALUEs read, if this instruction
2334 // writes any of those units then the corresponding DBG_VALUEs must sink.
2335 MapVector<MachineInstr *, MIRegs::second_type> DbgValsToSinkMap;
2336 for (auto &MO : MI.all_defs()) {
2337 for (MCRegUnit Unit : TRI->regunits(MO.getReg())) {
2338 for (const auto &MIRegs : SeenDbgInstrs.lookup(Unit)) {
2339 auto &Regs = DbgValsToSinkMap[MIRegs.first];
2340 llvm::append_range(Regs, MIRegs.second);
2341 }
2342 }
2343 }
2344 auto DbgValsToSink = DbgValsToSinkMap.takeVector();
2345
2346 LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccBB);
2347
2348 MachineBasicBlock::iterator InsertPos =
2349 SuccBB->SkipPHIsAndLabels(SuccBB->begin());
2350 if (blockPrologueInterferes(SuccBB, InsertPos, MI, TRI, TII, nullptr)) {
2351 LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2352 TRI);
2353 LLVM_DEBUG(dbgs() << " *** Not sinking: prologue interference\n");
2354 continue;
2355 }
2356
2357 // Clear the kill flag if SrcReg is killed between MI and the end of the
2358 // block.
2359 clearKillFlags(&MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI);
2360 performSink(MI, *SuccBB, InsertPos, DbgValsToSink);
2361 updateLiveIn(&MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy);
2362
2363 Changed = true;
2364 ++NumPostRACopySink;
2365 }
2366 return Changed;
2367}
2368
2369bool PostRAMachineSinkingImpl::run(MachineFunction &MF) {
2370 bool Changed = false;
2371 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2372 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2373
2374 ModifiedRegUnits.init(*TRI);
2375 UsedRegUnits.init(*TRI);
2376 for (auto &BB : MF)
2377 Changed |= tryToSinkCopy(BB, MF, TRI, TII);
2378
2379 return Changed;
2380}
2381
2382bool PostRAMachineSinkingLegacy::runOnMachineFunction(MachineFunction &MF) {
2383 if (skipFunction(MF.getFunction()))
2384 return false;
2385
2386 return PostRAMachineSinkingImpl().run(MF);
2387}
2388
2389PreservedAnalyses
2392 MFPropsModifier _(*this, MF);
2393
2394 if (!PostRAMachineSinkingImpl().run(MF))
2395 return PreservedAnalyses::all();
2396
2399 return PA;
2400}
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
basic Basic Alias true
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define DEBUG_TYPE
static const HTTPClientCleanup Cleanup
static Register UseReg(const MachineOperand &MO)
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
iv Induction Variable Users
Definition IVUsers.cpp:48
#define I(x, y, z)
Definition MD5.cpp:57
static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI)
Return true if this machine instruction loads from global offset table or constant pool.
static cl::opt< unsigned > SinkLoadInstsPerBlockThreshold("machine-sink-load-instrs-threshold", cl::desc("Do not try to find alias store for a load if there is a in-path " "block whose instruction number is higher than this threshold."), cl::init(2000), cl::Hidden)
static cl::opt< unsigned > SinkIntoCycleLimit("machine-sink-cycle-limit", cl::desc("The maximum number of instructions considered for cycle sinking."), cl::init(50), cl::Hidden)
TargetInstrInfo::RegSubRegPair RegSubRegPair
Register Reg
static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB, const SmallVectorImpl< unsigned > &UsedOpsInCopy, const LiveRegUnits &UsedRegUnits, const TargetRegisterInfo *TRI)
static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo, MachineBasicBlock::iterator InsertPos, ArrayRef< MIRegs > DbgValuesToSink)
Sink an instruction and its associated debug instructions.
static cl::opt< bool > SplitEdges("machine-sink-split", cl::desc("Split critical edges during machine sinking"), cl::init(true), cl::Hidden)
static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI)
Return true if MI is likely to be usable as a memory operation by the implicit null check optimizatio...
static cl::opt< bool > SinkInstsIntoCycle("sink-insts-to-avoid-spills", cl::desc("Sink instructions into cycles to avoid " "register spills"), cl::init(false), cl::Hidden)
static cl::opt< unsigned > SinkLoadBlocksThreshold("machine-sink-load-blocks-threshold", cl::desc("Do not try to find alias store for a load if the block number in " "the straight line is higher than this threshold."), cl::init(20), cl::Hidden)
static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB, const SmallVectorImpl< unsigned > &UsedOpsInCopy, const SmallVectorImpl< Register > &DefedRegsInCopy)
static bool hasRegisterDependency(MachineInstr *MI, SmallVectorImpl< unsigned > &UsedOpsInCopy, SmallVectorImpl< Register > &DefedRegsInCopy, LiveRegUnits &ModifiedRegUnits, LiveRegUnits &UsedRegUnits)
Register const TargetRegisterInfo * TRI
std::pair< MachineInstr *, SmallVector< Register, 2 > > MIRegs
Machine code static false bool blockPrologueInterferes(const MachineBasicBlock *BB, MachineBasicBlock::const_iterator End, const MachineInstr &MI, const TargetRegisterInfo *TRI, const TargetInstrInfo *TII, const MachineRegisterInfo *MRI)
Return true if a target defined block prologue instruction interferes with a sink candidate.
static cl::opt< unsigned > SplitEdgeProbabilityThreshold("machine-sink-split-probability-threshold", cl::desc("Percentage threshold for splitting single-instruction critical edge. " "If the branch threshold is higher than this threshold, we allow " "speculative execution of up to 1 instruction to avoid branching to " "splitted critical edge"), cl::init(40), cl::Hidden)
static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI, Register Reg)
If the sunk instruction is a copy, try to forward the copy instead of leaving an 'undef' DBG_VALUE in...
static cl::opt< bool > UseBlockFreqInfo("machine-sink-bfi", cl::desc("Use block frequency info to find successors to sink"), cl::init(true), cl::Hidden)
static MachineBasicBlock * getSingleLiveInSuccBB(MachineBasicBlock &CurBB, const SmallPtrSetImpl< MachineBasicBlock * > &SinkableBBs, Register Reg, const TargetRegisterInfo *TRI)
This file implements a map that provides insertion order iteration.
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the PointerIntPair class.
Remove Loads Into Fake Uses
static const char * name
This file implements a set that has insertion order iteration characteristics.
static bool ProcessBlock(BasicBlock &BB, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
Definition Sink.cpp:173
static bool SinkInstruction(Instruction *Inst, SmallPtrSetImpl< Instruction * > &Stores, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
SinkInstruction - Determine whether it is safe to sink the specified machine instruction out of its c...
Definition Sink.cpp:103
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
Target-Independent Code Generator Pass Configuration Options pass.
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
static LLVM_ABI DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
Definition DebugLoc.cpp:179
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
iterator_range< const_toplevel_iterator > toplevel_cycles() const
void splitCriticalEdge(BlockT *Pred, BlockT *Succ, BlockT *New)
unsigned getCycleDepth(const BlockT *Block) const
get the depth for the cycle which containing a given block.
CycleT * getCycle(const BlockT *Block) const
Find the innermost cycle containing a given block.
BlockT * getHeader() const
bool isReducible() const
Whether the cycle is a natural loop.
BlockT * getCyclePreheader() const
Return the preheader block for this cycle.
bool contains(const BlockT *Block) const
Return whether Block is contained in the cycle.
Module * getParent()
Get the module that this global value is contained inside of...
bool isAsCheapAsAMove(const MachineInstr &MI) const override
bool shouldSink(const MachineInstr &MI) const override
A set of register units used to track register liveness.
static void accumulateUsedDefed(const MachineInstr &MI, LiveRegUnits &ModifiedRegUnits, LiveRegUnits &UsedRegUnits, const TargetRegisterInfo *TRI)
For a machine instruction MI, adds all register units used in UsedRegUnits and defined or clobbered i...
bool available(MCRegister Reg) const
Returns true if no part of physical register Reg is live.
void init(const TargetRegisterInfo &TRI)
Initialize and clear the set.
LLVM_ABI void addLiveIns(const MachineBasicBlock &MBB)
Adds registers living into block MBB.
void clear()
Clears the set.
An RAII based helper class to modify MachineFunctionProperties when running pass.
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
bool isEHPad() const
Returns true if the block is a landing pad.
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
LLVM_ABI void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
LLVM_ABI void removeLiveInOverlappedWith(MCRegister Reg)
Remove the specified register from any overlapped live in.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
LLVM_ABI BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
LLVM_ABI void onEdgeSplit(const MachineBasicBlock &NewPredecessor, const MachineBasicBlock &NewSuccessor, const MachineBranchProbabilityInfo &MBPI)
incrementally calculate block frequencies when we split edges, to avoid full CFG traversal.
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Legacy analysis pass which computes a MachineCycleInfo.
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
bool hasDebugOperandForReg(Register Reg) const
Returns whether this debug value has at least one debug operand with the register Reg.
void setDebugValueUndef()
Sets all register debug operands in this debug value instruction to be undef.
LLVM_ABI iterator_range< filter_iterator< const MachineOperand *, std::function< bool(const MachineOperand &Op)> > > getDebugOperandsForReg(Register Reg) const
Returns a range of all of the operands that correspond to a debug use of Reg.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
bool isCopy() const
const MachineBasicBlock * getParent() const
bool isCopyLike() const
Return true if the instruction behaves like a copy.
bool isDebugInstr() const
LLVM_ABI void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, const TargetRegisterInfo &RegInfo)
Replace all occurrences of FromReg with ToReg:SubIdx, properly composing subreg indices where necessa...
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
filtered_mop_range all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
const MachineOperand & getOperand(unsigned i) const
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
Analysis pass that exposes the MachineLoopInfo for a machine function.
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
void setIsKill(bool Val=true)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
VectorType takeVector()
Clear the MapVector and return the underlying vector.
Definition MapVector.h:48
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void dump() const
Definition Pass.cpp:146
PointerIntPair - This class implements a pair of a pointer and small integer.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
Special value supplied for machine level alias analysis.
unsigned getRegPressureSetLimit(unsigned Idx) const
Get the register unit limit for the given pressure set index.
LLVM_ABI void runOnMachineFunction(const MachineFunction &MF, bool Rev=false)
runOnFunction - Prepare to answer questions about MF.
LLVM_ABI void collect(const MachineInstr &MI, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI, bool TrackLaneMasks, bool IgnoreDead)
Analyze the given instruction MI and fill in the Uses, Defs and DeadDefs list based on the MachineOpe...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
A vector that has set insertion semantics.
Definition SetVector.h:57
SlotIndexes pass.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:133
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
TargetInstrInfo - Interface to description of machine instruction set.
Target-Independent Code Generator Pass Configuration Options.
bool getEnableSinkAndFold() const
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
bool hasSuperClassEq(const TargetRegisterClass *RC) const
Returns true if RC is a super-class of or equal to this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Provide an instruction scheduling machine model to CodeGen passes.
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type size() const
Definition DenseSet.h:87
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
Abstract Attribute helper functions.
Definition Attributor.h:165
@ Entry
Definition COFF.h:862
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
@ User
could "use" a pointer
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
void stable_sort(R &&Range)
Definition STLExtras.h:2106
LLVM_ABI void initializeMachineSinkingLegacyPass(PassRegistry &)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI bool isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
CycleInfo::CycleT Cycle
Definition CycleInfo.h:24
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI char & PostRAMachineSinkingID
This pass perform post-ra machine sink for COPY instructions.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI char & MachineSinkingLegacyID
MachineSinking - This pass performs sinking on machine instructions.
iterator_range< df_iterator< T > > depth_first(const T &G)
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
GenericCycleInfo< MachineSSAContext > MachineCycleInfo
MachineCycleInfo::CycleT MachineCycle
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Represents a predicate at the MachineFunction level.
A pair composed of a register and a sub-register index.