LLVM 22.0.0git
RegisterCoalescer.cpp
Go to the documentation of this file.
1//===- RegisterCoalescer.cpp - Generic Register Coalescing Interface ------===//
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 file implements the generic RegisterCoalescer interface which
10// is used as the common interface used by all clients and
11// implementations of register coalescing.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RegisterCoalescer.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Statistic.h"
37#include "llvm/CodeGen/Passes.h"
45#include "llvm/IR/DebugLoc.h"
47#include "llvm/MC/LaneBitmask.h"
48#include "llvm/MC/MCInstrDesc.h"
50#include "llvm/Pass.h"
53#include "llvm/Support/Debug.h"
56#include <algorithm>
57#include <cassert>
58#include <iterator>
59#include <limits>
60#include <tuple>
61#include <utility>
62#include <vector>
63
64using namespace llvm;
65
66#define DEBUG_TYPE "regalloc"
67
68STATISTIC(numJoins, "Number of interval joins performed");
69STATISTIC(numCrossRCs, "Number of cross class joins performed");
70STATISTIC(numCommutes, "Number of instruction commuting performed");
71STATISTIC(numExtends, "Number of copies extended");
72STATISTIC(NumReMats, "Number of instructions re-materialized");
73STATISTIC(NumInflated, "Number of register classes inflated");
74STATISTIC(NumLaneConflicts, "Number of dead lane conflicts tested");
75STATISTIC(NumLaneResolves, "Number of dead lane conflicts resolved");
76STATISTIC(NumShrinkToUses, "Number of shrinkToUses called");
77
78static cl::opt<bool> EnableJoining("join-liveintervals",
79 cl::desc("Coalesce copies (default=true)"),
80 cl::init(true), cl::Hidden);
81
82static cl::opt<bool> UseTerminalRule("terminal-rule",
83 cl::desc("Apply the terminal rule"),
84 cl::init(false), cl::Hidden);
85
86/// Temporary flag to test critical edge unsplitting.
88 "join-splitedges",
89 cl::desc("Coalesce copies on split edges (default=subtarget)"), cl::Hidden);
90
91/// Temporary flag to test global copy optimization.
93 "join-globalcopies",
94 cl::desc("Coalesce copies that span blocks (default=subtarget)"),
96
98 "verify-coalescing",
99 cl::desc("Verify machine instrs before and after register coalescing"),
100 cl::Hidden);
101
103 "late-remat-update-threshold", cl::Hidden,
104 cl::desc("During rematerialization for a copy, if the def instruction has "
105 "many other copy uses to be rematerialized, delay the multiple "
106 "separate live interval update work and do them all at once after "
107 "all those rematerialization are done. It will save a lot of "
108 "repeated work. "),
109 cl::init(100));
110
112 "large-interval-size-threshold", cl::Hidden,
113 cl::desc("If the valnos size of an interval is larger than the threshold, "
114 "it is regarded as a large interval. "),
115 cl::init(100));
116
118 "large-interval-freq-threshold", cl::Hidden,
119 cl::desc("For a large interval, if it is coalesced with other live "
120 "intervals many times more than the threshold, stop its "
121 "coalescing to control the compile time. "),
122 cl::init(256));
123
124namespace {
125
126class JoinVals;
127
128class RegisterCoalescer : private LiveRangeEdit::Delegate {
129 MachineFunction *MF = nullptr;
130 MachineRegisterInfo *MRI = nullptr;
131 const TargetRegisterInfo *TRI = nullptr;
132 const TargetInstrInfo *TII = nullptr;
133 LiveIntervals *LIS = nullptr;
134 SlotIndexes *SI = nullptr;
135 const MachineLoopInfo *Loops = nullptr;
136 RegisterClassInfo RegClassInfo;
137
138 /// Position and VReg of a PHI instruction during coalescing.
139 struct PHIValPos {
140 SlotIndex SI; ///< Slot where this PHI occurs.
141 Register Reg; ///< VReg the PHI occurs in.
142 unsigned SubReg; ///< Qualifying subregister for Reg.
143 };
144
145 /// Map from debug instruction number to PHI position during coalescing.
146 DenseMap<unsigned, PHIValPos> PHIValToPos;
147 /// Index of, for each VReg, which debug instruction numbers and
148 /// corresponding PHIs are sensitive to coalescing. Each VReg may have
149 /// multiple PHI defs, at different positions.
150 DenseMap<Register, SmallVector<unsigned, 2>> RegToPHIIdx;
151
152 /// Debug variable location tracking -- for each VReg, maintain an
153 /// ordered-by-slot-index set of DBG_VALUEs, to help quick
154 /// identification of whether coalescing may change location validity.
155 using DbgValueLoc = std::pair<SlotIndex, MachineInstr *>;
156 DenseMap<Register, std::vector<DbgValueLoc>> DbgVRegToValues;
157
158 /// A LaneMask to remember on which subregister live ranges we need to call
159 /// shrinkToUses() later.
160 LaneBitmask ShrinkMask;
161
162 /// True if the main range of the currently coalesced intervals should be
163 /// checked for smaller live intervals.
164 bool ShrinkMainRange = false;
165
166 /// True if the coalescer should aggressively coalesce global copies
167 /// in favor of keeping local copies.
168 bool JoinGlobalCopies = false;
169
170 /// True if the coalescer should aggressively coalesce fall-thru
171 /// blocks exclusively containing copies.
172 bool JoinSplitEdges = false;
173
174 /// Copy instructions yet to be coalesced.
175 SmallVector<MachineInstr *, 8> WorkList;
176 SmallVector<MachineInstr *, 8> LocalWorkList;
177
178 /// Set of instruction pointers that have been erased, and
179 /// that may be present in WorkList.
180 SmallPtrSet<MachineInstr *, 8> ErasedInstrs;
181
182 /// Dead instructions that are about to be deleted.
183 SmallVector<MachineInstr *, 8> DeadDefs;
184
185 /// Virtual registers to be considered for register class inflation.
186 SmallVector<Register, 8> InflateRegs;
187
188 /// The collection of live intervals which should have been updated
189 /// immediately after rematerialiation but delayed until
190 /// lateLiveIntervalUpdate is called.
191 DenseSet<Register> ToBeUpdated;
192
193 /// Record how many times the large live interval with many valnos
194 /// has been tried to join with other live interval.
195 DenseMap<Register, unsigned long> LargeLIVisitCounter;
196
197 /// Recursively eliminate dead defs in DeadDefs.
198 void eliminateDeadDefs(LiveRangeEdit *Edit = nullptr);
199
200 /// LiveRangeEdit callback for eliminateDeadDefs().
201 void LRE_WillEraseInstruction(MachineInstr *MI) override;
202
203 /// Coalesce the LocalWorkList.
204 void coalesceLocals();
205
206 /// Join compatible live intervals
207 void joinAllIntervals();
208
209 /// Coalesce copies in the specified MBB, putting
210 /// copies that cannot yet be coalesced into WorkList.
211 void copyCoalesceInMBB(MachineBasicBlock *MBB);
212
213 /// Tries to coalesce all copies in CurrList. Returns true if any progress
214 /// was made.
215 bool copyCoalesceWorkList(MutableArrayRef<MachineInstr *> CurrList);
216
217 /// If one def has many copy like uses, and those copy uses are all
218 /// rematerialized, the live interval update needed for those
219 /// rematerializations will be delayed and done all at once instead
220 /// of being done multiple times. This is to save compile cost because
221 /// live interval update is costly.
222 void lateLiveIntervalUpdate();
223
224 /// Check if the incoming value defined by a COPY at \p SLRQ in the subrange
225 /// has no value defined in the predecessors. If the incoming value is the
226 /// same as defined by the copy itself, the value is considered undefined.
227 bool copyValueUndefInPredecessors(LiveRange &S, const MachineBasicBlock *MBB,
228 LiveQueryResult SLRQ);
229
230 /// Set necessary undef flags on subregister uses after pruning out undef
231 /// lane segments from the subrange.
232 void setUndefOnPrunedSubRegUses(LiveInterval &LI, Register Reg,
233 LaneBitmask PrunedLanes);
234
235 /// Attempt to join intervals corresponding to SrcReg/DstReg, which are the
236 /// src/dst of the copy instruction CopyMI. This returns true if the copy
237 /// was successfully coalesced away. If it is not currently possible to
238 /// coalesce this interval, but it may be possible if other things get
239 /// coalesced, then it returns true by reference in 'Again'.
240 bool joinCopy(MachineInstr *CopyMI, bool &Again,
241 SmallPtrSetImpl<MachineInstr *> &CurrentErasedInstrs);
242
243 /// Attempt to join these two intervals. On failure, this
244 /// returns false. The output "SrcInt" will not have been modified, so we
245 /// can use this information below to update aliases.
246 bool joinIntervals(CoalescerPair &CP);
247
248 /// Attempt joining two virtual registers. Return true on success.
249 bool joinVirtRegs(CoalescerPair &CP);
250
251 /// If a live interval has many valnos and is coalesced with other
252 /// live intervals many times, we regard such live interval as having
253 /// high compile time cost.
254 bool isHighCostLiveInterval(LiveInterval &LI);
255
256 /// Attempt joining with a reserved physreg.
257 bool joinReservedPhysReg(CoalescerPair &CP);
258
259 /// Add the LiveRange @p ToMerge as a subregister liverange of @p LI.
260 /// Subranges in @p LI which only partially interfere with the desired
261 /// LaneMask are split as necessary. @p LaneMask are the lanes that
262 /// @p ToMerge will occupy in the coalescer register. @p LI has its subrange
263 /// lanemasks already adjusted to the coalesced register.
264 void mergeSubRangeInto(LiveInterval &LI, const LiveRange &ToMerge,
265 LaneBitmask LaneMask, CoalescerPair &CP,
266 unsigned DstIdx);
267
268 /// Join the liveranges of two subregisters. Joins @p RRange into
269 /// @p LRange, @p RRange may be invalid afterwards.
270 void joinSubRegRanges(LiveRange &LRange, LiveRange &RRange,
271 LaneBitmask LaneMask, const CoalescerPair &CP);
272
273 /// We found a non-trivially-coalescable copy. If the source value number is
274 /// defined by a copy from the destination reg see if we can merge these two
275 /// destination reg valno# into a single value number, eliminating a copy.
276 /// This returns true if an interval was modified.
277 bool adjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI);
278
279 /// Return true if there are definitions of IntB
280 /// other than BValNo val# that can reach uses of AValno val# of IntA.
281 bool hasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
282 VNInfo *AValNo, VNInfo *BValNo);
283
284 /// We found a non-trivially-coalescable copy.
285 /// If the source value number is defined by a commutable instruction and
286 /// its other operand is coalesced to the copy dest register, see if we
287 /// can transform the copy into a noop by commuting the definition.
288 /// This returns a pair of two flags:
289 /// - the first element is true if an interval was modified,
290 /// - the second element is true if the destination interval needs
291 /// to be shrunk after deleting the copy.
292 std::pair<bool, bool> removeCopyByCommutingDef(const CoalescerPair &CP,
293 MachineInstr *CopyMI);
294
295 /// We found a copy which can be moved to its less frequent predecessor.
296 bool removePartialRedundancy(const CoalescerPair &CP, MachineInstr &CopyMI);
297
298 /// If the source of a copy is defined by a CheapAsAMove computation,
299 /// replace the copy by rematerialize the definition.
300 bool reMaterializeDef(const CoalescerPair &CP, MachineInstr *CopyMI,
301 bool &IsDefCopy);
302
303 /// Return true if a copy involving a physreg should be joined.
304 bool canJoinPhys(const CoalescerPair &CP);
305
306 /// Replace all defs and uses of SrcReg to DstReg and update the subregister
307 /// number if it is not zero. If DstReg is a physical register and the
308 /// existing subregister number of the def / use being updated is not zero,
309 /// make sure to set it to the correct physical subregister.
310 void updateRegDefsUses(Register SrcReg, Register DstReg, unsigned SubIdx);
311
312 /// If the given machine operand reads only undefined lanes add an undef
313 /// flag.
314 /// This can happen when undef uses were previously concealed by a copy
315 /// which we coalesced. Example:
316 /// %0:sub0<def,read-undef> = ...
317 /// %1 = COPY %0 <-- Coalescing COPY reveals undef
318 /// = use %1:sub1 <-- hidden undef use
319 void addUndefFlag(const LiveInterval &Int, SlotIndex UseIdx,
320 MachineOperand &MO, unsigned SubRegIdx);
321
322 /// Handle copies of undef values. If the undef value is an incoming
323 /// PHI value, it will convert @p CopyMI to an IMPLICIT_DEF.
324 /// Returns nullptr if @p CopyMI was not in any way eliminable. Otherwise,
325 /// it returns @p CopyMI (which could be an IMPLICIT_DEF at this point).
326 MachineInstr *eliminateUndefCopy(MachineInstr *CopyMI);
327
328 /// Check whether or not we should apply the terminal rule on the
329 /// destination (Dst) of \p Copy.
330 /// When the terminal rule applies, Copy is not profitable to
331 /// coalesce.
332 /// Dst is terminal if it has exactly one affinity (Dst, Src) and
333 /// at least one interference (Dst, Dst2). If Dst is terminal, the
334 /// terminal rule consists in checking that at least one of
335 /// interfering node, say Dst2, has an affinity of equal or greater
336 /// weight with Src.
337 /// In that case, Dst2 and Dst will not be able to be both coalesced
338 /// with Src. Since Dst2 exposes more coalescing opportunities than
339 /// Dst, we can drop \p Copy.
340 bool applyTerminalRule(const MachineInstr &Copy) const;
341
342 /// Wrapper method for \see LiveIntervals::shrinkToUses.
343 /// This method does the proper fixing of the live-ranges when the afore
344 /// mentioned method returns true.
345 void shrinkToUses(LiveInterval *LI,
346 SmallVectorImpl<MachineInstr *> *Dead = nullptr) {
347 NumShrinkToUses++;
348 if (LIS->shrinkToUses(LI, Dead)) {
349 /// Check whether or not \p LI is composed by multiple connected
350 /// components and if that is the case, fix that.
352 LIS->splitSeparateComponents(*LI, SplitLIs);
353 }
354 }
355
356 /// Wrapper Method to do all the necessary work when an Instruction is
357 /// deleted.
358 /// Optimizations should use this to make sure that deleted instructions
359 /// are always accounted for.
360 void deleteInstr(MachineInstr *MI) {
361 ErasedInstrs.insert(MI);
362 LIS->RemoveMachineInstrFromMaps(*MI);
363 MI->eraseFromParent();
364 }
365
366 /// Walk over function and initialize the DbgVRegToValues map.
367 void buildVRegToDbgValueMap(MachineFunction &MF);
368
369 /// Test whether, after merging, any DBG_VALUEs would refer to a
370 /// different value number than before merging, and whether this can
371 /// be resolved. If not, mark the DBG_VALUE as being undef.
372 void checkMergingChangesDbgValues(CoalescerPair &CP, LiveRange &LHS,
373 JoinVals &LHSVals, LiveRange &RHS,
374 JoinVals &RHSVals);
375
376 void checkMergingChangesDbgValuesImpl(Register Reg, LiveRange &OtherRange,
377 LiveRange &RegRange, JoinVals &Vals2);
378
379public:
380 // For legacy pass only.
381 RegisterCoalescer() {}
382 RegisterCoalescer &operator=(RegisterCoalescer &&Other) = default;
383
384 RegisterCoalescer(LiveIntervals *LIS, SlotIndexes *SI,
385 const MachineLoopInfo *Loops)
386 : LIS(LIS), SI(SI), Loops(Loops) {}
387
388 bool run(MachineFunction &MF);
389};
390
391class RegisterCoalescerLegacy : public MachineFunctionPass {
392public:
393 static char ID; ///< Class identification, replacement for typeinfo
394
395 RegisterCoalescerLegacy() : MachineFunctionPass(ID) {
397 }
398
399 void getAnalysisUsage(AnalysisUsage &AU) const override;
400
401 MachineFunctionProperties getClearedProperties() const override {
402 return MachineFunctionProperties().setIsSSA();
403 }
404
405 /// This is the pass entry point.
406 bool runOnMachineFunction(MachineFunction &) override;
407};
408
409} // end anonymous namespace
410
411char RegisterCoalescerLegacy::ID = 0;
412
413char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy::ID;
414
415INITIALIZE_PASS_BEGIN(RegisterCoalescerLegacy, "register-coalescer",
416 "Register Coalescer", false, false)
420INITIALIZE_PASS_END(RegisterCoalescerLegacy, "register-coalescer",
421 "Register Coalescer", false, false)
422
423[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
425 Register &Dst, unsigned &SrcSub,
426 unsigned &DstSub) {
427 if (MI->isCopy()) {
428 Dst = MI->getOperand(0).getReg();
429 DstSub = MI->getOperand(0).getSubReg();
430 Src = MI->getOperand(1).getReg();
431 SrcSub = MI->getOperand(1).getSubReg();
432 } else if (MI->isSubregToReg()) {
433 Dst = MI->getOperand(0).getReg();
434 DstSub = tri.composeSubRegIndices(MI->getOperand(0).getSubReg(),
435 MI->getOperand(3).getImm());
436 Src = MI->getOperand(2).getReg();
437 SrcSub = MI->getOperand(2).getSubReg();
438 } else
439 return false;
440 return true;
441}
442
443/// Return true if this block should be vacated by the coalescer to eliminate
444/// branches. The important cases to handle in the coalescer are critical edges
445/// split during phi elimination which contain only copies. Simple blocks that
446/// contain non-branches should also be vacated, but this can be handled by an
447/// earlier pass similar to early if-conversion.
448static bool isSplitEdge(const MachineBasicBlock *MBB) {
449 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
450 return false;
451
452 for (const auto &MI : *MBB) {
453 if (!MI.isCopyLike() && !MI.isUnconditionalBranch())
454 return false;
455 }
456 return true;
457}
458
460 SrcReg = DstReg = Register();
461 SrcIdx = DstIdx = 0;
462 NewRC = nullptr;
463 Flipped = CrossClass = false;
464
465 Register Src, Dst;
466 unsigned SrcSub = 0, DstSub = 0;
467 if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
468 return false;
469 Partial = SrcSub || DstSub;
470
471 // If one register is a physreg, it must be Dst.
472 if (Src.isPhysical()) {
473 if (Dst.isPhysical())
474 return false;
475 std::swap(Src, Dst);
476 std::swap(SrcSub, DstSub);
477 Flipped = true;
478 }
479
480 const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
481 const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
482
483 if (Dst.isPhysical()) {
484 // Eliminate DstSub on a physreg.
485 if (DstSub) {
486 Dst = TRI.getSubReg(Dst, DstSub);
487 if (!Dst)
488 return false;
489 DstSub = 0;
490 }
491
492 // Eliminate SrcSub by picking a corresponding Dst superregister.
493 if (SrcSub) {
494 Dst = TRI.getMatchingSuperReg(Dst, SrcSub, SrcRC);
495 if (!Dst)
496 return false;
497 } else if (!SrcRC->contains(Dst)) {
498 return false;
499 }
500 } else {
501 // Both registers are virtual.
502 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
503
504 // Both registers have subreg indices.
505 if (SrcSub && DstSub) {
506 // Copies between different sub-registers are never coalescable.
507 if (Src == Dst && SrcSub != DstSub)
508 return false;
509
510 NewRC = TRI.getCommonSuperRegClass(SrcRC, SrcSub, DstRC, DstSub, SrcIdx,
511 DstIdx);
512 if (!NewRC)
513 return false;
514 } else if (DstSub) {
515 // SrcReg will be merged with a sub-register of DstReg.
516 SrcIdx = DstSub;
517 NewRC = TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSub);
518 } else if (SrcSub) {
519 // DstReg will be merged with a sub-register of SrcReg.
520 DstIdx = SrcSub;
521 NewRC = TRI.getMatchingSuperRegClass(SrcRC, DstRC, SrcSub);
522 } else {
523 // This is a straight copy without sub-registers.
524 NewRC = TRI.getCommonSubClass(DstRC, SrcRC);
525 }
526
527 // The combined constraint may be impossible to satisfy.
528 if (!NewRC)
529 return false;
530
531 // Prefer SrcReg to be a sub-register of DstReg.
532 // FIXME: Coalescer should support subregs symmetrically.
533 if (DstIdx && !SrcIdx) {
534 std::swap(Src, Dst);
535 std::swap(SrcIdx, DstIdx);
536 Flipped = !Flipped;
537 }
538
539 CrossClass = NewRC != DstRC || NewRC != SrcRC;
540 }
541 // Check our invariants
542 assert(Src.isVirtual() && "Src must be virtual");
543 assert(!(Dst.isPhysical() && DstSub) && "Cannot have a physical SubIdx");
544 SrcReg = Src;
545 DstReg = Dst;
546 return true;
547}
548
550 if (DstReg.isPhysical())
551 return false;
552 std::swap(SrcReg, DstReg);
553 std::swap(SrcIdx, DstIdx);
554 Flipped = !Flipped;
555 return true;
556}
557
559 if (!MI)
560 return false;
561 Register Src, Dst;
562 unsigned SrcSub = 0, DstSub = 0;
563 if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
564 return false;
565
566 // Find the virtual register that is SrcReg.
567 if (Dst == SrcReg) {
568 std::swap(Src, Dst);
569 std::swap(SrcSub, DstSub);
570 } else if (Src != SrcReg) {
571 return false;
572 }
573
574 // Now check that Dst matches DstReg.
575 if (DstReg.isPhysical()) {
576 if (!Dst.isPhysical())
577 return false;
578 assert(!DstIdx && !SrcIdx && "Inconsistent CoalescerPair state.");
579 // DstSub could be set for a physreg from INSERT_SUBREG.
580 if (DstSub)
581 Dst = TRI.getSubReg(Dst, DstSub);
582 // Full copy of Src.
583 if (!SrcSub)
584 return DstReg == Dst;
585 // This is a partial register copy. Check that the parts match.
586 return Register(TRI.getSubReg(DstReg, SrcSub)) == Dst;
587 }
588
589 // DstReg is virtual.
590 if (DstReg != Dst)
591 return false;
592 // Registers match, do the subregisters line up?
593 return TRI.composeSubRegIndices(SrcIdx, SrcSub) ==
594 TRI.composeSubRegIndices(DstIdx, DstSub);
595}
596
597void RegisterCoalescerLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
598 AU.setPreservesCFG();
607}
608
609void RegisterCoalescer::eliminateDeadDefs(LiveRangeEdit *Edit) {
610 if (Edit) {
611 Edit->eliminateDeadDefs(DeadDefs);
612 return;
613 }
615 LiveRangeEdit(nullptr, NewRegs, *MF, *LIS, nullptr, this)
616 .eliminateDeadDefs(DeadDefs);
617}
618
619void RegisterCoalescer::LRE_WillEraseInstruction(MachineInstr *MI) {
620 // MI may be in WorkList. Make sure we don't visit it.
621 ErasedInstrs.insert(MI);
622}
623
624bool RegisterCoalescer::adjustCopiesBackFrom(const CoalescerPair &CP,
625 MachineInstr *CopyMI) {
626 assert(!CP.isPartial() && "This doesn't work for partial copies.");
627 assert(!CP.isPhys() && "This doesn't work for physreg copies.");
628
629 LiveInterval &IntA =
630 LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
631 LiveInterval &IntB =
632 LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
633 SlotIndex CopyIdx = LIS->getInstructionIndex(*CopyMI).getRegSlot();
634
635 // We have a non-trivially-coalescable copy with IntA being the source and
636 // IntB being the dest, thus this defines a value number in IntB. If the
637 // source value number (in IntA) is defined by a copy from B, see if we can
638 // merge these two pieces of B into a single value number, eliminating a copy.
639 // For example:
640 //
641 // A3 = B0
642 // ...
643 // B1 = A3 <- this copy
644 //
645 // In this case, B0 can be extended to where the B1 copy lives, allowing the
646 // B1 value number to be replaced with B0 (which simplifies the B
647 // liveinterval).
648
649 // BValNo is a value number in B that is defined by a copy from A. 'B1' in
650 // the example above.
652 if (BS == IntB.end())
653 return false;
654 VNInfo *BValNo = BS->valno;
655
656 // Get the location that B is defined at. Two options: either this value has
657 // an unknown definition point or it is defined at CopyIdx. If unknown, we
658 // can't process it.
659 if (BValNo->def != CopyIdx)
660 return false;
661
662 // AValNo is the value number in A that defines the copy, A3 in the example.
663 SlotIndex CopyUseIdx = CopyIdx.getRegSlot(true);
664 LiveInterval::iterator AS = IntA.FindSegmentContaining(CopyUseIdx);
665 // The live segment might not exist after fun with physreg coalescing.
666 if (AS == IntA.end())
667 return false;
668 VNInfo *AValNo = AS->valno;
669
670 // If AValNo is defined as a copy from IntB, we can potentially process this.
671 // Get the instruction that defines this value number.
672 MachineInstr *ACopyMI = LIS->getInstructionFromIndex(AValNo->def);
673 // Don't allow any partial copies, even if isCoalescable() allows them.
674 if (!CP.isCoalescable(ACopyMI) || !ACopyMI->isFullCopy())
675 return false;
676
677 // Get the Segment in IntB that this value number starts with.
679 IntB.FindSegmentContaining(AValNo->def.getPrevSlot());
680 if (ValS == IntB.end())
681 return false;
682
683 // Make sure that the end of the live segment is inside the same block as
684 // CopyMI.
685 MachineInstr *ValSEndInst =
686 LIS->getInstructionFromIndex(ValS->end.getPrevSlot());
687 if (!ValSEndInst || ValSEndInst->getParent() != CopyMI->getParent())
688 return false;
689
690 // Okay, we now know that ValS ends in the same block that the CopyMI
691 // live-range starts. If there are no intervening live segments between them
692 // in IntB, we can merge them.
693 if (ValS + 1 != BS)
694 return false;
695
696 LLVM_DEBUG(dbgs() << "Extending: " << printReg(IntB.reg(), TRI));
697
698 SlotIndex FillerStart = ValS->end, FillerEnd = BS->start;
699 // We are about to delete CopyMI, so need to remove it as the 'instruction
700 // that defines this value #'. Update the valnum with the new defining
701 // instruction #.
702 BValNo->def = FillerStart;
703
704 // Okay, we can merge them. We need to insert a new liverange:
705 // [ValS.end, BS.begin) of either value number, then we merge the
706 // two value numbers.
707 IntB.addSegment(LiveInterval::Segment(FillerStart, FillerEnd, BValNo));
708
709 // Okay, merge "B1" into the same value number as "B0".
710 if (BValNo != ValS->valno)
711 IntB.MergeValueNumberInto(BValNo, ValS->valno);
712
713 // Do the same for the subregister segments.
714 for (LiveInterval::SubRange &S : IntB.subranges()) {
715 // Check for SubRange Segments of the form [1234r,1234d:0) which can be
716 // removed to prevent creating bogus SubRange Segments.
717 LiveInterval::iterator SS = S.FindSegmentContaining(CopyIdx);
718 if (SS != S.end() && SlotIndex::isSameInstr(SS->start, SS->end)) {
719 S.removeSegment(*SS, true);
720 continue;
721 }
722 // The subrange may have ended before FillerStart. If so, extend it.
723 if (!S.getVNInfoAt(FillerStart)) {
724 SlotIndex BBStart =
725 LIS->getMBBStartIdx(LIS->getMBBFromIndex(FillerStart));
726 S.extendInBlock(BBStart, FillerStart);
727 }
728 VNInfo *SubBValNo = S.getVNInfoAt(CopyIdx);
729 S.addSegment(LiveInterval::Segment(FillerStart, FillerEnd, SubBValNo));
730 VNInfo *SubValSNo = S.getVNInfoAt(AValNo->def.getPrevSlot());
731 if (SubBValNo != SubValSNo)
732 S.MergeValueNumberInto(SubBValNo, SubValSNo);
733 }
734
735 LLVM_DEBUG(dbgs() << " result = " << IntB << '\n');
736
737 // If the source instruction was killing the source register before the
738 // merge, unset the isKill marker given the live range has been extended.
739 int UIdx =
740 ValSEndInst->findRegisterUseOperandIdx(IntB.reg(), /*TRI=*/nullptr, true);
741 if (UIdx != -1) {
742 ValSEndInst->getOperand(UIdx).setIsKill(false);
743 }
744
745 // Rewrite the copy.
746 CopyMI->substituteRegister(IntA.reg(), IntB.reg(), 0, *TRI);
747 // If the copy instruction was killing the destination register or any
748 // subrange before the merge trim the live range.
749 bool RecomputeLiveRange = AS->end == CopyIdx;
750 if (!RecomputeLiveRange) {
751 for (LiveInterval::SubRange &S : IntA.subranges()) {
752 LiveInterval::iterator SS = S.FindSegmentContaining(CopyUseIdx);
753 if (SS != S.end() && SS->end == CopyIdx) {
754 RecomputeLiveRange = true;
755 break;
756 }
757 }
758 }
759 if (RecomputeLiveRange)
760 shrinkToUses(&IntA);
761
762 ++numExtends;
763 return true;
764}
765
766bool RegisterCoalescer::hasOtherReachingDefs(LiveInterval &IntA,
767 LiveInterval &IntB, VNInfo *AValNo,
768 VNInfo *BValNo) {
769 // If AValNo has PHI kills, conservatively assume that IntB defs can reach
770 // the PHI values.
771 if (LIS->hasPHIKill(IntA, AValNo))
772 return true;
773
774 for (LiveRange::Segment &ASeg : IntA.segments) {
775 if (ASeg.valno != AValNo)
776 continue;
778 if (BI != IntB.begin())
779 --BI;
780 for (; BI != IntB.end() && ASeg.end >= BI->start; ++BI) {
781 if (BI->valno == BValNo)
782 continue;
783 if (BI->start <= ASeg.start && BI->end > ASeg.start)
784 return true;
785 if (BI->start > ASeg.start && BI->start < ASeg.end)
786 return true;
787 }
788 }
789 return false;
790}
791
792/// Copy segments with value number @p SrcValNo from liverange @p Src to live
793/// range @Dst and use value number @p DstValNo there.
794static std::pair<bool, bool> addSegmentsWithValNo(LiveRange &Dst,
795 VNInfo *DstValNo,
796 const LiveRange &Src,
797 const VNInfo *SrcValNo) {
798 bool Changed = false;
799 bool MergedWithDead = false;
800 for (const LiveRange::Segment &S : Src.segments) {
801 if (S.valno != SrcValNo)
802 continue;
803 // This is adding a segment from Src that ends in a copy that is about
804 // to be removed. This segment is going to be merged with a pre-existing
805 // segment in Dst. This works, except in cases when the corresponding
806 // segment in Dst is dead. For example: adding [192r,208r:1) from Src
807 // to [208r,208d:1) in Dst would create [192r,208d:1) in Dst.
808 // Recognized such cases, so that the segments can be shrunk.
809 LiveRange::Segment Added = LiveRange::Segment(S.start, S.end, DstValNo);
810 LiveRange::Segment &Merged = *Dst.addSegment(Added);
811 if (Merged.end.isDead())
812 MergedWithDead = true;
813 Changed = true;
814 }
815 return std::make_pair(Changed, MergedWithDead);
816}
817
818std::pair<bool, bool>
819RegisterCoalescer::removeCopyByCommutingDef(const CoalescerPair &CP,
820 MachineInstr *CopyMI) {
821 assert(!CP.isPhys());
822
823 LiveInterval &IntA =
824 LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
825 LiveInterval &IntB =
826 LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
827
828 // We found a non-trivially-coalescable copy with IntA being the source and
829 // IntB being the dest, thus this defines a value number in IntB. If the
830 // source value number (in IntA) is defined by a commutable instruction and
831 // its other operand is coalesced to the copy dest register, see if we can
832 // transform the copy into a noop by commuting the definition. For example,
833 //
834 // A3 = op A2 killed B0
835 // ...
836 // B1 = A3 <- this copy
837 // ...
838 // = op A3 <- more uses
839 //
840 // ==>
841 //
842 // B2 = op B0 killed A2
843 // ...
844 // B1 = B2 <- now an identity copy
845 // ...
846 // = op B2 <- more uses
847
848 // BValNo is a value number in B that is defined by a copy from A. 'B1' in
849 // the example above.
850 SlotIndex CopyIdx = LIS->getInstructionIndex(*CopyMI).getRegSlot();
851 VNInfo *BValNo = IntB.getVNInfoAt(CopyIdx);
852 assert(BValNo != nullptr && BValNo->def == CopyIdx);
853
854 // AValNo is the value number in A that defines the copy, A3 in the example.
855 VNInfo *AValNo = IntA.getVNInfoAt(CopyIdx.getRegSlot(true));
856 assert(AValNo && !AValNo->isUnused() && "COPY source not live");
857 if (AValNo->isPHIDef())
858 return {false, false};
860 if (!DefMI)
861 return {false, false};
862 if (!DefMI->isCommutable())
863 return {false, false};
864 // If DefMI is a two-address instruction then commuting it will change the
865 // destination register.
866 int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg(), /*TRI=*/nullptr);
867 assert(DefIdx != -1);
868 unsigned UseOpIdx;
869 if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
870 return {false, false};
871
872 // FIXME: The code below tries to commute 'UseOpIdx' operand with some other
873 // commutable operand which is expressed by 'CommuteAnyOperandIndex'value
874 // passed to the method. That _other_ operand is chosen by
875 // the findCommutedOpIndices() method.
876 //
877 // That is obviously an area for improvement in case of instructions having
878 // more than 2 operands. For example, if some instruction has 3 commutable
879 // operands then all possible variants (i.e. op#1<->op#2, op#1<->op#3,
880 // op#2<->op#3) of commute transformation should be considered/tried here.
881 unsigned NewDstIdx = TargetInstrInfo::CommuteAnyOperandIndex;
882 if (!TII->findCommutedOpIndices(*DefMI, UseOpIdx, NewDstIdx))
883 return {false, false};
884
885 MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
886 Register NewReg = NewDstMO.getReg();
887 if (NewReg != IntB.reg() || !IntB.Query(AValNo->def).isKill())
888 return {false, false};
889
890 // Make sure there are no other definitions of IntB that would reach the
891 // uses which the new definition can reach.
892 if (hasOtherReachingDefs(IntA, IntB, AValNo, BValNo))
893 return {false, false};
894
895 // If some of the uses of IntA.reg is already coalesced away, return false.
896 // It's not possible to determine whether it's safe to perform the coalescing.
897 for (MachineOperand &MO : MRI->use_nodbg_operands(IntA.reg())) {
898 MachineInstr *UseMI = MO.getParent();
899 unsigned OpNo = &MO - &UseMI->getOperand(0);
900 SlotIndex UseIdx = LIS->getInstructionIndex(*UseMI);
902 if (US == IntA.end() || US->valno != AValNo)
903 continue;
904 // If this use is tied to a def, we can't rewrite the register.
905 if (UseMI->isRegTiedToDefOperand(OpNo))
906 return {false, false};
907 }
908
909 LLVM_DEBUG(dbgs() << "\tremoveCopyByCommutingDef: " << AValNo->def << '\t'
910 << *DefMI);
911
912 // At this point we have decided that it is legal to do this
913 // transformation. Start by commuting the instruction.
915 MachineInstr *NewMI =
916 TII->commuteInstruction(*DefMI, false, UseOpIdx, NewDstIdx);
917 if (!NewMI)
918 return {false, false};
919 if (IntA.reg().isVirtual() && IntB.reg().isVirtual() &&
920 !MRI->constrainRegClass(IntB.reg(), MRI->getRegClass(IntA.reg())))
921 return {false, false};
922 if (NewMI != DefMI) {
923 LIS->ReplaceMachineInstrInMaps(*DefMI, *NewMI);
925 MBB->insert(Pos, NewMI);
926 MBB->erase(DefMI);
927 }
928
929 // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
930 // A = or A, B
931 // ...
932 // B = A
933 // ...
934 // C = killed A
935 // ...
936 // = B
937
938 // Update uses of IntA of the specific Val# with IntB.
939 for (MachineOperand &UseMO :
940 llvm::make_early_inc_range(MRI->use_operands(IntA.reg()))) {
941 if (UseMO.isUndef())
942 continue;
943 MachineInstr *UseMI = UseMO.getParent();
944 if (UseMI->isDebugInstr()) {
945 // FIXME These don't have an instruction index. Not clear we have enough
946 // info to decide whether to do this replacement or not. For now do it.
947 UseMO.setReg(NewReg);
948 continue;
949 }
950 SlotIndex UseIdx = LIS->getInstructionIndex(*UseMI).getRegSlot(true);
952 assert(US != IntA.end() && "Use must be live");
953 if (US->valno != AValNo)
954 continue;
955 // Kill flags are no longer accurate. They are recomputed after RA.
956 UseMO.setIsKill(false);
957 if (NewReg.isPhysical())
958 UseMO.substPhysReg(NewReg, *TRI);
959 else
960 UseMO.setReg(NewReg);
961 if (UseMI == CopyMI)
962 continue;
963 if (!UseMI->isCopy())
964 continue;
965 if (UseMI->getOperand(0).getReg() != IntB.reg() ||
967 continue;
968
969 // This copy will become a noop. If it's defining a new val#, merge it into
970 // BValNo.
971 SlotIndex DefIdx = UseIdx.getRegSlot();
972 VNInfo *DVNI = IntB.getVNInfoAt(DefIdx);
973 if (!DVNI)
974 continue;
975 LLVM_DEBUG(dbgs() << "\t\tnoop: " << DefIdx << '\t' << *UseMI);
976 assert(DVNI->def == DefIdx);
977 BValNo = IntB.MergeValueNumberInto(DVNI, BValNo);
978 for (LiveInterval::SubRange &S : IntB.subranges()) {
979 VNInfo *SubDVNI = S.getVNInfoAt(DefIdx);
980 if (!SubDVNI)
981 continue;
982 VNInfo *SubBValNo = S.getVNInfoAt(CopyIdx);
983 assert(SubBValNo->def == CopyIdx);
984 S.MergeValueNumberInto(SubDVNI, SubBValNo);
985 }
986
987 deleteInstr(UseMI);
988 }
989
990 // Extend BValNo by merging in IntA live segments of AValNo. Val# definition
991 // is updated.
992 bool ShrinkB = false;
994 if (IntA.hasSubRanges() || IntB.hasSubRanges()) {
995 if (!IntA.hasSubRanges()) {
996 LaneBitmask Mask = MRI->getMaxLaneMaskForVReg(IntA.reg());
997 IntA.createSubRangeFrom(Allocator, Mask, IntA);
998 } else if (!IntB.hasSubRanges()) {
999 LaneBitmask Mask = MRI->getMaxLaneMaskForVReg(IntB.reg());
1000 IntB.createSubRangeFrom(Allocator, Mask, IntB);
1001 }
1002 SlotIndex AIdx = CopyIdx.getRegSlot(true);
1003 LaneBitmask MaskA;
1004 const SlotIndexes &Indexes = *LIS->getSlotIndexes();
1005 for (LiveInterval::SubRange &SA : IntA.subranges()) {
1006 VNInfo *ASubValNo = SA.getVNInfoAt(AIdx);
1007 // Even if we are dealing with a full copy, some lanes can
1008 // still be undefined.
1009 // E.g.,
1010 // undef A.subLow = ...
1011 // B = COPY A <== A.subHigh is undefined here and does
1012 // not have a value number.
1013 if (!ASubValNo)
1014 continue;
1015 MaskA |= SA.LaneMask;
1016
1017 IntB.refineSubRanges(
1018 Allocator, SA.LaneMask,
1019 [&Allocator, &SA, CopyIdx, ASubValNo,
1020 &ShrinkB](LiveInterval::SubRange &SR) {
1021 VNInfo *BSubValNo = SR.empty() ? SR.getNextValue(CopyIdx, Allocator)
1022 : SR.getVNInfoAt(CopyIdx);
1023 assert(BSubValNo != nullptr);
1024 auto P = addSegmentsWithValNo(SR, BSubValNo, SA, ASubValNo);
1025 ShrinkB |= P.second;
1026 if (P.first)
1027 BSubValNo->def = ASubValNo->def;
1028 },
1029 Indexes, *TRI);
1030 }
1031 // Go over all subranges of IntB that have not been covered by IntA,
1032 // and delete the segments starting at CopyIdx. This can happen if
1033 // IntA has undef lanes that are defined in IntB.
1034 for (LiveInterval::SubRange &SB : IntB.subranges()) {
1035 if ((SB.LaneMask & MaskA).any())
1036 continue;
1037 if (LiveRange::Segment *S = SB.getSegmentContaining(CopyIdx))
1038 if (S->start.getBaseIndex() == CopyIdx.getBaseIndex())
1039 SB.removeSegment(*S, true);
1040 }
1041 }
1042
1043 BValNo->def = AValNo->def;
1044 auto P = addSegmentsWithValNo(IntB, BValNo, IntA, AValNo);
1045 ShrinkB |= P.second;
1046 LLVM_DEBUG(dbgs() << "\t\textended: " << IntB << '\n');
1047
1048 LIS->removeVRegDefAt(IntA, AValNo->def);
1049
1050 LLVM_DEBUG(dbgs() << "\t\ttrimmed: " << IntA << '\n');
1051 ++numCommutes;
1052 return {true, ShrinkB};
1053}
1054
1055/// For copy B = A in BB2, if A is defined by A = B in BB0 which is a
1056/// predecessor of BB2, and if B is not redefined on the way from A = B
1057/// in BB0 to B = A in BB2, B = A in BB2 is partially redundant if the
1058/// execution goes through the path from BB0 to BB2. We may move B = A
1059/// to the predecessor without such reversed copy.
1060/// So we will transform the program from:
1061/// BB0:
1062/// A = B; BB1:
1063/// ... ...
1064/// / \ /
1065/// BB2:
1066/// ...
1067/// B = A;
1068///
1069/// to:
1070///
1071/// BB0: BB1:
1072/// A = B; ...
1073/// ... B = A;
1074/// / \ /
1075/// BB2:
1076/// ...
1077///
1078/// A special case is when BB0 and BB2 are the same BB which is the only
1079/// BB in a loop:
1080/// BB1:
1081/// ...
1082/// BB0/BB2: ----
1083/// B = A; |
1084/// ... |
1085/// A = B; |
1086/// |-------
1087/// |
1088/// We may hoist B = A from BB0/BB2 to BB1.
1089///
1090/// The major preconditions for correctness to remove such partial
1091/// redundancy include:
1092/// 1. A in B = A in BB2 is defined by a PHI in BB2, and one operand of
1093/// the PHI is defined by the reversed copy A = B in BB0.
1094/// 2. No B is referenced from the start of BB2 to B = A.
1095/// 3. No B is defined from A = B to the end of BB0.
1096/// 4. BB1 has only one successor.
1097///
1098/// 2 and 4 implicitly ensure B is not live at the end of BB1.
1099/// 4 guarantees BB2 is hotter than BB1, so we can only move a copy to a
1100/// colder place, which not only prevent endless loop, but also make sure
1101/// the movement of copy is beneficial.
1102bool RegisterCoalescer::removePartialRedundancy(const CoalescerPair &CP,
1103 MachineInstr &CopyMI) {
1104 assert(!CP.isPhys());
1105 if (!CopyMI.isFullCopy())
1106 return false;
1107
1108 MachineBasicBlock &MBB = *CopyMI.getParent();
1109 // If this block is the target of an invoke/inlineasm_br, moving the copy into
1110 // the predecessor is tricker, and we don't handle it.
1112 return false;
1113
1114 if (MBB.pred_size() != 2)
1115 return false;
1116
1117 LiveInterval &IntA =
1118 LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
1119 LiveInterval &IntB =
1120 LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
1121
1122 // A is defined by PHI at the entry of MBB.
1123 SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot(true);
1124 VNInfo *AValNo = IntA.getVNInfoAt(CopyIdx);
1125 assert(AValNo && !AValNo->isUnused() && "COPY source not live");
1126 if (!AValNo->isPHIDef())
1127 return false;
1128
1129 // No B is referenced before CopyMI in MBB.
1130 if (IntB.overlaps(LIS->getMBBStartIdx(&MBB), CopyIdx))
1131 return false;
1132
1133 // MBB has two predecessors: one contains A = B so no copy will be inserted
1134 // for it. The other one will have a copy moved from MBB.
1135 bool FoundReverseCopy = false;
1136 MachineBasicBlock *CopyLeftBB = nullptr;
1137 for (MachineBasicBlock *Pred : MBB.predecessors()) {
1138 VNInfo *PVal = IntA.getVNInfoBefore(LIS->getMBBEndIdx(Pred));
1140 if (!DefMI || !DefMI->isFullCopy()) {
1141 CopyLeftBB = Pred;
1142 continue;
1143 }
1144 // Check DefMI is a reverse copy and it is in BB Pred.
1145 if (DefMI->getOperand(0).getReg() != IntA.reg() ||
1146 DefMI->getOperand(1).getReg() != IntB.reg() ||
1147 DefMI->getParent() != Pred) {
1148 CopyLeftBB = Pred;
1149 continue;
1150 }
1151 // If there is any other def of B after DefMI and before the end of Pred,
1152 // we need to keep the copy of B = A at the end of Pred if we remove
1153 // B = A from MBB.
1154 bool ValB_Changed = false;
1155 for (auto *VNI : IntB.valnos) {
1156 if (VNI->isUnused())
1157 continue;
1158 if (PVal->def < VNI->def && VNI->def < LIS->getMBBEndIdx(Pred)) {
1159 ValB_Changed = true;
1160 break;
1161 }
1162 }
1163 if (ValB_Changed) {
1164 CopyLeftBB = Pred;
1165 continue;
1166 }
1167 FoundReverseCopy = true;
1168 }
1169
1170 // If no reverse copy is found in predecessors, nothing to do.
1171 if (!FoundReverseCopy)
1172 return false;
1173
1174 // If CopyLeftBB is nullptr, it means every predecessor of MBB contains
1175 // reverse copy, CopyMI can be removed trivially if only IntA/IntB is updated.
1176 // If CopyLeftBB is not nullptr, move CopyMI from MBB to CopyLeftBB and
1177 // update IntA/IntB.
1178 //
1179 // If CopyLeftBB is not nullptr, ensure CopyLeftBB has a single succ so
1180 // MBB is hotter than CopyLeftBB.
1181 if (CopyLeftBB && CopyLeftBB->succ_size() > 1)
1182 return false;
1183
1184 // Now (almost sure it's) ok to move copy.
1185 if (CopyLeftBB) {
1186 // Position in CopyLeftBB where we should insert new copy.
1187 auto InsPos = CopyLeftBB->getFirstTerminator();
1188
1189 // Make sure that B isn't referenced in the terminators (if any) at the end
1190 // of the predecessor since we're about to insert a new definition of B
1191 // before them.
1192 if (InsPos != CopyLeftBB->end()) {
1193 SlotIndex InsPosIdx = LIS->getInstructionIndex(*InsPos).getRegSlot(true);
1194 if (IntB.overlaps(InsPosIdx, LIS->getMBBEndIdx(CopyLeftBB)))
1195 return false;
1196 }
1197
1198 LLVM_DEBUG(dbgs() << "\tremovePartialRedundancy: Move the copy to "
1199 << printMBBReference(*CopyLeftBB) << '\t' << CopyMI);
1200
1201 // Insert new copy to CopyLeftBB.
1202 MachineInstr *NewCopyMI = BuildMI(*CopyLeftBB, InsPos, CopyMI.getDebugLoc(),
1203 TII->get(TargetOpcode::COPY), IntB.reg())
1204 .addReg(IntA.reg());
1205 SlotIndex NewCopyIdx =
1206 LIS->InsertMachineInstrInMaps(*NewCopyMI).getRegSlot();
1207 IntB.createDeadDef(NewCopyIdx, LIS->getVNInfoAllocator());
1208 for (LiveInterval::SubRange &SR : IntB.subranges())
1209 SR.createDeadDef(NewCopyIdx, LIS->getVNInfoAllocator());
1210
1211 // If the newly created Instruction has an address of an instruction that
1212 // was deleted before (object recycled by the allocator) it needs to be
1213 // removed from the deleted list.
1214 ErasedInstrs.erase(NewCopyMI);
1215 } else {
1216 LLVM_DEBUG(dbgs() << "\tremovePartialRedundancy: Remove the copy from "
1217 << printMBBReference(MBB) << '\t' << CopyMI);
1218 }
1219
1220 const bool IsUndefCopy = CopyMI.getOperand(1).isUndef();
1221
1222 // Remove CopyMI.
1223 // Note: This is fine to remove the copy before updating the live-ranges.
1224 // While updating the live-ranges, we only look at slot indices and
1225 // never go back to the instruction.
1226 // Mark instructions as deleted.
1227 deleteInstr(&CopyMI);
1228
1229 // Update the liveness.
1230 SmallVector<SlotIndex, 8> EndPoints;
1231 VNInfo *BValNo = IntB.Query(CopyIdx).valueOutOrDead();
1232 LIS->pruneValue(*static_cast<LiveRange *>(&IntB), CopyIdx.getRegSlot(),
1233 &EndPoints);
1234 BValNo->markUnused();
1235
1236 if (IsUndefCopy) {
1237 // We're introducing an undef phi def, and need to set undef on any users of
1238 // the previously local def to avoid artifically extending the lifetime
1239 // through the block.
1240 for (MachineOperand &MO : MRI->use_nodbg_operands(IntB.reg())) {
1241 const MachineInstr &MI = *MO.getParent();
1242 SlotIndex UseIdx = LIS->getInstructionIndex(MI);
1243 if (!IntB.liveAt(UseIdx))
1244 MO.setIsUndef(true);
1245 }
1246 }
1247
1248 // Extend IntB to the EndPoints of its original live interval.
1249 LIS->extendToIndices(IntB, EndPoints);
1250
1251 // Now, do the same for its subranges.
1252 for (LiveInterval::SubRange &SR : IntB.subranges()) {
1253 EndPoints.clear();
1254 VNInfo *BValNo = SR.Query(CopyIdx).valueOutOrDead();
1255 assert(BValNo && "All sublanes should be live");
1256 LIS->pruneValue(SR, CopyIdx.getRegSlot(), &EndPoints);
1257 BValNo->markUnused();
1258 // We can have a situation where the result of the original copy is live,
1259 // but is immediately dead in this subrange, e.g. [336r,336d:0). That makes
1260 // the copy appear as an endpoint from pruneValue(), but we don't want it
1261 // to because the copy has been removed. We can go ahead and remove that
1262 // endpoint; there is no other situation here that there could be a use at
1263 // the same place as we know that the copy is a full copy.
1264 for (unsigned I = 0; I != EndPoints.size();) {
1265 if (SlotIndex::isSameInstr(EndPoints[I], CopyIdx)) {
1266 EndPoints[I] = EndPoints.back();
1267 EndPoints.pop_back();
1268 continue;
1269 }
1270 ++I;
1271 }
1273 IntB.computeSubRangeUndefs(Undefs, SR.LaneMask, *MRI,
1274 *LIS->getSlotIndexes());
1275 LIS->extendToIndices(SR, EndPoints, Undefs);
1276 }
1277 // If any dead defs were extended, truncate them.
1278 shrinkToUses(&IntB);
1279
1280 // Finally, update the live-range of IntA.
1281 shrinkToUses(&IntA);
1282 return true;
1283}
1284
1285/// Returns true if @p MI defines the full vreg @p Reg, as opposed to just
1286/// defining a subregister.
1288 assert(!Reg.isPhysical() && "This code cannot handle physreg aliasing");
1289
1290 for (const MachineOperand &Op : MI.all_defs()) {
1291 if (Op.getReg() != Reg)
1292 continue;
1293 // Return true if we define the full register or don't care about the value
1294 // inside other subregisters.
1295 if (Op.getSubReg() == 0 || Op.isUndef())
1296 return true;
1297 }
1298 return false;
1299}
1300
1301bool RegisterCoalescer::reMaterializeDef(const CoalescerPair &CP,
1302 MachineInstr *CopyMI,
1303 bool &IsDefCopy) {
1304 IsDefCopy = false;
1305 Register SrcReg = CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg();
1306 unsigned SrcIdx = CP.isFlipped() ? CP.getDstIdx() : CP.getSrcIdx();
1307 Register DstReg = CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg();
1308 unsigned DstIdx = CP.isFlipped() ? CP.getSrcIdx() : CP.getDstIdx();
1309 if (SrcReg.isPhysical())
1310 return false;
1311
1312 LiveInterval &SrcInt = LIS->getInterval(SrcReg);
1313 SlotIndex CopyIdx = LIS->getInstructionIndex(*CopyMI);
1314 VNInfo *ValNo = SrcInt.Query(CopyIdx).valueIn();
1315 if (!ValNo)
1316 return false;
1317 if (ValNo->isPHIDef() || ValNo->isUnused())
1318 return false;
1320 if (!DefMI)
1321 return false;
1322 if (DefMI->isCopyLike()) {
1323 IsDefCopy = true;
1324 return false;
1325 }
1326 if (!TII->isAsCheapAsAMove(*DefMI))
1327 return false;
1328
1329 if (!TII->isReMaterializable(*DefMI))
1330 return false;
1331
1332 if (!definesFullReg(*DefMI, SrcReg))
1333 return false;
1334 bool SawStore = false;
1335 if (!DefMI->isSafeToMove(SawStore))
1336 return false;
1337 const MCInstrDesc &MCID = DefMI->getDesc();
1338 if (MCID.getNumDefs() != 1)
1339 return false;
1340
1341 // If both SrcIdx and DstIdx are set, correct rematerialization would widen
1342 // the register substantially (beyond both source and dest size). This is bad
1343 // for performance since it can cascade through a function, introducing many
1344 // extra spills and fills (e.g. ARM can easily end up copying QQQQPR registers
1345 // around after a few subreg copies).
1346 if (SrcIdx && DstIdx)
1347 return false;
1348
1349 // Only support subregister destinations when the def is read-undef.
1350 MachineOperand &DstOperand = CopyMI->getOperand(0);
1351 Register CopyDstReg = DstOperand.getReg();
1352 if (DstOperand.getSubReg() && !DstOperand.isUndef())
1353 return false;
1354
1355 // In the physical register case, checking that the def is read-undef is not
1356 // enough. We're widening the def and need to avoid clobbering other live
1357 // values in the unused register pieces.
1358 //
1359 // TODO: Targets may support rewriting the rematerialized instruction to only
1360 // touch relevant lanes, in which case we don't need any liveness check.
1361 if (CopyDstReg.isPhysical() && CP.isPartial()) {
1362 for (MCRegUnit Unit : TRI->regunits(DstReg)) {
1363 // Ignore the register units we are writing anyway.
1364 if (is_contained(TRI->regunits(CopyDstReg), Unit))
1365 continue;
1366
1367 // Check if the other lanes we are defining are live at the
1368 // rematerialization point.
1369 LiveRange &LR = LIS->getRegUnit(Unit);
1370 if (LR.liveAt(CopyIdx))
1371 return false;
1372 }
1373 }
1374
1375 const unsigned DefSubIdx = DefMI->getOperand(0).getSubReg();
1376 const TargetRegisterClass *DefRC = TII->getRegClass(MCID, 0, TRI);
1377 if (!DefMI->isImplicitDef()) {
1378 if (DstReg.isPhysical()) {
1379 Register NewDstReg = DstReg;
1380
1381 unsigned NewDstIdx = TRI->composeSubRegIndices(CP.getSrcIdx(), DefSubIdx);
1382 if (NewDstIdx)
1383 NewDstReg = TRI->getSubReg(DstReg, NewDstIdx);
1384
1385 // Finally, make sure that the physical subregister that will be
1386 // constructed later is permitted for the instruction.
1387 if (!DefRC->contains(NewDstReg))
1388 return false;
1389 } else {
1390 // Theoretically, some stack frame reference could exist. Just make sure
1391 // it hasn't actually happened.
1392 assert(DstReg.isVirtual() &&
1393 "Only expect to deal with virtual or physical registers");
1394 }
1395 }
1396
1397 if (!VirtRegAuxInfo::allUsesAvailableAt(DefMI, CopyIdx, *LIS, *MRI, *TII))
1398 return false;
1399
1400 DebugLoc DL = CopyMI->getDebugLoc();
1401 MachineBasicBlock *MBB = CopyMI->getParent();
1403 std::next(MachineBasicBlock::iterator(CopyMI));
1404 LiveRangeEdit::Remat RM(ValNo);
1405 RM.OrigMI = DefMI;
1407 LiveRangeEdit Edit(&SrcInt, NewRegs, *MF, *LIS, nullptr, this);
1408 Edit.rematerializeAt(*MBB, MII, DstReg, RM, *TRI, false, SrcIdx, CopyMI);
1409 MachineInstr &NewMI = *std::prev(MII);
1410 NewMI.setDebugLoc(DL);
1411
1412 // In a situation like the following:
1413 // %0:subreg = instr ; DefMI, subreg = DstIdx
1414 // %1 = copy %0:subreg ; CopyMI, SrcIdx = 0
1415 // instead of widening %1 to the register class of %0 simply do:
1416 // %1 = instr
1417 const TargetRegisterClass *NewRC = CP.getNewRC();
1418 if (DstIdx != 0) {
1419 MachineOperand &DefMO = NewMI.getOperand(0);
1420 if (DefMO.getSubReg() == DstIdx) {
1421 assert(SrcIdx == 0 && CP.isFlipped() &&
1422 "Shouldn't have SrcIdx+DstIdx at this point");
1423 const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg);
1424 const TargetRegisterClass *CommonRC =
1425 TRI->getCommonSubClass(DefRC, DstRC);
1426 if (CommonRC != nullptr) {
1427 NewRC = CommonRC;
1428
1429 // Instruction might contain "undef %0:subreg" as use operand:
1430 // %0:subreg = instr op_1, ..., op_N, undef %0:subreg, op_N+2, ...
1431 //
1432 // Need to check all operands.
1433 for (MachineOperand &MO : NewMI.operands()) {
1434 if (MO.isReg() && MO.getReg() == DstReg && MO.getSubReg() == DstIdx) {
1435 MO.setSubReg(0);
1436 }
1437 }
1438
1439 DstIdx = 0;
1440 DefMO.setIsUndef(false); // Only subregs can have def+undef.
1441 }
1442 }
1443 }
1444
1445 // CopyMI may have implicit operands, save them so that we can transfer them
1446 // over to the newly materialized instruction after CopyMI is removed.
1448 ImplicitOps.reserve(CopyMI->getNumOperands() -
1449 CopyMI->getDesc().getNumOperands());
1450 for (unsigned I = CopyMI->getDesc().getNumOperands(),
1451 E = CopyMI->getNumOperands();
1452 I != E; ++I) {
1453 MachineOperand &MO = CopyMI->getOperand(I);
1454 if (MO.isReg()) {
1455 assert(MO.isImplicit() &&
1456 "No explicit operands after implicit operands.");
1457 assert((MO.getReg().isPhysical() ||
1458 (MO.getSubReg() == 0 && MO.getReg() == DstOperand.getReg())) &&
1459 "unexpected implicit virtual register def");
1460 ImplicitOps.push_back(MO);
1461 }
1462 }
1463
1464 CopyMI->eraseFromParent();
1465 ErasedInstrs.insert(CopyMI);
1466
1467 // NewMI may have dead implicit defs (E.g. EFLAGS for MOV<bits>r0 on X86).
1468 // We need to remember these so we can add intervals once we insert
1469 // NewMI into SlotIndexes.
1470 //
1471 // We also expect to have tied implicit-defs of super registers originating
1472 // from SUBREG_TO_REG, such as:
1473 // $edi = MOV32r0 implicit-def dead $eflags, implicit-def $rdi
1474 // undef %0.sub_32bit = MOV32r0 implicit-def dead $eflags, implicit-def %0
1475 //
1476 // The implicit-def of the super register may have been reduced to
1477 // subregisters depending on the uses.
1479 for (unsigned i = NewMI.getDesc().getNumOperands(),
1480 e = NewMI.getNumOperands();
1481 i != e; ++i) {
1482 MachineOperand &MO = NewMI.getOperand(i);
1483 if (MO.isReg() && MO.isDef()) {
1484 assert(MO.isImplicit());
1485 if (MO.getReg().isPhysical()) {
1486 assert(MO.isImplicit() && MO.getReg().isPhysical() &&
1487 (MO.isDead() ||
1488 (DefSubIdx &&
1489 ((TRI->getSubReg(MO.getReg(), DefSubIdx) ==
1490 MCRegister((unsigned)NewMI.getOperand(0).getReg())) ||
1491 TRI->isSubRegisterEq(NewMI.getOperand(0).getReg(),
1492 MO.getReg())))));
1493 NewMIImplDefs.push_back({i, MO.getReg()});
1494 } else {
1495 assert(MO.getReg() == NewMI.getOperand(0).getReg());
1496
1497 // We're only expecting another def of the main output, so the range
1498 // should get updated with the regular output range.
1499 //
1500 // FIXME: The range updating below probably needs updating to look at
1501 // the super register if subranges are tracked.
1502 assert(!MRI->shouldTrackSubRegLiveness(DstReg) &&
1503 "subrange update for implicit-def of super register may not be "
1504 "properly handled");
1505 }
1506 }
1507 }
1508
1509 if (DstReg.isVirtual()) {
1510 unsigned NewIdx = NewMI.getOperand(0).getSubReg();
1511
1512 if (DefRC != nullptr) {
1513 if (NewIdx)
1514 NewRC = TRI->getMatchingSuperRegClass(NewRC, DefRC, NewIdx);
1515 else
1516 NewRC = TRI->getCommonSubClass(NewRC, DefRC);
1517 assert(NewRC && "subreg chosen for remat incompatible with instruction");
1518 }
1519
1520 // Remap subranges to new lanemask and change register class.
1521 LiveInterval &DstInt = LIS->getInterval(DstReg);
1522 for (LiveInterval::SubRange &SR : DstInt.subranges()) {
1523 SR.LaneMask = TRI->composeSubRegIndexLaneMask(DstIdx, SR.LaneMask);
1524 }
1525 MRI->setRegClass(DstReg, NewRC);
1526
1527 // Update machine operands and add flags.
1528 updateRegDefsUses(DstReg, DstReg, DstIdx);
1529 NewMI.getOperand(0).setSubReg(NewIdx);
1530 // updateRegDefUses can add an "undef" flag to the definition, since
1531 // it will replace DstReg with DstReg.DstIdx. If NewIdx is 0, make
1532 // sure that "undef" is not set.
1533 if (NewIdx == 0)
1534 NewMI.getOperand(0).setIsUndef(false);
1535
1536 // In a situation like the following:
1537 //
1538 // undef %2.subreg:reg = INST %1:reg ; DefMI (rematerializable),
1539 // ; Defines only some of lanes,
1540 // ; so DefSubIdx = NewIdx = subreg
1541 // %3:reg = COPY %2 ; Copy full reg
1542 // .... = SOMEINSTR %3:reg ; Use full reg
1543 //
1544 // there are no subranges for %3 so after rematerialization we need
1545 // to explicitly create them. Undefined subranges are removed later on.
1546 if (NewIdx && !DstInt.hasSubRanges() &&
1547 MRI->shouldTrackSubRegLiveness(DstReg)) {
1548 LaneBitmask FullMask = MRI->getMaxLaneMaskForVReg(DstReg);
1549 LaneBitmask UsedLanes = TRI->getSubRegIndexLaneMask(NewIdx);
1550 LaneBitmask UnusedLanes = FullMask & ~UsedLanes;
1552 DstInt.createSubRangeFrom(Alloc, UsedLanes, DstInt);
1553 DstInt.createSubRangeFrom(Alloc, UnusedLanes, DstInt);
1554 }
1555
1556 // Add dead subregister definitions if we are defining the whole register
1557 // but only part of it is live.
1558 // This could happen if the rematerialization instruction is rematerializing
1559 // more than actually is used in the register.
1560 // An example would be:
1561 // %1 = LOAD CONSTANTS 5, 8 ; Loading both 5 and 8 in different subregs
1562 // ; Copying only part of the register here, but the rest is undef.
1563 // %2:sub_16bit<def, read-undef> = COPY %1:sub_16bit
1564 // ==>
1565 // ; Materialize all the constants but only using one
1566 // %2 = LOAD_CONSTANTS 5, 8
1567 //
1568 // at this point for the part that wasn't defined before we could have
1569 // subranges missing the definition.
1570 if (NewIdx == 0 && DstInt.hasSubRanges()) {
1571 SlotIndex CurrIdx = LIS->getInstructionIndex(NewMI);
1572 SlotIndex DefIndex =
1573 CurrIdx.getRegSlot(NewMI.getOperand(0).isEarlyClobber());
1574 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(DstReg);
1576 for (LiveInterval::SubRange &SR : DstInt.subranges()) {
1577 if (!SR.liveAt(DefIndex))
1578 SR.createDeadDef(DefIndex, Alloc);
1579 MaxMask &= ~SR.LaneMask;
1580 }
1581 if (MaxMask.any()) {
1582 LiveInterval::SubRange *SR = DstInt.createSubRange(Alloc, MaxMask);
1583 SR->createDeadDef(DefIndex, Alloc);
1584 }
1585 }
1586
1587 // Make sure that the subrange for resultant undef is removed
1588 // For example:
1589 // %1:sub1<def,read-undef> = LOAD CONSTANT 1
1590 // %2 = COPY %1
1591 // ==>
1592 // %2:sub1<def, read-undef> = LOAD CONSTANT 1
1593 // ; Correct but need to remove the subrange for %2:sub0
1594 // ; as it is now undef
1595 if (NewIdx != 0 && DstInt.hasSubRanges()) {
1596 // The affected subregister segments can be removed.
1597 SlotIndex CurrIdx = LIS->getInstructionIndex(NewMI);
1598 LaneBitmask DstMask = TRI->getSubRegIndexLaneMask(NewIdx);
1599 bool UpdatedSubRanges = false;
1600 SlotIndex DefIndex =
1601 CurrIdx.getRegSlot(NewMI.getOperand(0).isEarlyClobber());
1603 for (LiveInterval::SubRange &SR : DstInt.subranges()) {
1604 if ((SR.LaneMask & DstMask).none()) {
1606 << "Removing undefined SubRange "
1607 << PrintLaneMask(SR.LaneMask) << " : " << SR << "\n");
1608
1609 if (VNInfo *RmValNo = SR.getVNInfoAt(CurrIdx.getRegSlot())) {
1610 // VNI is in ValNo - remove any segments in this SubRange that have
1611 // this ValNo
1612 SR.removeValNo(RmValNo);
1613 }
1614
1615 // We may not have a defined value at this point, but still need to
1616 // clear out any empty subranges tentatively created by
1617 // updateRegDefUses. The original subrange def may have only undefed
1618 // some lanes.
1619 UpdatedSubRanges = true;
1620 } else {
1621 // We know that this lane is defined by this instruction,
1622 // but at this point it might not be live because it was not defined
1623 // by the original instruction. This happens when the
1624 // rematerialization widens the defined register. Assign that lane a
1625 // dead def so that the interferences are properly modeled.
1626 if (!SR.liveAt(DefIndex))
1627 SR.createDeadDef(DefIndex, Alloc);
1628 }
1629 }
1630 if (UpdatedSubRanges)
1631 DstInt.removeEmptySubRanges();
1632 }
1633 } else if (NewMI.getOperand(0).getReg() != CopyDstReg) {
1634 // The New instruction may be defining a sub-register of what's actually
1635 // been asked for. If so it must implicitly define the whole thing.
1636 assert(DstReg.isPhysical() &&
1637 "Only expect virtual or physical registers in remat");
1638
1639 // When we're rematerializing into a not-quite-right register we already add
1640 // the real definition as an implicit-def, but we should also be marking the
1641 // "official" register as dead, since nothing else is going to use it as a
1642 // result of this remat. Not doing this can affect pressure tracking.
1643 NewMI.getOperand(0).setIsDead(true);
1644
1645 bool HasDefMatchingCopy = false;
1646 for (auto [OpIndex, Reg] : NewMIImplDefs) {
1647 if (Reg != DstReg)
1648 continue;
1649 // Also, if CopyDstReg is a sub-register of DstReg (and it is defined), we
1650 // must mark DstReg as dead since it is not going to used as a result of
1651 // this remat.
1652 if (DstReg != CopyDstReg)
1653 NewMI.getOperand(OpIndex).setIsDead(true);
1654 else
1655 HasDefMatchingCopy = true;
1656 }
1657
1658 // If NewMI does not already have an implicit-def CopyDstReg add one now.
1659 if (!HasDefMatchingCopy)
1661 CopyDstReg, true /*IsDef*/, true /*IsImp*/, false /*IsKill*/));
1662
1663 // Record small dead def live-ranges for all the subregisters
1664 // of the destination register.
1665 // Otherwise, variables that live through may miss some
1666 // interferences, thus creating invalid allocation.
1667 // E.g., i386 code:
1668 // %1 = somedef ; %1 GR8
1669 // %2 = remat ; %2 GR32
1670 // CL = COPY %2.sub_8bit
1671 // = somedef %1 ; %1 GR8
1672 // =>
1673 // %1 = somedef ; %1 GR8
1674 // dead ECX = remat ; implicit-def CL
1675 // = somedef %1 ; %1 GR8
1676 // %1 will see the interferences with CL but not with CH since
1677 // no live-ranges would have been created for ECX.
1678 // Fix that!
1679 SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI);
1680 for (MCRegUnit Unit : TRI->regunits(NewMI.getOperand(0).getReg()))
1681 if (LiveRange *LR = LIS->getCachedRegUnit(Unit))
1682 LR->createDeadDef(NewMIIdx.getRegSlot(), LIS->getVNInfoAllocator());
1683 }
1684
1685 NewMI.setRegisterDefReadUndef(NewMI.getOperand(0).getReg());
1686
1687 // Transfer over implicit operands to the rematerialized instruction.
1688 for (MachineOperand &MO : ImplicitOps)
1689 NewMI.addOperand(MO);
1690
1691 SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI);
1692 for (Register Reg : make_second_range(NewMIImplDefs)) {
1693 for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
1694 if (LiveRange *LR = LIS->getCachedRegUnit(Unit))
1695 LR->createDeadDef(NewMIIdx.getRegSlot(), LIS->getVNInfoAllocator());
1696 }
1697
1698 LLVM_DEBUG(dbgs() << "Remat: " << NewMI);
1699 ++NumReMats;
1700
1701 // If the virtual SrcReg is completely eliminated, update all DBG_VALUEs
1702 // to describe DstReg instead.
1703 if (MRI->use_nodbg_empty(SrcReg)) {
1704 for (MachineOperand &UseMO :
1705 llvm::make_early_inc_range(MRI->use_operands(SrcReg))) {
1706 MachineInstr *UseMI = UseMO.getParent();
1707 if (UseMI->isDebugInstr()) {
1708 if (DstReg.isPhysical())
1709 UseMO.substPhysReg(DstReg, *TRI);
1710 else
1711 UseMO.setReg(DstReg);
1712 // Move the debug value directly after the def of the rematerialized
1713 // value in DstReg.
1714 MBB->splice(std::next(NewMI.getIterator()), UseMI->getParent(), UseMI);
1715 LLVM_DEBUG(dbgs() << "\t\tupdated: " << *UseMI);
1716 }
1717 }
1718 }
1719
1720 if (ToBeUpdated.count(SrcReg))
1721 return true;
1722
1723 unsigned NumCopyUses = 0;
1724 for (MachineOperand &UseMO : MRI->use_nodbg_operands(SrcReg)) {
1725 if (UseMO.getParent()->isCopyLike())
1726 NumCopyUses++;
1727 }
1728 if (NumCopyUses < LateRematUpdateThreshold) {
1729 // The source interval can become smaller because we removed a use.
1730 shrinkToUses(&SrcInt, &DeadDefs);
1731 if (!DeadDefs.empty())
1732 eliminateDeadDefs(&Edit);
1733 } else {
1734 ToBeUpdated.insert(SrcReg);
1735 }
1736 return true;
1737}
1738
1739MachineInstr *RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI) {
1740 // ProcessImplicitDefs may leave some copies of <undef> values, it only
1741 // removes local variables. When we have a copy like:
1742 //
1743 // %1 = COPY undef %2
1744 //
1745 // We delete the copy and remove the corresponding value number from %1.
1746 // Any uses of that value number are marked as <undef>.
1747
1748 // Note that we do not query CoalescerPair here but redo isMoveInstr as the
1749 // CoalescerPair may have a new register class with adjusted subreg indices
1750 // at this point.
1751 Register SrcReg, DstReg;
1752 unsigned SrcSubIdx = 0, DstSubIdx = 0;
1753 if (!isMoveInstr(*TRI, CopyMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
1754 return nullptr;
1755
1756 SlotIndex Idx = LIS->getInstructionIndex(*CopyMI);
1757 const LiveInterval &SrcLI = LIS->getInterval(SrcReg);
1758 // CopyMI is undef iff SrcReg is not live before the instruction.
1759 if (SrcSubIdx != 0 && SrcLI.hasSubRanges()) {
1760 LaneBitmask SrcMask = TRI->getSubRegIndexLaneMask(SrcSubIdx);
1761 for (const LiveInterval::SubRange &SR : SrcLI.subranges()) {
1762 if ((SR.LaneMask & SrcMask).none())
1763 continue;
1764 if (SR.liveAt(Idx))
1765 return nullptr;
1766 }
1767 } else if (SrcLI.liveAt(Idx))
1768 return nullptr;
1769
1770 // If the undef copy defines a live-out value (i.e. an input to a PHI def),
1771 // then replace it with an IMPLICIT_DEF.
1772 LiveInterval &DstLI = LIS->getInterval(DstReg);
1773 SlotIndex RegIndex = Idx.getRegSlot();
1774 LiveRange::Segment *Seg = DstLI.getSegmentContaining(RegIndex);
1775 assert(Seg != nullptr && "No segment for defining instruction");
1776 VNInfo *V = DstLI.getVNInfoAt(Seg->end);
1777
1778 // The source interval may also have been on an undef use, in which case the
1779 // copy introduced a live value.
1780 if (((V && V->isPHIDef()) || (!V && !DstLI.liveAt(Idx)))) {
1781 for (unsigned i = CopyMI->getNumOperands(); i != 0; --i) {
1782 MachineOperand &MO = CopyMI->getOperand(i - 1);
1783 if (MO.isReg()) {
1784 if (MO.isUse())
1785 CopyMI->removeOperand(i - 1);
1786 } else {
1787 assert(MO.isImm() &&
1788 CopyMI->getOpcode() == TargetOpcode::SUBREG_TO_REG);
1789 CopyMI->removeOperand(i - 1);
1790 }
1791 }
1792
1793 CopyMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
1794 LLVM_DEBUG(dbgs() << "\tReplaced copy of <undef> value with an "
1795 "implicit def\n");
1796 return CopyMI;
1797 }
1798
1799 // Remove any DstReg segments starting at the instruction.
1800 LLVM_DEBUG(dbgs() << "\tEliminating copy of <undef> value\n");
1801
1802 // Remove value or merge with previous one in case of a subregister def.
1803 if (VNInfo *PrevVNI = DstLI.getVNInfoAt(Idx)) {
1804 VNInfo *VNI = DstLI.getVNInfoAt(RegIndex);
1805 DstLI.MergeValueNumberInto(VNI, PrevVNI);
1806
1807 // The affected subregister segments can be removed.
1808 LaneBitmask DstMask = TRI->getSubRegIndexLaneMask(DstSubIdx);
1809 for (LiveInterval::SubRange &SR : DstLI.subranges()) {
1810 if ((SR.LaneMask & DstMask).none())
1811 continue;
1812
1813 VNInfo *SVNI = SR.getVNInfoAt(RegIndex);
1814 assert(SVNI != nullptr && SlotIndex::isSameInstr(SVNI->def, RegIndex));
1815 SR.removeValNo(SVNI);
1816 }
1817 DstLI.removeEmptySubRanges();
1818 } else
1819 LIS->removeVRegDefAt(DstLI, RegIndex);
1820
1821 // Mark uses as undef.
1822 for (MachineOperand &MO : MRI->reg_nodbg_operands(DstReg)) {
1823 if (MO.isDef() /*|| MO.isUndef()*/)
1824 continue;
1825 const MachineInstr &MI = *MO.getParent();
1826 SlotIndex UseIdx = LIS->getInstructionIndex(MI);
1827 LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
1828 bool isLive;
1829 if (!UseMask.all() && DstLI.hasSubRanges()) {
1830 isLive = false;
1831 for (const LiveInterval::SubRange &SR : DstLI.subranges()) {
1832 if ((SR.LaneMask & UseMask).none())
1833 continue;
1834 if (SR.liveAt(UseIdx)) {
1835 isLive = true;
1836 break;
1837 }
1838 }
1839 } else
1840 isLive = DstLI.liveAt(UseIdx);
1841 if (isLive)
1842 continue;
1843 MO.setIsUndef(true);
1844 LLVM_DEBUG(dbgs() << "\tnew undef: " << UseIdx << '\t' << MI);
1845 }
1846
1847 // A def of a subregister may be a use of the other subregisters, so
1848 // deleting a def of a subregister may also remove uses. Since CopyMI
1849 // is still part of the function (but about to be erased), mark all
1850 // defs of DstReg in it as <undef>, so that shrinkToUses would
1851 // ignore them.
1852 for (MachineOperand &MO : CopyMI->all_defs())
1853 if (MO.getReg() == DstReg)
1854 MO.setIsUndef(true);
1855 LIS->shrinkToUses(&DstLI);
1856
1857 return CopyMI;
1858}
1859
1860void RegisterCoalescer::addUndefFlag(const LiveInterval &Int, SlotIndex UseIdx,
1861 MachineOperand &MO, unsigned SubRegIdx) {
1862 LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubRegIdx);
1863 if (MO.isDef())
1864 Mask = ~Mask;
1865 bool IsUndef = true;
1866 for (const LiveInterval::SubRange &S : Int.subranges()) {
1867 if ((S.LaneMask & Mask).none())
1868 continue;
1869 if (S.liveAt(UseIdx)) {
1870 IsUndef = false;
1871 break;
1872 }
1873 }
1874 if (IsUndef) {
1875 MO.setIsUndef(true);
1876 // We found out some subregister use is actually reading an undefined
1877 // value. In some cases the whole vreg has become undefined at this
1878 // point so we have to potentially shrink the main range if the
1879 // use was ending a live segment there.
1880 LiveQueryResult Q = Int.Query(UseIdx);
1881 if (Q.valueOut() == nullptr)
1882 ShrinkMainRange = true;
1883 }
1884}
1885
1886void RegisterCoalescer::updateRegDefsUses(Register SrcReg, Register DstReg,
1887 unsigned SubIdx) {
1888 bool DstIsPhys = DstReg.isPhysical();
1889 LiveInterval *DstInt = DstIsPhys ? nullptr : &LIS->getInterval(DstReg);
1890
1891 if (DstInt && DstInt->hasSubRanges() && DstReg != SrcReg) {
1892 for (MachineOperand &MO : MRI->reg_operands(DstReg)) {
1893 if (MO.isUndef())
1894 continue;
1895 unsigned SubReg = MO.getSubReg();
1896 if (SubReg == 0 && MO.isDef())
1897 continue;
1898
1899 MachineInstr &MI = *MO.getParent();
1900 if (MI.isDebugInstr())
1901 continue;
1902 SlotIndex UseIdx = LIS->getInstructionIndex(MI).getRegSlot(true);
1903 addUndefFlag(*DstInt, UseIdx, MO, SubReg);
1904 }
1905 }
1906
1908 for (MachineRegisterInfo::reg_instr_iterator I = MRI->reg_instr_begin(SrcReg),
1909 E = MRI->reg_instr_end();
1910 I != E;) {
1911 MachineInstr *UseMI = &*(I++);
1912
1913 // Each instruction can only be rewritten once because sub-register
1914 // composition is not always idempotent. When SrcReg != DstReg, rewriting
1915 // the UseMI operands removes them from the SrcReg use-def chain, but when
1916 // SrcReg is DstReg we could encounter UseMI twice if it has multiple
1917 // operands mentioning the virtual register.
1918 if (SrcReg == DstReg && !Visited.insert(UseMI).second)
1919 continue;
1920
1922 bool Reads, Writes;
1923 std::tie(Reads, Writes) = UseMI->readsWritesVirtualRegister(SrcReg, &Ops);
1924
1925 // If SrcReg wasn't read, it may still be the case that DstReg is live-in
1926 // because SrcReg is a sub-register.
1927 if (DstInt && !Reads && SubIdx && !UseMI->isDebugInstr())
1928 Reads = DstInt->liveAt(LIS->getInstructionIndex(*UseMI));
1929
1930 // Replace SrcReg with DstReg in all UseMI operands.
1931 for (unsigned Op : Ops) {
1933
1934 // Adjust <undef> flags in case of sub-register joins. We don't want to
1935 // turn a full def into a read-modify-write sub-register def and vice
1936 // versa.
1937 if (SubIdx && MO.isDef())
1938 MO.setIsUndef(!Reads);
1939
1940 // A subreg use of a partially undef (super) register may be a complete
1941 // undef use now and then has to be marked that way.
1942 if (MO.isUse() && !MO.isUndef() && !DstIsPhys) {
1943 unsigned SubUseIdx = TRI->composeSubRegIndices(SubIdx, MO.getSubReg());
1944 if (SubUseIdx != 0 && MRI->shouldTrackSubRegLiveness(DstReg)) {
1945 if (!DstInt->hasSubRanges()) {
1947 LaneBitmask FullMask = MRI->getMaxLaneMaskForVReg(DstInt->reg());
1948 LaneBitmask UsedLanes = TRI->getSubRegIndexLaneMask(SubIdx);
1949 LaneBitmask UnusedLanes = FullMask & ~UsedLanes;
1950 DstInt->createSubRangeFrom(Allocator, UsedLanes, *DstInt);
1951 // The unused lanes are just empty live-ranges at this point.
1952 // It is the caller responsibility to set the proper
1953 // dead segments if there is an actual dead def of the
1954 // unused lanes. This may happen with rematerialization.
1955 DstInt->createSubRange(Allocator, UnusedLanes);
1956 }
1957 SlotIndex MIIdx = UseMI->isDebugInstr()
1959 : LIS->getInstructionIndex(*UseMI);
1960 SlotIndex UseIdx = MIIdx.getRegSlot(true);
1961 addUndefFlag(*DstInt, UseIdx, MO, SubUseIdx);
1962 }
1963 }
1964
1965 if (DstIsPhys)
1966 MO.substPhysReg(DstReg, *TRI);
1967 else
1968 MO.substVirtReg(DstReg, SubIdx, *TRI);
1969 }
1970
1971 LLVM_DEBUG({
1972 dbgs() << "\t\tupdated: ";
1973 if (!UseMI->isDebugInstr())
1974 dbgs() << LIS->getInstructionIndex(*UseMI) << "\t";
1975 dbgs() << *UseMI;
1976 });
1977 }
1978}
1979
1980bool RegisterCoalescer::canJoinPhys(const CoalescerPair &CP) {
1981 // Always join simple intervals that are defined by a single copy from a
1982 // reserved register. This doesn't increase register pressure, so it is
1983 // always beneficial.
1984 if (!MRI->isReserved(CP.getDstReg())) {
1985 LLVM_DEBUG(dbgs() << "\tCan only merge into reserved registers.\n");
1986 return false;
1987 }
1988
1989 LiveInterval &JoinVInt = LIS->getInterval(CP.getSrcReg());
1990 if (JoinVInt.containsOneValue())
1991 return true;
1992
1993 LLVM_DEBUG(
1994 dbgs() << "\tCannot join complex intervals into reserved register.\n");
1995 return false;
1996}
1997
1998bool RegisterCoalescer::copyValueUndefInPredecessors(
2000 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
2001 SlotIndex PredEnd = LIS->getMBBEndIdx(Pred);
2002 if (VNInfo *V = S.getVNInfoAt(PredEnd.getPrevSlot())) {
2003 // If this is a self loop, we may be reading the same value.
2004 if (V->id != SLRQ.valueOutOrDead()->id)
2005 return false;
2006 }
2007 }
2008
2009 return true;
2010}
2011
2012void RegisterCoalescer::setUndefOnPrunedSubRegUses(LiveInterval &LI,
2013 Register Reg,
2014 LaneBitmask PrunedLanes) {
2015 // If we had other instructions in the segment reading the undef sublane
2016 // value, we need to mark them with undef.
2017 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
2018 unsigned SubRegIdx = MO.getSubReg();
2019 if (SubRegIdx == 0 || MO.isUndef())
2020 continue;
2021
2022 LaneBitmask SubRegMask = TRI->getSubRegIndexLaneMask(SubRegIdx);
2023 SlotIndex Pos = LIS->getInstructionIndex(*MO.getParent());
2024 for (LiveInterval::SubRange &S : LI.subranges()) {
2025 if (!S.liveAt(Pos) && (PrunedLanes & SubRegMask).any()) {
2026 MO.setIsUndef();
2027 break;
2028 }
2029 }
2030 }
2031
2033
2034 // A def of a subregister may be a use of other register lanes. Replacing
2035 // such a def with a def of a different register will eliminate the use,
2036 // and may cause the recorded live range to be larger than the actual
2037 // liveness in the program IR.
2038 LIS->shrinkToUses(&LI);
2039}
2040
2041bool RegisterCoalescer::joinCopy(
2042 MachineInstr *CopyMI, bool &Again,
2043 SmallPtrSetImpl<MachineInstr *> &CurrentErasedInstrs) {
2044 Again = false;
2045 LLVM_DEBUG(dbgs() << LIS->getInstructionIndex(*CopyMI) << '\t' << *CopyMI);
2046
2048 if (!CP.setRegisters(CopyMI)) {
2049 LLVM_DEBUG(dbgs() << "\tNot coalescable.\n");
2050 return false;
2051 }
2052
2053 if (CP.getNewRC()) {
2054 if (RegClassInfo.getNumAllocatableRegs(CP.getNewRC()) == 0) {
2055 LLVM_DEBUG(dbgs() << "\tNo " << TRI->getRegClassName(CP.getNewRC())
2056 << "are available for allocation\n");
2057 return false;
2058 }
2059
2060 auto SrcRC = MRI->getRegClass(CP.getSrcReg());
2061 auto DstRC = MRI->getRegClass(CP.getDstReg());
2062 unsigned SrcIdx = CP.getSrcIdx();
2063 unsigned DstIdx = CP.getDstIdx();
2064 if (CP.isFlipped()) {
2065 std::swap(SrcIdx, DstIdx);
2066 std::swap(SrcRC, DstRC);
2067 }
2068 if (!TRI->shouldCoalesce(CopyMI, SrcRC, SrcIdx, DstRC, DstIdx,
2069 CP.getNewRC(), *LIS)) {
2070 LLVM_DEBUG(dbgs() << "\tSubtarget bailed on coalescing.\n");
2071 return false;
2072 }
2073 }
2074
2075 // Dead code elimination. This really should be handled by MachineDCE, but
2076 // sometimes dead copies slip through, and we can't generate invalid live
2077 // ranges.
2078 if (!CP.isPhys() && CopyMI->allDefsAreDead()) {
2079 LLVM_DEBUG(dbgs() << "\tCopy is dead.\n");
2080 DeadDefs.push_back(CopyMI);
2081 eliminateDeadDefs();
2082 return true;
2083 }
2084
2085 // Eliminate undefs.
2086 if (!CP.isPhys()) {
2087 // If this is an IMPLICIT_DEF, leave it alone, but don't try to coalesce.
2088 if (MachineInstr *UndefMI = eliminateUndefCopy(CopyMI)) {
2089 if (UndefMI->isImplicitDef())
2090 return false;
2091 deleteInstr(CopyMI);
2092 return false; // Not coalescable.
2093 }
2094 }
2095
2096 // Coalesced copies are normally removed immediately, but transformations
2097 // like removeCopyByCommutingDef() can inadvertently create identity copies.
2098 // When that happens, just join the values and remove the copy.
2099 if (CP.getSrcReg() == CP.getDstReg()) {
2100 LiveInterval &LI = LIS->getInterval(CP.getSrcReg());
2101 LLVM_DEBUG(dbgs() << "\tCopy already coalesced: " << LI << '\n');
2102 const SlotIndex CopyIdx = LIS->getInstructionIndex(*CopyMI);
2103 LiveQueryResult LRQ = LI.Query(CopyIdx);
2104 if (VNInfo *DefVNI = LRQ.valueDefined()) {
2105 VNInfo *ReadVNI = LRQ.valueIn();
2106 assert(ReadVNI && "No value before copy and no <undef> flag.");
2107 assert(ReadVNI != DefVNI && "Cannot read and define the same value.");
2108
2109 // Track incoming undef lanes we need to eliminate from the subrange.
2110 LaneBitmask PrunedLanes;
2111 MachineBasicBlock *MBB = CopyMI->getParent();
2112
2113 // Process subregister liveranges.
2114 for (LiveInterval::SubRange &S : LI.subranges()) {
2115 LiveQueryResult SLRQ = S.Query(CopyIdx);
2116 if (VNInfo *SDefVNI = SLRQ.valueDefined()) {
2117 if (VNInfo *SReadVNI = SLRQ.valueIn())
2118 SDefVNI = S.MergeValueNumberInto(SDefVNI, SReadVNI);
2119
2120 // If this copy introduced an undef subrange from an incoming value,
2121 // we need to eliminate the undef live in values from the subrange.
2122 if (copyValueUndefInPredecessors(S, MBB, SLRQ)) {
2123 LLVM_DEBUG(dbgs() << "Incoming sublane value is undef at copy\n");
2124 PrunedLanes |= S.LaneMask;
2125 S.removeValNo(SDefVNI);
2126 }
2127 }
2128 }
2129
2130 LI.MergeValueNumberInto(DefVNI, ReadVNI);
2131 if (PrunedLanes.any()) {
2132 LLVM_DEBUG(dbgs() << "Pruning undef incoming lanes: " << PrunedLanes
2133 << '\n');
2134 setUndefOnPrunedSubRegUses(LI, CP.getSrcReg(), PrunedLanes);
2135 }
2136
2137 LLVM_DEBUG(dbgs() << "\tMerged values: " << LI << '\n');
2138 }
2139 deleteInstr(CopyMI);
2140 return true;
2141 }
2142
2143 // Enforce policies.
2144 if (CP.isPhys()) {
2145 LLVM_DEBUG(dbgs() << "\tConsidering merging "
2146 << printReg(CP.getSrcReg(), TRI) << " with "
2147 << printReg(CP.getDstReg(), TRI, CP.getSrcIdx()) << '\n');
2148 if (!canJoinPhys(CP)) {
2149 // Before giving up coalescing, try rematerializing the source of
2150 // the copy instead if it is cheap.
2151 bool IsDefCopy = false;
2152 if (reMaterializeDef(CP, CopyMI, IsDefCopy))
2153 return true;
2154 if (IsDefCopy)
2155 Again = true; // May be possible to coalesce later.
2156 return false;
2157 }
2158 } else {
2159 // When possible, let DstReg be the larger interval.
2160 if (!CP.isPartial() && LIS->getInterval(CP.getSrcReg()).size() >
2161 LIS->getInterval(CP.getDstReg()).size())
2162 CP.flip();
2163
2164 LLVM_DEBUG({
2165 dbgs() << "\tConsidering merging to "
2166 << TRI->getRegClassName(CP.getNewRC()) << " with ";
2167 if (CP.getDstIdx() && CP.getSrcIdx())
2168 dbgs() << printReg(CP.getDstReg()) << " in "
2169 << TRI->getSubRegIndexName(CP.getDstIdx()) << " and "
2170 << printReg(CP.getSrcReg()) << " in "
2171 << TRI->getSubRegIndexName(CP.getSrcIdx()) << '\n';
2172 else
2173 dbgs() << printReg(CP.getSrcReg(), TRI) << " in "
2174 << printReg(CP.getDstReg(), TRI, CP.getSrcIdx()) << '\n';
2175 });
2176 }
2177
2178 ShrinkMask = LaneBitmask::getNone();
2179 ShrinkMainRange = false;
2180
2181 // Okay, attempt to join these two intervals. On failure, this returns false.
2182 // Otherwise, if one of the intervals being joined is a physreg, this method
2183 // always canonicalizes DstInt to be it. The output "SrcInt" will not have
2184 // been modified, so we can use this information below to update aliases.
2185 if (!joinIntervals(CP)) {
2186 // Coalescing failed.
2187
2188 // Try rematerializing the definition of the source if it is cheap.
2189 bool IsDefCopy = false;
2190 if (reMaterializeDef(CP, CopyMI, IsDefCopy))
2191 return true;
2192
2193 // If we can eliminate the copy without merging the live segments, do so
2194 // now.
2195 if (!CP.isPartial() && !CP.isPhys()) {
2196 bool Changed = adjustCopiesBackFrom(CP, CopyMI);
2197 bool Shrink = false;
2198 if (!Changed)
2199 std::tie(Changed, Shrink) = removeCopyByCommutingDef(CP, CopyMI);
2200 if (Changed) {
2201 deleteInstr(CopyMI);
2202 if (Shrink) {
2203 Register DstReg = CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg();
2204 LiveInterval &DstLI = LIS->getInterval(DstReg);
2205 shrinkToUses(&DstLI);
2206 LLVM_DEBUG(dbgs() << "\t\tshrunk: " << DstLI << '\n');
2207 }
2208 LLVM_DEBUG(dbgs() << "\tTrivial!\n");
2209 return true;
2210 }
2211 }
2212
2213 // Try and see if we can partially eliminate the copy by moving the copy to
2214 // its predecessor.
2215 if (!CP.isPartial() && !CP.isPhys())
2216 if (removePartialRedundancy(CP, *CopyMI))
2217 return true;
2218
2219 // Otherwise, we are unable to join the intervals.
2220 LLVM_DEBUG(dbgs() << "\tInterference!\n");
2221 Again = true; // May be possible to coalesce later.
2222 return false;
2223 }
2224
2225 // Coalescing to a virtual register that is of a sub-register class of the
2226 // other. Make sure the resulting register is set to the right register class.
2227 if (CP.isCrossClass()) {
2228 ++numCrossRCs;
2229 MRI->setRegClass(CP.getDstReg(), CP.getNewRC());
2230 }
2231
2232 // Removing sub-register copies can ease the register class constraints.
2233 // Make sure we attempt to inflate the register class of DstReg.
2234 if (!CP.isPhys() && RegClassInfo.isProperSubClass(CP.getNewRC()))
2235 InflateRegs.push_back(CP.getDstReg());
2236
2237 // CopyMI has been erased by joinIntervals at this point. Remove it from
2238 // ErasedInstrs since copyCoalesceWorkList() won't add a successful join back
2239 // to the work list. This keeps ErasedInstrs from growing needlessly.
2240 if (ErasedInstrs.erase(CopyMI))
2241 // But we may encounter the instruction again in this iteration.
2242 CurrentErasedInstrs.insert(CopyMI);
2243
2244 // Rewrite all SrcReg operands to DstReg.
2245 // Also update DstReg operands to include DstIdx if it is set.
2246 if (CP.getDstIdx())
2247 updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx());
2248 updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx());
2249
2250 // Shrink subregister ranges if necessary.
2251 if (ShrinkMask.any()) {
2252 LiveInterval &LI = LIS->getInterval(CP.getDstReg());
2253 for (LiveInterval::SubRange &S : LI.subranges()) {
2254 if ((S.LaneMask & ShrinkMask).none())
2255 continue;
2256 LLVM_DEBUG(dbgs() << "Shrink LaneUses (Lane " << PrintLaneMask(S.LaneMask)
2257 << ")\n");
2258 LIS->shrinkToUses(S, LI.reg());
2259 ShrinkMainRange = true;
2260 }
2262 }
2263
2264 // CP.getSrcReg()'s live interval has been merged into CP.getDstReg's live
2265 // interval. Since CP.getSrcReg() is in ToBeUpdated set and its live interval
2266 // is not up-to-date, need to update the merged live interval here.
2267 if (ToBeUpdated.count(CP.getSrcReg()))
2268 ShrinkMainRange = true;
2269
2270 if (ShrinkMainRange) {
2271 LiveInterval &LI = LIS->getInterval(CP.getDstReg());
2272 shrinkToUses(&LI);
2273 }
2274
2275 // SrcReg is guaranteed to be the register whose live interval that is
2276 // being merged.
2277 LIS->removeInterval(CP.getSrcReg());
2278
2279 // Update regalloc hint.
2280 TRI->updateRegAllocHint(CP.getSrcReg(), CP.getDstReg(), *MF);
2281
2282 LLVM_DEBUG({
2283 dbgs() << "\tSuccess: " << printReg(CP.getSrcReg(), TRI, CP.getSrcIdx())
2284 << " -> " << printReg(CP.getDstReg(), TRI, CP.getDstIdx()) << '\n';
2285 dbgs() << "\tResult = ";
2286 if (CP.isPhys())
2287 dbgs() << printReg(CP.getDstReg(), TRI);
2288 else
2289 dbgs() << LIS->getInterval(CP.getDstReg());
2290 dbgs() << '\n';
2291 });
2292
2293 ++numJoins;
2294 return true;
2295}
2296
2297bool RegisterCoalescer::joinReservedPhysReg(CoalescerPair &CP) {
2298 Register DstReg = CP.getDstReg();
2299 Register SrcReg = CP.getSrcReg();
2300 assert(CP.isPhys() && "Must be a physreg copy");
2301 assert(MRI->isReserved(DstReg) && "Not a reserved register");
2302 LiveInterval &RHS = LIS->getInterval(SrcReg);
2303 LLVM_DEBUG(dbgs() << "\t\tRHS = " << RHS << '\n');
2304
2305 assert(RHS.containsOneValue() && "Invalid join with reserved register");
2306
2307 // Optimization for reserved registers like ESP. We can only merge with a
2308 // reserved physreg if RHS has a single value that is a copy of DstReg.
2309 // The live range of the reserved register will look like a set of dead defs
2310 // - we don't properly track the live range of reserved registers.
2311
2312 // Deny any overlapping intervals. This depends on all the reserved
2313 // register live ranges to look like dead defs.
2314 if (!MRI->isConstantPhysReg(DstReg)) {
2315 for (MCRegUnit Unit : TRI->regunits(DstReg)) {
2316 // Abort if not all the regunits are reserved.
2317 for (MCRegUnitRootIterator RI(Unit, TRI); RI.isValid(); ++RI) {
2318 if (!MRI->isReserved(*RI))
2319 return false;
2320 }
2321 if (RHS.overlaps(LIS->getRegUnit(Unit))) {
2322 LLVM_DEBUG(dbgs() << "\t\tInterference: " << printRegUnit(Unit, TRI)
2323 << '\n');
2324 return false;
2325 }
2326 }
2327
2328 // We must also check for overlaps with regmask clobbers.
2329 BitVector RegMaskUsable;
2330 if (LIS->checkRegMaskInterference(RHS, RegMaskUsable) &&
2331 !RegMaskUsable.test(DstReg.id())) {
2332 LLVM_DEBUG(dbgs() << "\t\tRegMask interference\n");
2333 return false;
2334 }
2335 }
2336
2337 // Skip any value computations, we are not adding new values to the
2338 // reserved register. Also skip merging the live ranges, the reserved
2339 // register live range doesn't need to be accurate as long as all the
2340 // defs are there.
2341
2342 // Delete the identity copy.
2343 MachineInstr *CopyMI;
2344 if (CP.isFlipped()) {
2345 // Physreg is copied into vreg
2346 // %y = COPY %physreg_x
2347 // ... //< no other def of %physreg_x here
2348 // use %y
2349 // =>
2350 // ...
2351 // use %physreg_x
2352 CopyMI = MRI->getVRegDef(SrcReg);
2353 deleteInstr(CopyMI);
2354 } else {
2355 // VReg is copied into physreg:
2356 // %y = def
2357 // ... //< no other def or use of %physreg_x here
2358 // %physreg_x = COPY %y
2359 // =>
2360 // %physreg_x = def
2361 // ...
2362 if (!MRI->hasOneNonDBGUse(SrcReg)) {
2363 LLVM_DEBUG(dbgs() << "\t\tMultiple vreg uses!\n");
2364 return false;
2365 }
2366
2367 if (!LIS->intervalIsInOneMBB(RHS)) {
2368 LLVM_DEBUG(dbgs() << "\t\tComplex control flow!\n");
2369 return false;
2370 }
2371
2372 MachineInstr &DestMI = *MRI->getVRegDef(SrcReg);
2373 CopyMI = &*MRI->use_instr_nodbg_begin(SrcReg);
2374 SlotIndex CopyRegIdx = LIS->getInstructionIndex(*CopyMI).getRegSlot();
2375 SlotIndex DestRegIdx = LIS->getInstructionIndex(DestMI).getRegSlot();
2376
2377 if (!MRI->isConstantPhysReg(DstReg)) {
2378 // We checked above that there are no interfering defs of the physical
2379 // register. However, for this case, where we intend to move up the def of
2380 // the physical register, we also need to check for interfering uses.
2381 SlotIndexes *Indexes = LIS->getSlotIndexes();
2382 for (SlotIndex SI = Indexes->getNextNonNullIndex(DestRegIdx);
2383 SI != CopyRegIdx; SI = Indexes->getNextNonNullIndex(SI)) {
2385 if (MI->readsRegister(DstReg, TRI)) {
2386 LLVM_DEBUG(dbgs() << "\t\tInterference (read): " << *MI);
2387 return false;
2388 }
2389 }
2390 }
2391
2392 // We're going to remove the copy which defines a physical reserved
2393 // register, so remove its valno, etc.
2394 LLVM_DEBUG(dbgs() << "\t\tRemoving phys reg def of "
2395 << printReg(DstReg, TRI) << " at " << CopyRegIdx << "\n");
2396
2397 LIS->removePhysRegDefAt(DstReg.asMCReg(), CopyRegIdx);
2398 deleteInstr(CopyMI);
2399
2400 // Create a new dead def at the new def location.
2401 for (MCRegUnit Unit : TRI->regunits(DstReg)) {
2402 LiveRange &LR = LIS->getRegUnit(Unit);
2403 LR.createDeadDef(DestRegIdx, LIS->getVNInfoAllocator());
2404 }
2405 }
2406
2407 // We don't track kills for reserved registers.
2408 MRI->clearKillFlags(CP.getSrcReg());
2409
2410 return true;
2411}
2412
2413//===----------------------------------------------------------------------===//
2414// Interference checking and interval joining
2415//===----------------------------------------------------------------------===//
2416//
2417// In the easiest case, the two live ranges being joined are disjoint, and
2418// there is no interference to consider. It is quite common, though, to have
2419// overlapping live ranges, and we need to check if the interference can be
2420// resolved.
2421//
2422// The live range of a single SSA value forms a sub-tree of the dominator tree.
2423// This means that two SSA values overlap if and only if the def of one value
2424// is contained in the live range of the other value. As a special case, the
2425// overlapping values can be defined at the same index.
2426//
2427// The interference from an overlapping def can be resolved in these cases:
2428//
2429// 1. Coalescable copies. The value is defined by a copy that would become an
2430// identity copy after joining SrcReg and DstReg. The copy instruction will
2431// be removed, and the value will be merged with the source value.
2432//
2433// There can be several copies back and forth, causing many values to be
2434// merged into one. We compute a list of ultimate values in the joined live
2435// range as well as a mappings from the old value numbers.
2436//
2437// 2. IMPLICIT_DEF. This instruction is only inserted to ensure all PHI
2438// predecessors have a live out value. It doesn't cause real interference,
2439// and can be merged into the value it overlaps. Like a coalescable copy, it
2440// can be erased after joining.
2441//
2442// 3. Copy of external value. The overlapping def may be a copy of a value that
2443// is already in the other register. This is like a coalescable copy, but
2444// the live range of the source register must be trimmed after erasing the
2445// copy instruction:
2446//
2447// %src = COPY %ext
2448// %dst = COPY %ext <-- Remove this COPY, trim the live range of %ext.
2449//
2450// 4. Clobbering undefined lanes. Vector registers are sometimes built by
2451// defining one lane at a time:
2452//
2453// %dst:ssub0<def,read-undef> = FOO
2454// %src = BAR
2455// %dst:ssub1 = COPY %src
2456//
2457// The live range of %src overlaps the %dst value defined by FOO, but
2458// merging %src into %dst:ssub1 is only going to clobber the ssub1 lane
2459// which was undef anyway.
2460//
2461// The value mapping is more complicated in this case. The final live range
2462// will have different value numbers for both FOO and BAR, but there is no
2463// simple mapping from old to new values. It may even be necessary to add
2464// new PHI values.
2465//
2466// 5. Clobbering dead lanes. A def may clobber a lane of a vector register that
2467// is live, but never read. This can happen because we don't compute
2468// individual live ranges per lane.
2469//
2470// %dst = FOO
2471// %src = BAR
2472// %dst:ssub1 = COPY %src
2473//
2474// This kind of interference is only resolved locally. If the clobbered
2475// lane value escapes the block, the join is aborted.
2476
2477namespace {
2478
2479/// Track information about values in a single virtual register about to be
2480/// joined. Objects of this class are always created in pairs - one for each
2481/// side of the CoalescerPair (or one for each lane of a side of the coalescer
2482/// pair)
2483class JoinVals {
2484 /// Live range we work on.
2485 LiveRange &LR;
2486
2487 /// (Main) register we work on.
2488 const Register Reg;
2489
2490 /// Reg (and therefore the values in this liverange) will end up as
2491 /// subregister SubIdx in the coalesced register. Either CP.DstIdx or
2492 /// CP.SrcIdx.
2493 const unsigned SubIdx;
2494
2495 /// The LaneMask that this liverange will occupy the coalesced register. May
2496 /// be smaller than the lanemask produced by SubIdx when merging subranges.
2497 const LaneBitmask LaneMask;
2498
2499 /// This is true when joining sub register ranges, false when joining main
2500 /// ranges.
2501 const bool SubRangeJoin;
2502
2503 /// Whether the current LiveInterval tracks subregister liveness.
2504 const bool TrackSubRegLiveness;
2505
2506 /// Values that will be present in the final live range.
2507 SmallVectorImpl<VNInfo *> &NewVNInfo;
2508
2509 const CoalescerPair &CP;
2510 LiveIntervals *LIS;
2511 SlotIndexes *Indexes;
2512 const TargetRegisterInfo *TRI;
2513
2514 /// Value number assignments. Maps value numbers in LI to entries in
2515 /// NewVNInfo. This is suitable for passing to LiveInterval::join().
2516 SmallVector<int, 8> Assignments;
2517
2518public:
2519 /// Conflict resolution for overlapping values.
2520 enum ConflictResolution {
2521 /// No overlap, simply keep this value.
2522 CR_Keep,
2523
2524 /// Merge this value into OtherVNI and erase the defining instruction.
2525 /// Used for IMPLICIT_DEF, coalescable copies, and copies from external
2526 /// values.
2527 CR_Erase,
2528
2529 /// Merge this value into OtherVNI but keep the defining instruction.
2530 /// This is for the special case where OtherVNI is defined by the same
2531 /// instruction.
2532 CR_Merge,
2533
2534 /// Keep this value, and have it replace OtherVNI where possible. This
2535 /// complicates value mapping since OtherVNI maps to two different values
2536 /// before and after this def.
2537 /// Used when clobbering undefined or dead lanes.
2538 CR_Replace,
2539
2540 /// Unresolved conflict. Visit later when all values have been mapped.
2541 CR_Unresolved,
2542
2543 /// Unresolvable conflict. Abort the join.
2544 CR_Impossible
2545 };
2546
2547private:
2548 /// Per-value info for LI. The lane bit masks are all relative to the final
2549 /// joined register, so they can be compared directly between SrcReg and
2550 /// DstReg.
2551 struct Val {
2552 ConflictResolution Resolution = CR_Keep;
2553
2554 /// Lanes written by this def, 0 for unanalyzed values.
2555 LaneBitmask WriteLanes;
2556
2557 /// Lanes with defined values in this register. Other lanes are undef and
2558 /// safe to clobber.
2559 LaneBitmask ValidLanes;
2560
2561 /// Value in LI being redefined by this def.
2562 VNInfo *RedefVNI = nullptr;
2563
2564 /// Value in the other live range that overlaps this def, if any.
2565 VNInfo *OtherVNI = nullptr;
2566
2567 /// Is this value an IMPLICIT_DEF that can be erased?
2568 ///
2569 /// IMPLICIT_DEF values should only exist at the end of a basic block that
2570 /// is a predecessor to a phi-value. These IMPLICIT_DEF instructions can be
2571 /// safely erased if they are overlapping a live value in the other live
2572 /// interval.
2573 ///
2574 /// Weird control flow graphs and incomplete PHI handling in
2575 /// ProcessImplicitDefs can very rarely create IMPLICIT_DEF values with
2576 /// longer live ranges. Such IMPLICIT_DEF values should be treated like
2577 /// normal values.
2578 bool ErasableImplicitDef = false;
2579
2580 /// True when the live range of this value will be pruned because of an
2581 /// overlapping CR_Replace value in the other live range.
2582 bool Pruned = false;
2583
2584 /// True once Pruned above has been computed.
2585 bool PrunedComputed = false;
2586
2587 /// True if this value is determined to be identical to OtherVNI
2588 /// (in valuesIdentical). This is used with CR_Erase where the erased
2589 /// copy is redundant, i.e. the source value is already the same as
2590 /// the destination. In such cases the subranges need to be updated
2591 /// properly. See comment at pruneSubRegValues for more info.
2592 bool Identical = false;
2593
2594 Val() = default;
2595
2596 bool isAnalyzed() const { return WriteLanes.any(); }
2597
2598 /// Mark this value as an IMPLICIT_DEF which must be kept as if it were an
2599 /// ordinary value.
2600 void mustKeepImplicitDef(const TargetRegisterInfo &TRI,
2601 const MachineInstr &ImpDef) {
2602 assert(ImpDef.isImplicitDef());
2603 ErasableImplicitDef = false;
2604 ValidLanes = TRI.getSubRegIndexLaneMask(ImpDef.getOperand(0).getSubReg());
2605 }
2606 };
2607
2608 /// One entry per value number in LI.
2610
2611 /// Compute the bitmask of lanes actually written by DefMI.
2612 /// Set Redef if there are any partial register definitions that depend on the
2613 /// previous value of the register.
2614 LaneBitmask computeWriteLanes(const MachineInstr *DefMI, bool &Redef) const;
2615
2616 /// Find the ultimate value that VNI was copied from.
2617 std::pair<const VNInfo *, Register> followCopyChain(const VNInfo *VNI) const;
2618
2619 bool valuesIdentical(VNInfo *Value0, VNInfo *Value1,
2620 const JoinVals &Other) const;
2621
2622 /// Analyze ValNo in this live range, and set all fields of Vals[ValNo].
2623 /// Return a conflict resolution when possible, but leave the hard cases as
2624 /// CR_Unresolved.
2625 /// Recursively calls computeAssignment() on this and Other, guaranteeing that
2626 /// both OtherVNI and RedefVNI have been analyzed and mapped before returning.
2627 /// The recursion always goes upwards in the dominator tree, making loops
2628 /// impossible.
2629 ConflictResolution analyzeValue(unsigned ValNo, JoinVals &Other);
2630
2631 /// Compute the value assignment for ValNo in RI.
2632 /// This may be called recursively by analyzeValue(), but never for a ValNo on
2633 /// the stack.
2634 void computeAssignment(unsigned ValNo, JoinVals &Other);
2635
2636 /// Assuming ValNo is going to clobber some valid lanes in Other.LR, compute
2637 /// the extent of the tainted lanes in the block.
2638 ///
2639 /// Multiple values in Other.LR can be affected since partial redefinitions
2640 /// can preserve previously tainted lanes.
2641 ///
2642 /// 1 %dst = VLOAD <-- Define all lanes in %dst
2643 /// 2 %src = FOO <-- ValNo to be joined with %dst:ssub0
2644 /// 3 %dst:ssub1 = BAR <-- Partial redef doesn't clear taint in ssub0
2645 /// 4 %dst:ssub0 = COPY %src <-- Conflict resolved, ssub0 wasn't read
2646 ///
2647 /// For each ValNo in Other that is affected, add an (EndIndex, TaintedLanes)
2648 /// entry to TaintedVals.
2649 ///
2650 /// Returns false if the tainted lanes extend beyond the basic block.
2651 bool
2652 taintExtent(unsigned ValNo, LaneBitmask TaintedLanes, JoinVals &Other,
2653 SmallVectorImpl<std::pair<SlotIndex, LaneBitmask>> &TaintExtent);
2654
2655 /// Return true if MI uses any of the given Lanes from Reg.
2656 /// This does not include partial redefinitions of Reg.
2657 bool usesLanes(const MachineInstr &MI, Register, unsigned, LaneBitmask) const;
2658
2659 /// Determine if ValNo is a copy of a value number in LR or Other.LR that will
2660 /// be pruned:
2661 ///
2662 /// %dst = COPY %src
2663 /// %src = COPY %dst <-- This value to be pruned.
2664 /// %dst = COPY %src <-- This value is a copy of a pruned value.
2665 bool isPrunedValue(unsigned ValNo, JoinVals &Other);
2666
2667public:
2668 JoinVals(LiveRange &LR, Register Reg, unsigned SubIdx, LaneBitmask LaneMask,
2669 SmallVectorImpl<VNInfo *> &newVNInfo, const CoalescerPair &cp,
2670 LiveIntervals *lis, const TargetRegisterInfo *TRI, bool SubRangeJoin,
2671 bool TrackSubRegLiveness)
2672 : LR(LR), Reg(Reg), SubIdx(SubIdx), LaneMask(LaneMask),
2673 SubRangeJoin(SubRangeJoin), TrackSubRegLiveness(TrackSubRegLiveness),
2674 NewVNInfo(newVNInfo), CP(cp), LIS(lis), Indexes(LIS->getSlotIndexes()),
2675 TRI(TRI), Assignments(LR.getNumValNums(), -1),
2676 Vals(LR.getNumValNums()) {}
2677
2678 /// Analyze defs in LR and compute a value mapping in NewVNInfo.
2679 /// Returns false if any conflicts were impossible to resolve.
2680 bool mapValues(JoinVals &Other);
2681
2682 /// Try to resolve conflicts that require all values to be mapped.
2683 /// Returns false if any conflicts were impossible to resolve.
2684 bool resolveConflicts(JoinVals &Other);
2685
2686 /// Prune the live range of values in Other.LR where they would conflict with
2687 /// CR_Replace values in LR. Collect end points for restoring the live range
2688 /// after joining.
2689 void pruneValues(JoinVals &Other, SmallVectorImpl<SlotIndex> &EndPoints,
2690 bool changeInstrs);
2691
2692 /// Removes subranges starting at copies that get removed. This sometimes
2693 /// happens when undefined subranges are copied around. These ranges contain
2694 /// no useful information and can be removed.
2695 void pruneSubRegValues(LiveInterval &LI, LaneBitmask &ShrinkMask);
2696
2697 /// Pruning values in subranges can lead to removing segments in these
2698 /// subranges started by IMPLICIT_DEFs. The corresponding segments in
2699 /// the main range also need to be removed. This function will mark
2700 /// the corresponding values in the main range as pruned, so that
2701 /// eraseInstrs can do the final cleanup.
2702 /// The parameter @p LI must be the interval whose main range is the
2703 /// live range LR.
2704 void pruneMainSegments(LiveInterval &LI, bool &ShrinkMainRange);
2705
2706 /// Erase any machine instructions that have been coalesced away.
2707 /// Add erased instructions to ErasedInstrs.
2708 /// Add foreign virtual registers to ShrinkRegs if their live range ended at
2709 /// the erased instrs.
2710 void eraseInstrs(SmallPtrSetImpl<MachineInstr *> &ErasedInstrs,
2711 SmallVectorImpl<Register> &ShrinkRegs,
2712 LiveInterval *LI = nullptr);
2713
2714 /// Remove liverange defs at places where implicit defs will be removed.
2715 void removeImplicitDefs();
2716
2717 /// Get the value assignments suitable for passing to LiveInterval::join.
2718 const int *getAssignments() const { return Assignments.data(); }
2719
2720 /// Get the conflict resolution for a value number.
2721 ConflictResolution getResolution(unsigned Num) const {
2722 return Vals[Num].Resolution;
2723 }
2724};
2725
2726} // end anonymous namespace
2727
2728LaneBitmask JoinVals::computeWriteLanes(const MachineInstr *DefMI,
2729 bool &Redef) const {
2730 LaneBitmask L;
2731 for (const MachineOperand &MO : DefMI->all_defs()) {
2732 if (MO.getReg() != Reg)
2733 continue;
2734 L |= TRI->getSubRegIndexLaneMask(
2735 TRI->composeSubRegIndices(SubIdx, MO.getSubReg()));
2736 if (MO.readsReg())
2737 Redef = true;
2738 }
2739 return L;
2740}
2741
2742std::pair<const VNInfo *, Register>
2743JoinVals::followCopyChain(const VNInfo *VNI) const {
2744 Register TrackReg = Reg;
2745
2746 while (!VNI->isPHIDef()) {
2747 SlotIndex Def = VNI->def;
2748 MachineInstr *MI = Indexes->getInstructionFromIndex(Def);
2749 assert(MI && "No defining instruction");
2750 if (!MI->isFullCopy())
2751 return std::make_pair(VNI, TrackReg);
2752 Register SrcReg = MI->getOperand(1).getReg();
2753 if (!SrcReg.isVirtual())
2754 return std::make_pair(VNI, TrackReg);
2755
2756 const LiveInterval &LI = LIS->getInterval(SrcReg);
2757 const VNInfo *ValueIn;
2758 // No subrange involved.
2759 if (!SubRangeJoin || !LI.hasSubRanges()) {
2760 LiveQueryResult LRQ = LI.Query(Def);
2761 ValueIn = LRQ.valueIn();
2762 } else {
2763 // Query subranges. Ensure that all matching ones take us to the same def
2764 // (allowing some of them to be undef).
2765 ValueIn = nullptr;
2766 for (const LiveInterval::SubRange &S : LI.subranges()) {
2767 // Transform lanemask to a mask in the joined live interval.
2768 LaneBitmask SMask = TRI->composeSubRegIndexLaneMask(SubIdx, S.LaneMask);
2769 if ((SMask & LaneMask).none())
2770 continue;
2771 LiveQueryResult LRQ = S.Query(Def);
2772 if (!ValueIn) {
2773 ValueIn = LRQ.valueIn();
2774 continue;
2775 }
2776 if (LRQ.valueIn() && ValueIn != LRQ.valueIn())
2777 return std::make_pair(VNI, TrackReg);
2778 }
2779 }
2780 if (ValueIn == nullptr) {
2781 // Reaching an undefined value is legitimate, for example:
2782 //
2783 // 1 undef %0.sub1 = ... ;; %0.sub0 == undef
2784 // 2 %1 = COPY %0 ;; %1 is defined here.
2785 // 3 %0 = COPY %1 ;; Now %0.sub0 has a definition,
2786 // ;; but it's equivalent to "undef".
2787 return std::make_pair(nullptr, SrcReg);
2788 }
2789 VNI = ValueIn;
2790 TrackReg = SrcReg;
2791 }
2792 return std::make_pair(VNI, TrackReg);
2793}
2794
2795bool JoinVals::valuesIdentical(VNInfo *Value0, VNInfo *Value1,
2796 const JoinVals &Other) const {
2797 const VNInfo *Orig0;
2798 Register Reg0;
2799 std::tie(Orig0, Reg0) = followCopyChain(Value0);
2800 if (Orig0 == Value1 && Reg0 == Other.Reg)
2801 return true;
2802
2803 const VNInfo *Orig1;
2804 Register Reg1;
2805 std::tie(Orig1, Reg1) = Other.followCopyChain(Value1);
2806 // If both values are undefined, and the source registers are the same
2807 // register, the values are identical. Filter out cases where only one
2808 // value is defined.
2809 if (Orig0 == nullptr || Orig1 == nullptr)
2810 return Orig0 == Orig1 && Reg0 == Reg1;
2811
2812 // The values are equal if they are defined at the same place and use the
2813 // same register. Note that we cannot compare VNInfos directly as some of
2814 // them might be from a copy created in mergeSubRangeInto() while the other
2815 // is from the original LiveInterval.
2816 return Orig0->def == Orig1->def && Reg0 == Reg1;
2817}
2818
2819JoinVals::ConflictResolution JoinVals::analyzeValue(unsigned ValNo,
2820 JoinVals &Other) {
2821 Val &V = Vals[ValNo];
2822 assert(!V.isAnalyzed() && "Value has already been analyzed!");
2823 VNInfo *VNI = LR.getValNumInfo(ValNo);
2824 if (VNI->isUnused()) {
2825 V.WriteLanes = LaneBitmask::getAll();
2826 return CR_Keep;
2827 }
2828
2829 // Get the instruction defining this value, compute the lanes written.
2830 const MachineInstr *DefMI = nullptr;
2831 if (VNI->isPHIDef()) {
2832 // Conservatively assume that all lanes in a PHI are valid.
2833 LaneBitmask Lanes = SubRangeJoin ? LaneBitmask::getLane(0)
2834 : TRI->getSubRegIndexLaneMask(SubIdx);
2835 V.ValidLanes = V.WriteLanes = Lanes;
2836 } else {
2837 DefMI = Indexes->getInstructionFromIndex(VNI->def);
2838 assert(DefMI != nullptr);
2839 if (SubRangeJoin) {
2840 // We don't care about the lanes when joining subregister ranges.
2841 V.WriteLanes = V.ValidLanes = LaneBitmask::getLane(0);
2842 if (DefMI->isImplicitDef()) {
2843 V.ValidLanes = LaneBitmask::getNone();
2844 V.ErasableImplicitDef = true;
2845 }
2846 } else {
2847 bool Redef = false;
2848 V.ValidLanes = V.WriteLanes = computeWriteLanes(DefMI, Redef);
2849
2850 // If this is a read-modify-write instruction, there may be more valid
2851 // lanes than the ones written by this instruction.
2852 // This only covers partial redef operands. DefMI may have normal use
2853 // operands reading the register. They don't contribute valid lanes.
2854 //
2855 // This adds ssub1 to the set of valid lanes in %src:
2856 //
2857 // %src:ssub1 = FOO
2858 //
2859 // This leaves only ssub1 valid, making any other lanes undef:
2860 //
2861 // %src:ssub1<def,read-undef> = FOO %src:ssub2
2862 //
2863 // The <read-undef> flag on the def operand means that old lane values are
2864 // not important.
2865 if (Redef) {
2866 V.RedefVNI = LR.Query(VNI->def).valueIn();
2867 assert((TrackSubRegLiveness || V.RedefVNI) &&
2868 "Instruction is reading nonexistent value");
2869 if (V.RedefVNI != nullptr) {
2870 computeAssignment(V.RedefVNI->id, Other);
2871 V.ValidLanes |= Vals[V.RedefVNI->id].ValidLanes;
2872 }
2873 }
2874
2875 // An IMPLICIT_DEF writes undef values.
2876 if (DefMI->isImplicitDef()) {
2877 // We normally expect IMPLICIT_DEF values to be live only until the end
2878 // of their block. If the value is really live longer and gets pruned in
2879 // another block, this flag is cleared again.
2880 //
2881 // Clearing the valid lanes is deferred until it is sure this can be
2882 // erased.
2883 V.ErasableImplicitDef = true;
2884 }
2885 }
2886 }
2887
2888 // Find the value in Other that overlaps VNI->def, if any.
2889 LiveQueryResult OtherLRQ = Other.LR.Query(VNI->def);
2890
2891 // It is possible that both values are defined by the same instruction, or
2892 // the values are PHIs defined in the same block. When that happens, the two
2893 // values should be merged into one, but not into any preceding value.
2894 // The first value defined or visited gets CR_Keep, the other gets CR_Merge.
2895 if (VNInfo *OtherVNI = OtherLRQ.valueDefined()) {
2896 assert(SlotIndex::isSameInstr(VNI->def, OtherVNI->def) && "Broken LRQ");
2897
2898 // One value stays, the other is merged. Keep the earlier one, or the first
2899 // one we see.
2900 if (OtherVNI->def < VNI->def)
2901 Other.computeAssignment(OtherVNI->id, *this);
2902 else if (VNI->def < OtherVNI->def && OtherLRQ.valueIn()) {
2903 // This is an early-clobber def overlapping a live-in value in the other
2904 // register. Not mergeable.
2905 V.OtherVNI = OtherLRQ.valueIn();
2906 return CR_Impossible;
2907 }
2908 V.OtherVNI = OtherVNI;
2909 Val &OtherV = Other.Vals[OtherVNI->id];
2910 // Keep this value, check for conflicts when analyzing OtherVNI. Avoid
2911 // revisiting OtherVNI->id in JoinVals::computeAssignment() below before it
2912 // is assigned.
2913 if (!OtherV.isAnalyzed() || Other.Assignments[OtherVNI->id] == -1)
2914 return CR_Keep;
2915 // Both sides have been analyzed now.
2916 // Allow overlapping PHI values. Any real interference would show up in a
2917 // predecessor, the PHI itself can't introduce any conflicts.
2918 if (VNI->isPHIDef())
2919 return CR_Merge;
2920 if ((V.ValidLanes & OtherV.ValidLanes).any())
2921 // Overlapping lanes can't be resolved.
2922 return CR_Impossible;
2923 return CR_Merge;
2924 }
2925
2926 // No simultaneous def. Is Other live at the def?
2927 V.OtherVNI = OtherLRQ.valueIn();
2928 if (!V.OtherVNI)
2929 // No overlap, no conflict.
2930 return CR_Keep;
2931
2932 assert(!SlotIndex::isSameInstr(VNI->def, V.OtherVNI->def) && "Broken LRQ");
2933
2934 // We have overlapping values, or possibly a kill of Other.
2935 // Recursively compute assignments up the dominator tree.
2936 Other.computeAssignment(V.OtherVNI->id, *this);
2937 Val &OtherV = Other.Vals[V.OtherVNI->id];
2938
2939 if (OtherV.ErasableImplicitDef) {
2940 // Check if OtherV is an IMPLICIT_DEF that extends beyond its basic block.
2941 // This shouldn't normally happen, but ProcessImplicitDefs can leave such
2942 // IMPLICIT_DEF instructions behind, and there is nothing wrong with it
2943 // technically.
2944 //
2945 // When it happens, treat that IMPLICIT_DEF as a normal value, and don't try
2946 // to erase the IMPLICIT_DEF instruction.
2947 //
2948 // Additionally we must keep an IMPLICIT_DEF if we're redefining an incoming
2949 // value.
2950
2951 MachineInstr *OtherImpDef =
2952 Indexes->getInstructionFromIndex(V.OtherVNI->def);
2953 MachineBasicBlock *OtherMBB = OtherImpDef->getParent();
2954 if (DefMI &&
2955 (DefMI->getParent() != OtherMBB || LIS->isLiveInToMBB(LR, OtherMBB))) {
2956 LLVM_DEBUG(dbgs() << "IMPLICIT_DEF defined at " << V.OtherVNI->def
2957 << " extends into "
2959 << ", keeping it.\n");
2960 OtherV.mustKeepImplicitDef(*TRI, *OtherImpDef);
2961 } else if (OtherMBB->hasEHPadSuccessor()) {
2962 // If OtherV is defined in a basic block that has EH pad successors then
2963 // we get the same problem not just if OtherV is live beyond its basic
2964 // block, but beyond the last call instruction in its basic block. Handle
2965 // this case conservatively.
2966 LLVM_DEBUG(
2967 dbgs() << "IMPLICIT_DEF defined at " << V.OtherVNI->def
2968 << " may be live into EH pad successors, keeping it.\n");
2969 OtherV.mustKeepImplicitDef(*TRI, *OtherImpDef);
2970 } else {
2971 // We deferred clearing these lanes in case we needed to save them
2972 OtherV.ValidLanes &= ~OtherV.WriteLanes;
2973 }
2974 }
2975
2976 // Allow overlapping PHI values. Any real interference would show up in a
2977 // predecessor, the PHI itself can't introduce any conflicts.
2978 if (VNI->isPHIDef())
2979 return CR_Replace;
2980
2981 // Check for simple erasable conflicts.
2982 if (DefMI->isImplicitDef())
2983 return CR_Erase;
2984
2985 // Include the non-conflict where DefMI is a coalescable copy that kills
2986 // OtherVNI. We still want the copy erased and value numbers merged.
2987 if (CP.isCoalescable(DefMI)) {
2988 // Some of the lanes copied from OtherVNI may be undef, making them undef
2989 // here too.
2990 V.ValidLanes &= ~V.WriteLanes | OtherV.ValidLanes;
2991 return CR_Erase;
2992 }
2993
2994 // This may not be a real conflict if DefMI simply kills Other and defines
2995 // VNI.
2996 if (OtherLRQ.isKill() && OtherLRQ.endPoint() <= VNI->def)
2997 return CR_Keep;
2998
2999 // Handle the case where VNI and OtherVNI can be proven to be identical:
3000 //
3001 // %other = COPY %ext
3002 // %this = COPY %ext <-- Erase this copy
3003 //
3004 if (DefMI->isFullCopy() && !CP.isPartial() &&
3005 valuesIdentical(VNI, V.OtherVNI, Other)) {
3006 V.Identical = true;
3007 return CR_Erase;
3008 }
3009
3010 // The remaining checks apply to the lanes, which aren't tracked here. This
3011 // was already decided to be OK via the following CR_Replace condition.
3012 // CR_Replace.
3013 if (SubRangeJoin)
3014 return CR_Replace;
3015
3016 // If the lanes written by this instruction were all undef in OtherVNI, it is
3017 // still safe to join the live ranges. This can't be done with a simple value
3018 // mapping, though - OtherVNI will map to multiple values:
3019 //
3020 // 1 %dst:ssub0 = FOO <-- OtherVNI
3021 // 2 %src = BAR <-- VNI
3022 // 3 %dst:ssub1 = COPY killed %src <-- Eliminate this copy.
3023 // 4 BAZ killed %dst
3024 // 5 QUUX killed %src
3025 //
3026 // Here OtherVNI will map to itself in [1;2), but to VNI in [2;5). CR_Replace
3027 // handles this complex value mapping.
3028 if ((V.WriteLanes & OtherV.ValidLanes).none())
3029 return CR_Replace;
3030
3031 // If the other live range is killed by DefMI and the live ranges are still
3032 // overlapping, it must be because we're looking at an early clobber def:
3033 //
3034 // %dst<def,early-clobber> = ASM killed %src
3035 //
3036 // In this case, it is illegal to merge the two live ranges since the early
3037 // clobber def would clobber %src before it was read.
3038 if (OtherLRQ.isKill()) {
3039 // This case where the def doesn't overlap the kill is handled above.
3040 assert(VNI->def.isEarlyClobber() &&
3041 "Only early clobber defs can overlap a kill");
3042 return CR_Impossible;
3043 }
3044
3045 // VNI is clobbering live lanes in OtherVNI, but there is still the
3046 // possibility that no instructions actually read the clobbered lanes.
3047 // If we're clobbering all the lanes in OtherVNI, at least one must be read.
3048 // Otherwise Other.RI wouldn't be live here.
3049 if ((TRI->getSubRegIndexLaneMask(Other.SubIdx) & ~V.WriteLanes).none())
3050 return CR_Impossible;
3051
3052 if (TrackSubRegLiveness) {
3053 auto &OtherLI = LIS->getInterval(Other.Reg);
3054 // If OtherVNI does not have subranges, it means all the lanes of OtherVNI
3055 // share the same live range, so we just need to check whether they have
3056 // any conflict bit in their LaneMask.
3057 if (!OtherLI.hasSubRanges()) {
3058 LaneBitmask OtherMask = TRI->getSubRegIndexLaneMask(Other.SubIdx);
3059 return (OtherMask & V.WriteLanes).none() ? CR_Replace : CR_Impossible;
3060 }
3061
3062 // If we are clobbering some active lanes of OtherVNI at VNI->def, it is
3063 // impossible to resolve the conflict. Otherwise, we can just replace
3064 // OtherVNI because of no real conflict.
3065 for (LiveInterval::SubRange &OtherSR : OtherLI.subranges()) {
3066 LaneBitmask OtherMask =
3067 TRI->composeSubRegIndexLaneMask(Other.SubIdx, OtherSR.LaneMask);
3068 if ((OtherMask & V.WriteLanes).none())
3069 continue;
3070
3071 auto OtherSRQ = OtherSR.Query(VNI->def);
3072 if (OtherSRQ.valueIn() && OtherSRQ.endPoint() > VNI->def) {
3073 // VNI is clobbering some lanes of OtherVNI, they have real conflict.
3074 return CR_Impossible;
3075 }
3076 }
3077
3078 // VNI is NOT clobbering any lane of OtherVNI, just replace OtherVNI.
3079 return CR_Replace;
3080 }
3081
3082 // We need to verify that no instructions are reading the clobbered lanes.
3083 // To save compile time, we'll only check that locally. Don't allow the
3084 // tainted value to escape the basic block.
3085 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def);
3086 if (OtherLRQ.endPoint() >= Indexes->getMBBEndIdx(MBB))
3087 return CR_Impossible;
3088
3089 // There are still some things that could go wrong besides clobbered lanes
3090 // being read, for example OtherVNI may be only partially redefined in MBB,
3091 // and some clobbered lanes could escape the block. Save this analysis for
3092 // resolveConflicts() when all values have been mapped. We need to know
3093 // RedefVNI and WriteLanes for any later defs in MBB, and we can't compute
3094 // that now - the recursive analyzeValue() calls must go upwards in the
3095 // dominator tree.
3096 return CR_Unresolved;
3097}
3098
3099void JoinVals::computeAssignment(unsigned ValNo, JoinVals &Other) {
3100 Val &V = Vals[ValNo];
3101 if (V.isAnalyzed()) {
3102 // Recursion should always move up the dominator tree, so ValNo is not
3103 // supposed to reappear before it has been assigned.
3104 assert(Assignments[ValNo] != -1 && "Bad recursion?");
3105 return;
3106 }
3107 switch ((V.Resolution = analyzeValue(ValNo, Other))) {
3108 case CR_Erase:
3109 case CR_Merge:
3110 // Merge this ValNo into OtherVNI.
3111 assert(V.OtherVNI && "OtherVNI not assigned, can't merge.");
3112 assert(Other.Vals[V.OtherVNI->id].isAnalyzed() && "Missing recursion");
3113 Assignments[ValNo] = Other.Assignments[V.OtherVNI->id];
3114 LLVM_DEBUG(dbgs() << "\t\tmerge " << printReg(Reg) << ':' << ValNo << '@'
3115 << LR.getValNumInfo(ValNo)->def << " into "
3116 << printReg(Other.Reg) << ':' << V.OtherVNI->id << '@'
3117 << V.OtherVNI->def << " --> @"
3118 << NewVNInfo[Assignments[ValNo]]->def << '\n');
3119 break;
3120 case CR_Replace:
3121 case CR_Unresolved: {
3122 // The other value is going to be pruned if this join is successful.
3123 assert(V.OtherVNI && "OtherVNI not assigned, can't prune");
3124 Val &OtherV = Other.Vals[V.OtherVNI->id];
3125 OtherV.Pruned = true;
3126 [[fallthrough]];
3127 }
3128 default:
3129 // This value number needs to go in the final joined live range.
3130 Assignments[ValNo] = NewVNInfo.size();
3131 NewVNInfo.push_back(LR.getValNumInfo(ValNo));
3132 break;
3133 }
3134}
3135
3136bool JoinVals::mapValues(JoinVals &Other) {
3137 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3138 computeAssignment(i, Other);
3139 if (Vals[i].Resolution == CR_Impossible) {
3140 LLVM_DEBUG(dbgs() << "\t\tinterference at " << printReg(Reg) << ':' << i
3141 << '@' << LR.getValNumInfo(i)->def << '\n');
3142 return false;
3143 }
3144 }
3145 return true;
3146}
3147
3148bool JoinVals::taintExtent(
3149 unsigned ValNo, LaneBitmask TaintedLanes, JoinVals &Other,
3150 SmallVectorImpl<std::pair<SlotIndex, LaneBitmask>> &TaintExtent) {
3151 VNInfo *VNI = LR.getValNumInfo(ValNo);
3152 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def);
3153 SlotIndex MBBEnd = Indexes->getMBBEndIdx(MBB);
3154
3155 // Scan Other.LR from VNI.def to MBBEnd.
3156 LiveInterval::iterator OtherI = Other.LR.find(VNI->def);
3157 assert(OtherI != Other.LR.end() && "No conflict?");
3158 do {
3159 // OtherI is pointing to a tainted value. Abort the join if the tainted
3160 // lanes escape the block.
3161 SlotIndex End = OtherI->end;
3162 if (End >= MBBEnd) {
3163 LLVM_DEBUG(dbgs() << "\t\ttaints global " << printReg(Other.Reg) << ':'
3164 << OtherI->valno->id << '@' << OtherI->start << '\n');
3165 return false;
3166 }
3167 LLVM_DEBUG(dbgs() << "\t\ttaints local " << printReg(Other.Reg) << ':'
3168 << OtherI->valno->id << '@' << OtherI->start << " to "
3169 << End << '\n');
3170 // A dead def is not a problem.
3171 if (End.isDead())
3172 break;
3173 TaintExtent.push_back(std::make_pair(End, TaintedLanes));
3174
3175 // Check for another def in the MBB.
3176 if (++OtherI == Other.LR.end() || OtherI->start >= MBBEnd)
3177 break;
3178
3179 // Lanes written by the new def are no longer tainted.
3180 const Val &OV = Other.Vals[OtherI->valno->id];
3181 TaintedLanes &= ~OV.WriteLanes;
3182 if (!OV.RedefVNI)
3183 break;
3184 } while (TaintedLanes.any());
3185 return true;
3186}
3187
3188bool JoinVals::usesLanes(const MachineInstr &MI, Register Reg, unsigned SubIdx,
3189 LaneBitmask Lanes) const {
3190 if (MI.isDebugOrPseudoInstr())
3191 return false;
3192 for (const MachineOperand &MO : MI.all_uses()) {
3193 if (MO.getReg() != Reg)
3194 continue;
3195 if (!MO.readsReg())
3196 continue;
3197 unsigned S = TRI->composeSubRegIndices(SubIdx, MO.getSubReg());
3198 if ((Lanes & TRI->getSubRegIndexLaneMask(S)).any())
3199 return true;
3200 }
3201 return false;
3202}
3203
3204bool JoinVals::resolveConflicts(JoinVals &Other) {
3205 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3206 Val &V = Vals[i];
3207 assert(V.Resolution != CR_Impossible && "Unresolvable conflict");
3208 if (V.Resolution != CR_Unresolved)
3209 continue;
3210 LLVM_DEBUG(dbgs() << "\t\tconflict at " << printReg(Reg) << ':' << i << '@'
3211 << LR.getValNumInfo(i)->def << ' '
3212 << PrintLaneMask(LaneMask) << '\n');
3213 if (SubRangeJoin)
3214 return false;
3215
3216 ++NumLaneConflicts;
3217 assert(V.OtherVNI && "Inconsistent conflict resolution.");
3218 VNInfo *VNI = LR.getValNumInfo(i);
3219 const Val &OtherV = Other.Vals[V.OtherVNI->id];
3220
3221 // VNI is known to clobber some lanes in OtherVNI. If we go ahead with the
3222 // join, those lanes will be tainted with a wrong value. Get the extent of
3223 // the tainted lanes.
3224 LaneBitmask TaintedLanes = V.WriteLanes & OtherV.ValidLanes;
3226 if (!taintExtent(i, TaintedLanes, Other, TaintExtent))
3227 // Tainted lanes would extend beyond the basic block.
3228 return false;
3229
3230 assert(!TaintExtent.empty() && "There should be at least one conflict.");
3231
3232 // Now look at the instructions from VNI->def to TaintExtent (inclusive).
3233 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def);
3235 if (!VNI->isPHIDef()) {
3236 MI = Indexes->getInstructionFromIndex(VNI->def);
3237 if (!VNI->def.isEarlyClobber()) {
3238 // No need to check the instruction defining VNI for reads.
3239 ++MI;
3240 }
3241 }
3242 assert(!SlotIndex::isSameInstr(VNI->def, TaintExtent.front().first) &&
3243 "Interference ends on VNI->def. Should have been handled earlier");
3244 MachineInstr *LastMI =
3245 Indexes->getInstructionFromIndex(TaintExtent.front().first);
3246 assert(LastMI && "Range must end at a proper instruction");
3247 unsigned TaintNum = 0;
3248 while (true) {
3249 assert(MI != MBB->end() && "Bad LastMI");
3250 if (usesLanes(*MI, Other.Reg, Other.SubIdx, TaintedLanes)) {
3251 LLVM_DEBUG(dbgs() << "\t\ttainted lanes used by: " << *MI);
3252 return false;
3253 }
3254 // LastMI is the last instruction to use the current value.
3255 if (&*MI == LastMI) {
3256 if (++TaintNum == TaintExtent.size())
3257 break;
3258 LastMI = Indexes->getInstructionFromIndex(TaintExtent[TaintNum].first);
3259 assert(LastMI && "Range must end at a proper instruction");
3260 TaintedLanes = TaintExtent[TaintNum].second;
3261 }
3262 ++MI;
3263 }
3264
3265 // The tainted lanes are unused.
3266 V.Resolution = CR_Replace;
3267 ++NumLaneResolves;
3268 }
3269 return true;
3270}
3271
3272bool JoinVals::isPrunedValue(unsigned ValNo, JoinVals &Other) {
3273 Val &V = Vals[ValNo];
3274 if (V.Pruned || V.PrunedComputed)
3275 return V.Pruned;
3276
3277 if (V.Resolution != CR_Erase && V.Resolution != CR_Merge)
3278 return V.Pruned;
3279
3280 // Follow copies up the dominator tree and check if any intermediate value
3281 // has been pruned.
3282 V.PrunedComputed = true;
3283 V.Pruned = Other.isPrunedValue(V.OtherVNI->id, *this);
3284 return V.Pruned;
3285}
3286
3287void JoinVals::pruneValues(JoinVals &Other,
3288 SmallVectorImpl<SlotIndex> &EndPoints,
3289 bool changeInstrs) {
3290 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3291 SlotIndex Def = LR.getValNumInfo(i)->def;
3292 switch (Vals[i].Resolution) {
3293 case CR_Keep:
3294 break;
3295 case CR_Replace: {
3296 // This value takes precedence over the value in Other.LR.
3297 LIS->pruneValue(Other.LR, Def, &EndPoints);
3298 // Check if we're replacing an IMPLICIT_DEF value. The IMPLICIT_DEF
3299 // instructions are only inserted to provide a live-out value for PHI
3300 // predecessors, so the instruction should simply go away once its value
3301 // has been replaced.
3302 Val &OtherV = Other.Vals[Vals[i].OtherVNI->id];
3303 bool EraseImpDef =
3304 OtherV.ErasableImplicitDef && OtherV.Resolution == CR_Keep;
3305 if (!Def.isBlock()) {
3306 if (changeInstrs) {
3307 // Remove <def,read-undef> flags. This def is now a partial redef.
3308 // Also remove dead flags since the joined live range will
3309 // continue past this instruction.
3310 for (MachineOperand &MO :
3311 Indexes->getInstructionFromIndex(Def)->all_defs()) {
3312 if (MO.getReg() == Reg) {
3313 if (MO.getSubReg() != 0 && MO.isUndef() && !EraseImpDef)
3314 MO.setIsUndef(false);
3315 MO.setIsDead(false);
3316 }
3317 }
3318 }
3319 // This value will reach instructions below, but we need to make sure
3320 // the live range also reaches the instruction at Def.
3321 if (!EraseImpDef)
3322 EndPoints.push_back(Def);
3323 }
3324 LLVM_DEBUG(dbgs() << "\t\tpruned " << printReg(Other.Reg) << " at " << Def
3325 << ": " << Other.LR << '\n');
3326 break;
3327 }
3328 case CR_Erase:
3329 case CR_Merge:
3330 if (isPrunedValue(i, Other)) {
3331 // This value is ultimately a copy of a pruned value in LR or Other.LR.
3332 // We can no longer trust the value mapping computed by
3333 // computeAssignment(), the value that was originally copied could have
3334 // been replaced.
3335 LIS->pruneValue(LR, Def, &EndPoints);
3336 LLVM_DEBUG(dbgs() << "\t\tpruned all of " << printReg(Reg) << " at "
3337 << Def << ": " << LR << '\n');
3338 }
3339 break;
3340 case CR_Unresolved:
3341 case CR_Impossible:
3342 llvm_unreachable("Unresolved conflicts");
3343 }
3344 }
3345}
3346
3347// Check if the segment consists of a copied live-through value (i.e. the copy
3348// in the block only extended the liveness, of an undef value which we may need
3349// to handle).
3350static bool isLiveThrough(const LiveQueryResult Q) {
3351 return Q.valueIn() && Q.valueIn()->isPHIDef() && Q.valueIn() == Q.valueOut();
3352}
3353
3354/// Consider the following situation when coalescing the copy between
3355/// %31 and %45 at 800. (The vertical lines represent live range segments.)
3356///
3357/// Main range Subrange 0004 (sub2)
3358/// %31 %45 %31 %45
3359/// 544 %45 = COPY %28 + +
3360/// | v1 | v1
3361/// 560B bb.1: + +
3362/// 624 = %45.sub2 | v2 | v2
3363/// 800 %31 = COPY %45 + + + +
3364/// | v0 | v0
3365/// 816 %31.sub1 = ... + |
3366/// 880 %30 = COPY %31 | v1 +
3367/// 928 %45 = COPY %30 | + +
3368/// | | v0 | v0 <--+
3369/// 992B ; backedge -> bb.1 | + + |
3370/// 1040 = %31.sub0 + |
3371/// This value must remain
3372/// live-out!
3373///
3374/// Assuming that %31 is coalesced into %45, the copy at 928 becomes
3375/// redundant, since it copies the value from %45 back into it. The
3376/// conflict resolution for the main range determines that %45.v0 is
3377/// to be erased, which is ok since %31.v1 is identical to it.
3378/// The problem happens with the subrange for sub2: it has to be live
3379/// on exit from the block, but since 928 was actually a point of
3380/// definition of %45.sub2, %45.sub2 was not live immediately prior
3381/// to that definition. As a result, when 928 was erased, the value v0
3382/// for %45.sub2 was pruned in pruneSubRegValues. Consequently, an
3383/// IMPLICIT_DEF was inserted as a "backedge" definition for %45.sub2,
3384/// providing an incorrect value to the use at 624.
3385///
3386/// Since the main-range values %31.v1 and %45.v0 were proved to be
3387/// identical, the corresponding values in subranges must also be the
3388/// same. A redundant copy is removed because it's not needed, and not
3389/// because it copied an undefined value, so any liveness that originated
3390/// from that copy cannot disappear. When pruning a value that started
3391/// at the removed copy, the corresponding identical value must be
3392/// extended to replace it.
3393void JoinVals::pruneSubRegValues(LiveInterval &LI, LaneBitmask &ShrinkMask) {
3394 // Look for values being erased.
3395 bool DidPrune = false;
3396 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3397 Val &V = Vals[i];
3398 // We should trigger in all cases in which eraseInstrs() does something.
3399 // match what eraseInstrs() is doing, print a message so
3400 if (V.Resolution != CR_Erase &&
3401 (V.Resolution != CR_Keep || !V.ErasableImplicitDef || !V.Pruned))
3402 continue;
3403
3404 // Check subranges at the point where the copy will be removed.
3405 SlotIndex Def = LR.getValNumInfo(i)->def;
3406 SlotIndex OtherDef;
3407 if (V.Identical)
3408 OtherDef = V.OtherVNI->def;
3409
3410 // Print message so mismatches with eraseInstrs() can be diagnosed.
3411 LLVM_DEBUG(dbgs() << "\t\tExpecting instruction removal at " << Def
3412 << '\n');
3413 for (LiveInterval::SubRange &S : LI.subranges()) {
3414 LiveQueryResult Q = S.Query(Def);
3415
3416 // If a subrange starts at the copy then an undefined value has been
3417 // copied and we must remove that subrange value as well.
3418 VNInfo *ValueOut = Q.valueOutOrDead();
3419 if (ValueOut != nullptr &&
3420 (Q.valueIn() == nullptr ||
3421 (V.Identical && V.Resolution == CR_Erase && ValueOut->def == Def))) {
3422 LLVM_DEBUG(dbgs() << "\t\tPrune sublane " << PrintLaneMask(S.LaneMask)
3423 << " at " << Def << "\n");
3424 SmallVector<SlotIndex, 8> EndPoints;
3425 LIS->pruneValue(S, Def, &EndPoints);
3426 DidPrune = true;
3427 // Mark value number as unused.
3428 ValueOut->markUnused();
3429
3430 if (V.Identical && S.Query(OtherDef).valueOutOrDead()) {
3431 // If V is identical to V.OtherVNI (and S was live at OtherDef),
3432 // then we can't simply prune V from S. V needs to be replaced
3433 // with V.OtherVNI.
3434 LIS->extendToIndices(S, EndPoints);
3435 }
3436
3437 // We may need to eliminate the subrange if the copy introduced a live
3438 // out undef value.
3439 if (ValueOut->isPHIDef())
3440 ShrinkMask |= S.LaneMask;
3441 continue;
3442 }
3443
3444 // If a subrange ends at the copy, then a value was copied but only
3445 // partially used later. Shrink the subregister range appropriately.
3446 //
3447 // Ultimately this calls shrinkToUses, so assuming ShrinkMask is
3448 // conservatively correct.
3449 if ((Q.valueIn() != nullptr && Q.valueOut() == nullptr) ||
3450 (V.Resolution == CR_Erase && isLiveThrough(Q))) {
3451 LLVM_DEBUG(dbgs() << "\t\tDead uses at sublane "
3452 << PrintLaneMask(S.LaneMask) << " at " << Def
3453 << "\n");
3454 ShrinkMask |= S.LaneMask;
3455 }
3456 }
3457 }
3458 if (DidPrune)
3460}
3461
3462/// Check if any of the subranges of @p LI contain a definition at @p Def.
3464 for (LiveInterval::SubRange &SR : LI.subranges()) {
3465 if (VNInfo *VNI = SR.Query(Def).valueOutOrDead())
3466 if (VNI->def == Def)
3467 return true;
3468 }
3469 return false;
3470}
3471
3472void JoinVals::pruneMainSegments(LiveInterval &LI, bool &ShrinkMainRange) {
3473 assert(&static_cast<LiveRange &>(LI) == &LR);
3474
3475 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3476 if (Vals[i].Resolution != CR_Keep)
3477 continue;
3478 VNInfo *VNI = LR.getValNumInfo(i);
3479 if (VNI->isUnused() || VNI->isPHIDef() || isDefInSubRange(LI, VNI->def))
3480 continue;
3481 Vals[i].Pruned = true;
3482 ShrinkMainRange = true;
3483 }
3484}
3485
3486void JoinVals::removeImplicitDefs() {
3487 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3488 Val &V = Vals[i];
3489 if (V.Resolution != CR_Keep || !V.ErasableImplicitDef || !V.Pruned)
3490 continue;
3491
3492 VNInfo *VNI = LR.getValNumInfo(i);
3493 VNI->markUnused();
3494 LR.removeValNo(VNI);
3495 }
3496}
3497
3498void JoinVals::eraseInstrs(SmallPtrSetImpl<MachineInstr *> &ErasedInstrs,
3499 SmallVectorImpl<Register> &ShrinkRegs,
3500 LiveInterval *LI) {
3501 for (unsigned i = 0, e = LR.getNumValNums(); i != e; ++i) {
3502 // Get the def location before markUnused() below invalidates it.
3503 VNInfo *VNI = LR.getValNumInfo(i);
3504 SlotIndex Def = VNI->def;
3505 switch (Vals[i].Resolution) {
3506 case CR_Keep: {
3507 // If an IMPLICIT_DEF value is pruned, it doesn't serve a purpose any
3508 // longer. The IMPLICIT_DEF instructions are only inserted by
3509 // PHIElimination to guarantee that all PHI predecessors have a value.
3510 if (!Vals[i].ErasableImplicitDef || !Vals[i].Pruned)
3511 break;
3512 // Remove value number i from LR.
3513 // For intervals with subranges, removing a segment from the main range
3514 // may require extending the previous segment: for each definition of
3515 // a subregister, there will be a corresponding def in the main range.
3516 // That def may fall in the middle of a segment from another subrange.
3517 // In such cases, removing this def from the main range must be
3518 // complemented by extending the main range to account for the liveness
3519 // of the other subrange.
3520 // The new end point of the main range segment to be extended.
3521 SlotIndex NewEnd;
3522 if (LI != nullptr) {
3524 assert(I != LR.end());
3525 // Do not extend beyond the end of the segment being removed.
3526 // The segment may have been pruned in preparation for joining
3527 // live ranges.
3528 NewEnd = I->end;
3529 }
3530
3531 LR.removeValNo(VNI);
3532 // Note that this VNInfo is reused and still referenced in NewVNInfo,
3533 // make it appear like an unused value number.
3534 VNI->markUnused();
3535
3536 if (LI != nullptr && LI->hasSubRanges()) {
3537 assert(static_cast<LiveRange *>(LI) == &LR);
3538 // Determine the end point based on the subrange information:
3539 // minimum of (earliest def of next segment,
3540 // latest end point of containing segment)
3541 SlotIndex ED, LE;
3542 for (LiveInterval::SubRange &SR : LI->subranges()) {
3543 LiveRange::iterator I = SR.find(Def);
3544 if (I == SR.end())
3545 continue;
3546 if (I->start > Def)
3547 ED = ED.isValid() ? std::min(ED, I->start) : I->start;
3548 else
3549 LE = LE.isValid() ? std::max(LE, I->end) : I->end;
3550 }
3551 if (LE.isValid())
3552 NewEnd = std::min(NewEnd, LE);
3553 if (ED.isValid())
3554 NewEnd = std::min(NewEnd, ED);
3555
3556 // We only want to do the extension if there was a subrange that
3557 // was live across Def.
3558 if (LE.isValid()) {
3559 LiveRange::iterator S = LR.find(Def);
3560 if (S != LR.begin())
3561 std::prev(S)->end = NewEnd;
3562 }
3563 }
3564 LLVM_DEBUG({
3565 dbgs() << "\t\tremoved " << i << '@' << Def << ": " << LR << '\n';
3566 if (LI != nullptr)
3567 dbgs() << "\t\t LHS = " << *LI << '\n';
3568 });
3569 [[fallthrough]];
3570 }
3571
3572 case CR_Erase: {
3573 MachineInstr *MI = Indexes->getInstructionFromIndex(Def);
3574 assert(MI && "No instruction to erase");
3575 if (MI->isCopy()) {
3576 Register Reg = MI->getOperand(1).getReg();
3577 if (Reg.isVirtual() && Reg != CP.getSrcReg() && Reg != CP.getDstReg())
3578 ShrinkRegs.push_back(Reg);
3579 }
3580 ErasedInstrs.insert(MI);
3581 LLVM_DEBUG(dbgs() << "\t\terased:\t" << Def << '\t' << *MI);
3583 MI->eraseFromParent();
3584 break;
3585 }
3586 default:
3587 break;
3588 }
3589 }
3590}
3591
3592void RegisterCoalescer::joinSubRegRanges(LiveRange &LRange, LiveRange &RRange,
3593 LaneBitmask LaneMask,
3594 const CoalescerPair &CP) {
3595 SmallVector<VNInfo *, 16> NewVNInfo;
3596 JoinVals RHSVals(RRange, CP.getSrcReg(), CP.getSrcIdx(), LaneMask, NewVNInfo,
3597 CP, LIS, TRI, true, true);
3598 JoinVals LHSVals(LRange, CP.getDstReg(), CP.getDstIdx(), LaneMask, NewVNInfo,
3599 CP, LIS, TRI, true, true);
3600
3601 // Compute NewVNInfo and resolve conflicts (see also joinVirtRegs())
3602 // We should be able to resolve all conflicts here as we could successfully do
3603 // it on the mainrange already. There is however a problem when multiple
3604 // ranges get mapped to the "overflow" lane mask bit which creates unexpected
3605 // interferences.
3606 if (!LHSVals.mapValues(RHSVals) || !RHSVals.mapValues(LHSVals)) {
3607 // We already determined that it is legal to merge the intervals, so this
3608 // should never fail.
3609 llvm_unreachable("*** Couldn't join subrange!\n");
3610 }
3611 if (!LHSVals.resolveConflicts(RHSVals) ||
3612 !RHSVals.resolveConflicts(LHSVals)) {
3613 // We already determined that it is legal to merge the intervals, so this
3614 // should never fail.
3615 llvm_unreachable("*** Couldn't join subrange!\n");
3616 }
3617
3618 // The merging algorithm in LiveInterval::join() can't handle conflicting
3619 // value mappings, so we need to remove any live ranges that overlap a
3620 // CR_Replace resolution. Collect a set of end points that can be used to
3621 // restore the live range after joining.
3622 SmallVector<SlotIndex, 8> EndPoints;
3623 LHSVals.pruneValues(RHSVals, EndPoints, false);
3624 RHSVals.pruneValues(LHSVals, EndPoints, false);
3625
3626 LHSVals.removeImplicitDefs();
3627 RHSVals.removeImplicitDefs();
3628
3629 assert(LRange.verify() && RRange.verify());
3630
3631 // Join RRange into LHS.
3632 LRange.join(RRange, LHSVals.getAssignments(), RHSVals.getAssignments(),
3633 NewVNInfo);
3634
3635 LLVM_DEBUG(dbgs() << "\t\tjoined lanes: " << PrintLaneMask(LaneMask) << ' '
3636 << LRange << "\n");
3637 if (EndPoints.empty())
3638 return;
3639
3640 // Recompute the parts of the live range we had to remove because of
3641 // CR_Replace conflicts.
3642 LLVM_DEBUG({
3643 dbgs() << "\t\trestoring liveness to " << EndPoints.size() << " points: ";
3644 for (unsigned i = 0, n = EndPoints.size(); i != n; ++i) {
3645 dbgs() << EndPoints[i];
3646 if (i != n - 1)
3647 dbgs() << ',';
3648 }
3649 dbgs() << ": " << LRange << '\n';
3650 });
3651 LIS->extendToIndices(LRange, EndPoints);
3652}
3653
3654void RegisterCoalescer::mergeSubRangeInto(LiveInterval &LI,
3655 const LiveRange &ToMerge,
3656 LaneBitmask LaneMask,
3657 CoalescerPair &CP,
3658 unsigned ComposeSubRegIdx) {
3660 LI.refineSubRanges(
3661 Allocator, LaneMask,
3662 [this, &Allocator, &ToMerge, &CP](LiveInterval::SubRange &SR) {
3663 if (SR.empty()) {
3664 SR.assign(ToMerge, Allocator);
3665 } else {
3666 // joinSubRegRange() destroys the merged range, so we need a copy.
3667 LiveRange RangeCopy(ToMerge, Allocator);
3668 joinSubRegRanges(SR, RangeCopy, SR.LaneMask, CP);
3669 }
3670 },
3671 *LIS->getSlotIndexes(), *TRI, ComposeSubRegIdx);
3672}
3673
3674bool RegisterCoalescer::isHighCostLiveInterval(LiveInterval &LI) {
3676 return false;
3677 auto &Counter = LargeLIVisitCounter[LI.reg()];
3678 if (Counter < LargeIntervalFreqThreshold) {
3679 Counter++;
3680 return false;
3681 }
3682 return true;
3683}
3684
3685bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP) {
3686 SmallVector<VNInfo *, 16> NewVNInfo;
3687 LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
3688 LiveInterval &LHS = LIS->getInterval(CP.getDstReg());
3689 bool TrackSubRegLiveness = MRI->shouldTrackSubRegLiveness(*CP.getNewRC());
3690 JoinVals RHSVals(RHS, CP.getSrcReg(), CP.getSrcIdx(), LaneBitmask::getNone(),
3691 NewVNInfo, CP, LIS, TRI, false, TrackSubRegLiveness);
3692 JoinVals LHSVals(LHS, CP.getDstReg(), CP.getDstIdx(), LaneBitmask::getNone(),
3693 NewVNInfo, CP, LIS, TRI, false, TrackSubRegLiveness);
3694
3695 LLVM_DEBUG(dbgs() << "\t\tRHS = " << RHS << "\n\t\tLHS = " << LHS << '\n');
3696
3697 if (isHighCostLiveInterval(LHS) || isHighCostLiveInterval(RHS))
3698 return false;
3699
3700 // First compute NewVNInfo and the simple value mappings.
3701 // Detect impossible conflicts early.
3702 if (!LHSVals.mapValues(RHSVals) || !RHSVals.mapValues(LHSVals))
3703 return false;
3704
3705 // Some conflicts can only be resolved after all values have been mapped.
3706 if (!LHSVals.resolveConflicts(RHSVals) || !RHSVals.resolveConflicts(LHSVals))
3707 return false;
3708
3709 // All clear, the live ranges can be merged.
3710 if (RHS.hasSubRanges() || LHS.hasSubRanges()) {
3712
3713 // Transform lanemasks from the LHS to masks in the coalesced register and
3714 // create initial subranges if necessary.
3715 unsigned DstIdx = CP.getDstIdx();
3716 if (!LHS.hasSubRanges()) {
3717 LaneBitmask Mask = DstIdx == 0 ? CP.getNewRC()->getLaneMask()
3718 : TRI->getSubRegIndexLaneMask(DstIdx);
3719 // LHS must support subregs or we wouldn't be in this codepath.
3720 assert(Mask.any());
3721 LHS.createSubRangeFrom(Allocator, Mask, LHS);
3722 } else if (DstIdx != 0) {
3723 // Transform LHS lanemasks to new register class if necessary.
3724 for (LiveInterval::SubRange &R : LHS.subranges()) {
3725 LaneBitmask Mask = TRI->composeSubRegIndexLaneMask(DstIdx, R.LaneMask);
3726 R.LaneMask = Mask;
3727 }
3728 }
3729 LLVM_DEBUG(dbgs() << "\t\tLHST = " << printReg(CP.getDstReg()) << ' ' << LHS
3730 << '\n');
3731
3732 // Determine lanemasks of RHS in the coalesced register and merge subranges.
3733 unsigned SrcIdx = CP.getSrcIdx();
3734 if (!RHS.hasSubRanges()) {
3735 LaneBitmask Mask = SrcIdx == 0 ? CP.getNewRC()->getLaneMask()
3736 : TRI->getSubRegIndexLaneMask(SrcIdx);
3737 mergeSubRangeInto(LHS, RHS, Mask, CP, DstIdx);
3738 } else {
3739 // Pair up subranges and merge.
3740 for (LiveInterval::SubRange &R : RHS.subranges()) {
3741 LaneBitmask Mask = TRI->composeSubRegIndexLaneMask(SrcIdx, R.LaneMask);
3742 mergeSubRangeInto(LHS, R, Mask, CP, DstIdx);
3743 }
3744 }
3745 LLVM_DEBUG(dbgs() << "\tJoined SubRanges " << LHS << "\n");
3746
3747 // Pruning implicit defs from subranges may result in the main range
3748 // having stale segments.
3749 LHSVals.pruneMainSegments(LHS, ShrinkMainRange);
3750
3751 LHSVals.pruneSubRegValues(LHS, ShrinkMask);
3752 RHSVals.pruneSubRegValues(LHS, ShrinkMask);
3753 } else if (TrackSubRegLiveness && !CP.getDstIdx() && CP.getSrcIdx()) {
3754 LHS.createSubRangeFrom(LIS->getVNInfoAllocator(),
3755 CP.getNewRC()->getLaneMask(), LHS);
3756 mergeSubRangeInto(LHS, RHS, TRI->getSubRegIndexLaneMask(CP.getSrcIdx()), CP,
3757 CP.getDstIdx());
3758 LHSVals.pruneMainSegments(LHS, ShrinkMainRange);
3759 LHSVals.pruneSubRegValues(LHS, ShrinkMask);
3760 }
3761
3762 // The merging algorithm in LiveInterval::join() can't handle conflicting
3763 // value mappings, so we need to remove any live ranges that overlap a
3764 // CR_Replace resolution. Collect a set of end points that can be used to
3765 // restore the live range after joining.
3766 SmallVector<SlotIndex, 8> EndPoints;
3767 LHSVals.pruneValues(RHSVals, EndPoints, true);
3768 RHSVals.pruneValues(LHSVals, EndPoints, true);
3769
3770 // Erase COPY and IMPLICIT_DEF instructions. This may cause some external
3771 // registers to require trimming.
3772 SmallVector<Register, 8> ShrinkRegs;
3773 LHSVals.eraseInstrs(ErasedInstrs, ShrinkRegs, &LHS);
3774 RHSVals.eraseInstrs(ErasedInstrs, ShrinkRegs);
3775 while (!ShrinkRegs.empty())
3776 shrinkToUses(&LIS->getInterval(ShrinkRegs.pop_back_val()));
3777
3778 // Scan and mark undef any DBG_VALUEs that would refer to a different value.
3779 checkMergingChangesDbgValues(CP, LHS, LHSVals, RHS, RHSVals);
3780
3781 // If the RHS covers any PHI locations that were tracked for debug-info, we
3782 // must update tracking information to reflect the join.
3783 auto RegIt = RegToPHIIdx.find(CP.getSrcReg());
3784 if (RegIt != RegToPHIIdx.end()) {
3785 // Iterate over all the debug instruction numbers assigned this register.
3786 for (unsigned InstID : RegIt->second) {
3787 auto PHIIt = PHIValToPos.find(InstID);
3788 assert(PHIIt != PHIValToPos.end());
3789 const SlotIndex &SI = PHIIt->second.SI;
3790
3791 // Does the RHS cover the position of this PHI?
3792 auto LII = RHS.find(SI);
3793 if (LII == RHS.end() || LII->start > SI)
3794 continue;
3795
3796 // Accept two kinds of subregister movement:
3797 // * When we merge from one register class into a larger register:
3798 // %1:gr16 = some-inst
3799 // ->
3800 // %2:gr32.sub_16bit = some-inst
3801 // * When the PHI is already in a subregister, and the larger class
3802 // is coalesced:
3803 // %2:gr32.sub_16bit = some-inst
3804 // %3:gr32 = COPY %2
3805 // ->
3806 // %3:gr32.sub_16bit = some-inst
3807 // Test for subregister move:
3808 if (CP.getSrcIdx() != 0 || CP.getDstIdx() != 0)
3809 // If we're moving between different subregisters, ignore this join.
3810 // The PHI will not get a location, dropping variable locations.
3811 if (PHIIt->second.SubReg && PHIIt->second.SubReg != CP.getSrcIdx())
3812 continue;
3813
3814 // Update our tracking of where the PHI is.
3815 PHIIt->second.Reg = CP.getDstReg();
3816
3817 // If we merge into a sub-register of a larger class (test above),
3818 // update SubReg.
3819 if (CP.getSrcIdx() != 0)
3820 PHIIt->second.SubReg = CP.getSrcIdx();
3821 }
3822
3823 // Rebuild the register index in RegToPHIIdx to account for PHIs tracking
3824 // different VRegs now. Copy old collection of debug instruction numbers and
3825 // erase the old one:
3826 auto InstrNums = RegIt->second;
3827 RegToPHIIdx.erase(RegIt);
3828
3829 // There might already be PHIs being tracked in the destination VReg. Insert
3830 // into an existing tracking collection, or insert a new one.
3831 RegIt = RegToPHIIdx.find(CP.getDstReg());
3832 if (RegIt != RegToPHIIdx.end())
3833 llvm::append_range(RegIt->second, InstrNums);
3834 else
3835 RegToPHIIdx.insert({CP.getDstReg(), InstrNums});
3836 }
3837
3838 // Join RHS into LHS.
3839 LHS.join(RHS, LHSVals.getAssignments(), RHSVals.getAssignments(), NewVNInfo);
3840
3841 // Kill flags are going to be wrong if the live ranges were overlapping.
3842 // Eventually, we should simply clear all kill flags when computing live
3843 // ranges. They are reinserted after register allocation.
3844 MRI->clearKillFlags(LHS.reg());
3845 MRI->clearKillFlags(RHS.reg());
3846
3847 if (!EndPoints.empty()) {
3848 // Recompute the parts of the live range we had to remove because of
3849 // CR_Replace conflicts.
3850 LLVM_DEBUG({
3851 dbgs() << "\t\trestoring liveness to " << EndPoints.size() << " points: ";
3852 for (unsigned i = 0, n = EndPoints.size(); i != n; ++i) {
3853 dbgs() << EndPoints[i];
3854 if (i != n - 1)
3855 dbgs() << ',';
3856 }
3857 dbgs() << ": " << LHS << '\n';
3858 });
3859 LIS->extendToIndices((LiveRange &)LHS, EndPoints);
3860 }
3861
3862 return true;
3863}
3864
3865bool RegisterCoalescer::joinIntervals(CoalescerPair &CP) {
3866 return CP.isPhys() ? joinReservedPhysReg(CP) : joinVirtRegs(CP);
3867}
3868
3869void RegisterCoalescer::buildVRegToDbgValueMap(MachineFunction &MF) {
3870 const SlotIndexes &Slots = *LIS->getSlotIndexes();
3872
3873 // After collecting a block of DBG_VALUEs into ToInsert, enter them into the
3874 // vreg => DbgValueLoc map.
3875 auto CloseNewDVRange = [this, &ToInsert](SlotIndex Slot) {
3876 for (auto *X : ToInsert) {
3877 for (const auto &Op : X->debug_operands()) {
3878 if (Op.isReg() && Op.getReg().isVirtual())
3879 DbgVRegToValues[Op.getReg()].push_back({Slot, X});
3880 }
3881 }
3882
3883 ToInsert.clear();
3884 };
3885
3886 // Iterate over all instructions, collecting them into the ToInsert vector.
3887 // Once a non-debug instruction is found, record the slot index of the
3888 // collected DBG_VALUEs.
3889 for (auto &MBB : MF) {
3890 SlotIndex CurrentSlot = Slots.getMBBStartIdx(&MBB);
3891
3892 for (auto &MI : MBB) {
3893 if (MI.isDebugValue()) {
3894 if (any_of(MI.debug_operands(), [](const MachineOperand &MO) {
3895 return MO.isReg() && MO.getReg().isVirtual();
3896 }))
3897 ToInsert.push_back(&MI);
3898 } else if (!MI.isDebugOrPseudoInstr()) {
3899 CurrentSlot = Slots.getInstructionIndex(MI);
3900 CloseNewDVRange(CurrentSlot);
3901 }
3902 }
3903
3904 // Close range of DBG_VALUEs at the end of blocks.
3905 CloseNewDVRange(Slots.getMBBEndIdx(&MBB));
3906 }
3907
3908 // Sort all DBG_VALUEs we've seen by slot number.
3909 for (auto &Pair : DbgVRegToValues)
3910 llvm::sort(Pair.second);
3911}
3912
3913void RegisterCoalescer::checkMergingChangesDbgValues(CoalescerPair &CP,
3914 LiveRange &LHS,
3915 JoinVals &LHSVals,
3916 LiveRange &RHS,
3917 JoinVals &RHSVals) {
3918 auto ScanForDstReg = [&](Register Reg) {
3919 checkMergingChangesDbgValuesImpl(Reg, RHS, LHS, LHSVals);
3920 };
3921
3922 auto ScanForSrcReg = [&](Register Reg) {
3923 checkMergingChangesDbgValuesImpl(Reg, LHS, RHS, RHSVals);
3924 };
3925
3926 // Scan for unsound updates of both the source and destination register.
3927 ScanForSrcReg(CP.getSrcReg());
3928 ScanForDstReg(CP.getDstReg());
3929}
3930
3931void RegisterCoalescer::checkMergingChangesDbgValuesImpl(Register Reg,
3932 LiveRange &OtherLR,
3933 LiveRange &RegLR,
3934 JoinVals &RegVals) {
3935 // Are there any DBG_VALUEs to examine?
3936 auto VRegMapIt = DbgVRegToValues.find(Reg);
3937 if (VRegMapIt == DbgVRegToValues.end())
3938 return;
3939
3940 auto &DbgValueSet = VRegMapIt->second;
3941 auto DbgValueSetIt = DbgValueSet.begin();
3942 auto SegmentIt = OtherLR.begin();
3943
3944 bool LastUndefResult = false;
3945 SlotIndex LastUndefIdx;
3946
3947 // If the "Other" register is live at a slot Idx, test whether Reg can
3948 // safely be merged with it, or should be marked undef.
3949 auto ShouldUndef = [&RegVals, &RegLR, &LastUndefResult,
3950 &LastUndefIdx](SlotIndex Idx) -> bool {
3951 // Our worst-case performance typically happens with asan, causing very
3952 // many DBG_VALUEs of the same location. Cache a copy of the most recent
3953 // result for this edge-case.
3954 if (LastUndefIdx == Idx)
3955 return LastUndefResult;
3956
3957 // If the other range was live, and Reg's was not, the register coalescer
3958 // will not have tried to resolve any conflicts. We don't know whether
3959 // the DBG_VALUE will refer to the same value number, so it must be made
3960 // undef.
3961 auto OtherIt = RegLR.find(Idx);
3962 if (OtherIt == RegLR.end())
3963 return true;
3964
3965 // Both the registers were live: examine the conflict resolution record for
3966 // the value number Reg refers to. CR_Keep meant that this value number
3967 // "won" and the merged register definitely refers to that value. CR_Erase
3968 // means the value number was a redundant copy of the other value, which
3969 // was coalesced and Reg deleted. It's safe to refer to the other register
3970 // (which will be the source of the copy).
3971 auto Resolution = RegVals.getResolution(OtherIt->valno->id);
3972 LastUndefResult =
3973 Resolution != JoinVals::CR_Keep && Resolution != JoinVals::CR_Erase;
3974 LastUndefIdx = Idx;
3975 return LastUndefResult;
3976 };
3977
3978 // Iterate over both the live-range of the "Other" register, and the set of
3979 // DBG_VALUEs for Reg at the same time. Advance whichever one has the lowest
3980 // slot index. This relies on the DbgValueSet being ordered.
3981 while (DbgValueSetIt != DbgValueSet.end() && SegmentIt != OtherLR.end()) {
3982 if (DbgValueSetIt->first < SegmentIt->end) {
3983 // "Other" is live and there is a DBG_VALUE of Reg: test if we should
3984 // set it undef.
3985 if (DbgValueSetIt->first >= SegmentIt->start) {
3986 bool HasReg = DbgValueSetIt->second->hasDebugOperandForReg(Reg);
3987 bool ShouldUndefReg = ShouldUndef(DbgValueSetIt->first);
3988 if (HasReg && ShouldUndefReg) {
3989 // Mark undef, erase record of this DBG_VALUE to avoid revisiting.
3990 DbgValueSetIt->second->setDebugValueUndef();
3991 continue;
3992 }
3993 }
3994 ++DbgValueSetIt;
3995 } else {
3996 ++SegmentIt;
3997 }
3998 }
3999}
4000
4001namespace {
4002
4003/// Information concerning MBB coalescing priority.
4004struct MBBPriorityInfo {
4005 MachineBasicBlock *MBB;
4006 unsigned Depth;
4007 bool IsSplit;
4008
4009 MBBPriorityInfo(MachineBasicBlock *mbb, unsigned depth, bool issplit)
4010 : MBB(mbb), Depth(depth), IsSplit(issplit) {}
4011};
4012
4013} // end anonymous namespace
4014
4015/// C-style comparator that sorts first based on the loop depth of the basic
4016/// block (the unsigned), and then on the MBB number.
4017///
4018/// EnableGlobalCopies assumes that the primary sort key is loop depth.
4019static int compareMBBPriority(const MBBPriorityInfo *LHS,
4020 const MBBPriorityInfo *RHS) {
4021 // Deeper loops first
4022 if (LHS->Depth != RHS->Depth)
4023 return LHS->Depth > RHS->Depth ? -1 : 1;
4024
4025 // Try to unsplit critical edges next.
4026 if (LHS->IsSplit != RHS->IsSplit)
4027 return LHS->IsSplit ? -1 : 1;
4028
4029 // Prefer blocks that are more connected in the CFG. This takes care of
4030 // the most difficult copies first while intervals are short.
4031 unsigned cl = LHS->MBB->pred_size() + LHS->MBB->succ_size();
4032 unsigned cr = RHS->MBB->pred_size() + RHS->MBB->succ_size();
4033 if (cl != cr)
4034 return cl > cr ? -1 : 1;
4035
4036 // As a last resort, sort by block number.
4037 return LHS->MBB->getNumber() < RHS->MBB->getNumber() ? -1 : 1;
4038}
4039
4040/// \returns true if the given copy uses or defines a local live range.
4041static bool isLocalCopy(MachineInstr *Copy, const LiveIntervals *LIS) {
4042 if (!Copy->isCopy())
4043 return false;
4044
4045 if (Copy->getOperand(1).isUndef())
4046 return false;
4047
4048 Register SrcReg = Copy->getOperand(1).getReg();
4049 Register DstReg = Copy->getOperand(0).getReg();
4050 if (SrcReg.isPhysical() || DstReg.isPhysical())
4051 return false;
4052
4053 return LIS->intervalIsInOneMBB(LIS->getInterval(SrcReg)) ||
4054 LIS->intervalIsInOneMBB(LIS->getInterval(DstReg));
4055}
4056
4057void RegisterCoalescer::lateLiveIntervalUpdate() {
4058 for (Register reg : ToBeUpdated) {
4059 if (!LIS->hasInterval(reg))
4060 continue;
4061 LiveInterval &LI = LIS->getInterval(reg);
4062 shrinkToUses(&LI, &DeadDefs);
4063 if (!DeadDefs.empty())
4064 eliminateDeadDefs();
4065 }
4066 ToBeUpdated.clear();
4067}
4068
4069bool RegisterCoalescer::copyCoalesceWorkList(
4071 bool Progress = false;
4072 SmallPtrSet<MachineInstr *, 4> CurrentErasedInstrs;
4073 for (MachineInstr *&MI : CurrList) {
4074 if (!MI)
4075 continue;
4076 // Skip instruction pointers that have already been erased, for example by
4077 // dead code elimination.
4078 if (ErasedInstrs.count(MI) || CurrentErasedInstrs.count(MI)) {
4079 MI = nullptr;
4080 continue;
4081 }
4082 bool Again = false;
4083 bool Success = joinCopy(MI, Again, CurrentErasedInstrs);
4084 Progress |= Success;
4085 if (Success || !Again)
4086 MI = nullptr;
4087 }
4088 // Clear instructions not recorded in `ErasedInstrs` but erased.
4089 if (!CurrentErasedInstrs.empty()) {
4090 for (MachineInstr *&MI : CurrList) {
4091 if (MI && CurrentErasedInstrs.count(MI))
4092 MI = nullptr;
4093 }
4094 for (MachineInstr *&MI : WorkList) {
4095 if (MI && CurrentErasedInstrs.count(MI))
4096 MI = nullptr;
4097 }
4098 }
4099 return Progress;
4100}
4101
4102/// Check if DstReg is a terminal node.
4103/// I.e., it does not have any affinity other than \p Copy.
4104static bool isTerminalReg(Register DstReg, const MachineInstr &Copy,
4105 const MachineRegisterInfo *MRI) {
4106 assert(Copy.isCopyLike());
4107 // Check if the destination of this copy as any other affinity.
4108 for (const MachineInstr &MI : MRI->reg_nodbg_instructions(DstReg))
4109 if (&MI != &Copy && MI.isCopyLike())
4110 return false;
4111 return true;
4112}
4113
4114bool RegisterCoalescer::applyTerminalRule(const MachineInstr &Copy) const {
4115 assert(Copy.isCopyLike());
4116 if (!UseTerminalRule)
4117 return false;
4118 Register SrcReg, DstReg;
4119 unsigned SrcSubReg = 0, DstSubReg = 0;
4120 if (!isMoveInstr(*TRI, &Copy, SrcReg, DstReg, SrcSubReg, DstSubReg))
4121 return false;
4122 // Check if the destination of this copy has any other affinity.
4123 if (DstReg.isPhysical() ||
4124 // If SrcReg is a physical register, the copy won't be coalesced.
4125 // Ignoring it may have other side effect (like missing
4126 // rematerialization). So keep it.
4127 SrcReg.isPhysical() || !isTerminalReg(DstReg, Copy, MRI))
4128 return false;
4129
4130 // DstReg is a terminal node. Check if it interferes with any other
4131 // copy involving SrcReg.
4132 const MachineBasicBlock *OrigBB = Copy.getParent();
4133 const LiveInterval &DstLI = LIS->getInterval(DstReg);
4134 for (const MachineInstr &MI : MRI->reg_nodbg_instructions(SrcReg)) {
4135 // Technically we should check if the weight of the new copy is
4136 // interesting compared to the other one and update the weight
4137 // of the copies accordingly. However, this would only work if
4138 // we would gather all the copies first then coalesce, whereas
4139 // right now we interleave both actions.
4140 // For now, just consider the copies that are in the same block.
4141 if (&MI == &Copy || !MI.isCopyLike() || MI.getParent() != OrigBB)
4142 continue;
4143 Register OtherSrcReg, OtherReg;
4144 unsigned OtherSrcSubReg = 0, OtherSubReg = 0;
4145 if (!isMoveInstr(*TRI, &Copy, OtherSrcReg, OtherReg, OtherSrcSubReg,
4146 OtherSubReg))
4147 return false;
4148 if (OtherReg == SrcReg)
4149 OtherReg = OtherSrcReg;
4150 // Check if OtherReg is a non-terminal.
4151 if (OtherReg.isPhysical() || isTerminalReg(OtherReg, MI, MRI))
4152 continue;
4153 // Check that OtherReg interfere with DstReg.
4154 if (LIS->getInterval(OtherReg).overlaps(DstLI)) {
4155 LLVM_DEBUG(dbgs() << "Apply terminal rule for: " << printReg(DstReg)
4156 << '\n');
4157 return true;
4158 }
4159 }
4160 return false;
4161}
4162
4163void RegisterCoalescer::copyCoalesceInMBB(MachineBasicBlock *MBB) {
4164 LLVM_DEBUG(dbgs() << MBB->getName() << ":\n");
4165
4166 // Collect all copy-like instructions in MBB. Don't start coalescing anything
4167 // yet, it might invalidate the iterator.
4168 const unsigned PrevSize = WorkList.size();
4169 if (JoinGlobalCopies) {
4170 SmallVector<MachineInstr *, 2> LocalTerminals;
4171 SmallVector<MachineInstr *, 2> GlobalTerminals;
4172 // Coalesce copies top-down to propagate coalescing and rematerialization
4173 // forward.
4174 for (MachineInstr &MI : *MBB) {
4175 if (!MI.isCopyLike())
4176 continue;
4177 bool ApplyTerminalRule = applyTerminalRule(MI);
4178 if (isLocalCopy(&MI, LIS)) {
4179 if (ApplyTerminalRule)
4180 LocalTerminals.push_back(&MI);
4181 else
4182 LocalWorkList.push_back(&MI);
4183 } else {
4184 if (ApplyTerminalRule)
4185 GlobalTerminals.push_back(&MI);
4186 else
4187 WorkList.push_back(&MI);
4188 }
4189 }
4190 // Append the copies evicted by the terminal rule at the end of the list.
4191 LocalWorkList.append(LocalTerminals.begin(), LocalTerminals.end());
4192 WorkList.append(GlobalTerminals.begin(), GlobalTerminals.end());
4193 } else {
4195 // Coalesce copies top-down to propagate coalescing and rematerialization
4196 // forward.
4197 for (MachineInstr &MII : *MBB)
4198 if (MII.isCopyLike()) {
4199 if (applyTerminalRule(MII))
4200 Terminals.push_back(&MII);
4201 else
4202 WorkList.push_back(&MII);
4203 }
4204 // Append the copies evicted by the terminal rule at the end of the list.
4205 WorkList.append(Terminals.begin(), Terminals.end());
4206 }
4207 // Try coalescing the collected copies immediately, and remove the nulls.
4208 // This prevents the WorkList from getting too large since most copies are
4209 // joinable on the first attempt.
4210 MutableArrayRef<MachineInstr *> CurrList(WorkList.begin() + PrevSize,
4211 WorkList.end());
4212 if (copyCoalesceWorkList(CurrList))
4213 WorkList.erase(
4214 std::remove(WorkList.begin() + PrevSize, WorkList.end(), nullptr),
4215 WorkList.end());
4216}
4217
4218void RegisterCoalescer::coalesceLocals() {
4219 copyCoalesceWorkList(LocalWorkList);
4220 for (MachineInstr *MI : LocalWorkList) {
4221 if (MI)
4222 WorkList.push_back(MI);
4223 }
4224 LocalWorkList.clear();
4225}
4226
4227void RegisterCoalescer::joinAllIntervals() {
4228 LLVM_DEBUG(dbgs() << "********** JOINING INTERVALS ***********\n");
4229 assert(WorkList.empty() && LocalWorkList.empty() && "Old data still around.");
4230
4231 std::vector<MBBPriorityInfo> MBBs;
4232 MBBs.reserve(MF->size());
4233 for (MachineBasicBlock &MBB : *MF) {
4234 MBBs.push_back(MBBPriorityInfo(&MBB, Loops->getLoopDepth(&MBB),
4235 JoinSplitEdges && isSplitEdge(&MBB)));
4236 }
4237 array_pod_sort(MBBs.begin(), MBBs.end(), compareMBBPriority);
4238
4239 // Coalesce intervals in MBB priority order.
4240 unsigned CurrDepth = std::numeric_limits<unsigned>::max();
4241 for (MBBPriorityInfo &MBB : MBBs) {
4242 // Try coalescing the collected local copies for deeper loops.
4243 if (JoinGlobalCopies && MBB.Depth < CurrDepth) {
4244 coalesceLocals();
4245 CurrDepth = MBB.Depth;
4246 }
4247 copyCoalesceInMBB(MBB.MBB);
4248 }
4249 lateLiveIntervalUpdate();
4250 coalesceLocals();
4251
4252 // Joining intervals can allow other intervals to be joined. Iteratively join
4253 // until we make no progress.
4254 while (copyCoalesceWorkList(WorkList))
4255 /* empty */;
4256 lateLiveIntervalUpdate();
4257}
4258
4262 MFPropsModifier _(*this, MF);
4263 auto &LIS = MFAM.getResult<LiveIntervalsAnalysis>(MF);
4264 auto &Loops = MFAM.getResult<MachineLoopAnalysis>(MF);
4265 auto *SI = MFAM.getCachedResult<SlotIndexesAnalysis>(MF);
4266 RegisterCoalescer Impl(&LIS, SI, &Loops);
4267 if (!Impl.run(MF))
4268 return PreservedAnalyses::all();
4270 PA.preserveSet<CFGAnalyses>();
4271 PA.preserve<LiveIntervalsAnalysis>();
4272 PA.preserve<SlotIndexesAnalysis>();
4273 PA.preserve<MachineLoopAnalysis>();
4274 PA.preserve<MachineDominatorTreeAnalysis>();
4275 return PA;
4276}
4277
4278bool RegisterCoalescerLegacy::runOnMachineFunction(MachineFunction &MF) {
4279 auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
4280 auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
4281 auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4282 SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI() : nullptr;
4283 RegisterCoalescer Impl(LIS, SI, Loops);
4284 return Impl.run(MF);
4285}
4286
4287bool RegisterCoalescer::run(MachineFunction &fn) {
4288 LLVM_DEBUG(dbgs() << "********** REGISTER COALESCER **********\n"
4289 << "********** Function: " << fn.getName() << '\n');
4290
4291 // Variables changed between a setjmp and a longjump can have undefined value
4292 // after the longjmp. This behaviour can be observed if such a variable is
4293 // spilled, so longjmp won't restore the value in the spill slot.
4294 // RegisterCoalescer should not run in functions with a setjmp to avoid
4295 // merging such undefined variables with predictable ones.
4296 //
4297 // TODO: Could specifically disable coalescing registers live across setjmp
4298 // calls
4299 if (fn.exposesReturnsTwice()) {
4300 LLVM_DEBUG(
4301 dbgs() << "* Skipped as it exposes functions that returns twice.\n");
4302 return false;
4303 }
4304
4305 MF = &fn;
4306 MRI = &fn.getRegInfo();
4307 const TargetSubtargetInfo &STI = fn.getSubtarget();
4308 TRI = STI.getRegisterInfo();
4309 TII = STI.getInstrInfo();
4311 JoinGlobalCopies = STI.enableJoinGlobalCopies();
4312 else
4313 JoinGlobalCopies = (EnableGlobalCopies == cl::BOU_TRUE);
4314
4315 // If there are PHIs tracked by debug-info, they will need updating during
4316 // coalescing. Build an index of those PHIs to ease updating.
4317 SlotIndexes *Slots = LIS->getSlotIndexes();
4318 for (const auto &DebugPHI : MF->DebugPHIPositions) {
4319 MachineBasicBlock *MBB = DebugPHI.second.MBB;
4320 Register Reg = DebugPHI.second.Reg;
4321 unsigned SubReg = DebugPHI.second.SubReg;
4322 SlotIndex SI = Slots->getMBBStartIdx(MBB);
4323 PHIValPos P = {SI, Reg, SubReg};
4324 PHIValToPos.insert(std::make_pair(DebugPHI.first, P));
4325 RegToPHIIdx[Reg].push_back(DebugPHI.first);
4326 }
4327
4328 // The MachineScheduler does not currently require JoinSplitEdges. This will
4329 // either be enabled unconditionally or replaced by a more general live range
4330 // splitting optimization.
4331 JoinSplitEdges = EnableJoinSplits;
4332
4333 if (VerifyCoalescing)
4334 MF->verify(LIS, SI, "Before register coalescing", &errs());
4335
4336 DbgVRegToValues.clear();
4338
4339 RegClassInfo.runOnMachineFunction(fn);
4340
4341 // Join (coalesce) intervals if requested.
4342 if (EnableJoining)
4343 joinAllIntervals();
4344
4345 // After deleting a lot of copies, register classes may be less constrained.
4346 // Removing sub-register operands may allow GR32_ABCD -> GR32 and DPR_VFP2 ->
4347 // DPR inflation.
4348 array_pod_sort(InflateRegs.begin(), InflateRegs.end());
4349 InflateRegs.erase(llvm::unique(InflateRegs), InflateRegs.end());
4350 LLVM_DEBUG(dbgs() << "Trying to inflate " << InflateRegs.size()
4351 << " regs.\n");
4352 for (Register Reg : InflateRegs) {
4353 if (MRI->reg_nodbg_empty(Reg))
4354 continue;
4355 if (MRI->recomputeRegClass(Reg)) {
4356 LLVM_DEBUG(dbgs() << printReg(Reg) << " inflated to "
4357 << TRI->getRegClassName(MRI->getRegClass(Reg)) << '\n');
4358 ++NumInflated;
4359
4360 LiveInterval &LI = LIS->getInterval(Reg);
4361 if (LI.hasSubRanges()) {
4362 // If the inflated register class does not support subregisters anymore
4363 // remove the subranges.
4364 if (!MRI->shouldTrackSubRegLiveness(Reg)) {
4365 LI.clearSubRanges();
4366 } else {
4367#ifndef NDEBUG
4368 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
4369 // If subranges are still supported, then the same subregs
4370 // should still be supported.
4371 for (LiveInterval::SubRange &S : LI.subranges()) {
4372 assert((S.LaneMask & ~MaxMask).none());
4373 }
4374#endif
4375 }
4376 }
4377 }
4378 }
4379
4380 // After coalescing, update any PHIs that are being tracked by debug-info
4381 // with their new VReg locations.
4382 for (auto &p : MF->DebugPHIPositions) {
4383 auto it = PHIValToPos.find(p.first);
4384 assert(it != PHIValToPos.end());
4385 p.second.Reg = it->second.Reg;
4386 p.second.SubReg = it->second.SubReg;
4387 }
4388
4389 PHIValToPos.clear();
4390 RegToPHIIdx.clear();
4391
4392 LLVM_DEBUG(LIS->dump());
4393
4394 if (VerifyCoalescing)
4395 MF->verify(LIS, SI, "After register coalescing", &errs());
4396 return true;
4397}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
const HexagonInstrInfo * TII
Hexagon Hardware Loops
#define _
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
A common definition of LaneBitmask for use in TableGen and CodeGen.
#define I(x, y, z)
Definition MD5.cpp:58
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
if(PassOpts->AAPipeline)
#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
Basic Register Allocator
static cl::opt< cl::boolOrDefault > EnableGlobalCopies("join-globalcopies", cl::desc("Coalesce copies that span blocks (default=subtarget)"), cl::init(cl::BOU_UNSET), cl::Hidden)
Temporary flag to test global copy optimization.
static bool isLocalCopy(MachineInstr *Copy, const LiveIntervals *LIS)
static bool isSplitEdge(const MachineBasicBlock *MBB)
Return true if this block should be vacated by the coalescer to eliminate branches.
static int compareMBBPriority(const MBBPriorityInfo *LHS, const MBBPriorityInfo *RHS)
C-style comparator that sorts first based on the loop depth of the basic block (the unsigned),...
static cl::opt< unsigned > LargeIntervalSizeThreshold("large-interval-size-threshold", cl::Hidden, cl::desc("If the valnos size of an interval is larger than the threshold, " "it is regarded as a large interval. "), cl::init(100))
static bool isDefInSubRange(LiveInterval &LI, SlotIndex Def)
Check if any of the subranges of LI contain a definition at Def.
static std::pair< bool, bool > addSegmentsWithValNo(LiveRange &Dst, VNInfo *DstValNo, const LiveRange &Src, const VNInfo *SrcValNo)
Copy segments with value number SrcValNo from liverange Src to live range @Dst and use value number D...
static bool isLiveThrough(const LiveQueryResult Q)
static bool isTerminalReg(Register DstReg, const MachineInstr &Copy, const MachineRegisterInfo *MRI)
Check if DstReg is a terminal node.
static cl::opt< bool > VerifyCoalescing("verify-coalescing", cl::desc("Verify machine instrs before and after register coalescing"), cl::Hidden)
register Register static false bool isMoveInstr(const TargetRegisterInfo &tri, const MachineInstr *MI, Register &Src, Register &Dst, unsigned &SrcSub, unsigned &DstSub)
static cl::opt< bool > EnableJoinSplits("join-splitedges", cl::desc("Coalesce copies on split edges (default=subtarget)"), cl::Hidden)
Temporary flag to test critical edge unsplitting.
static cl::opt< bool > EnableJoining("join-liveintervals", cl::desc("Coalesce copies (default=true)"), cl::init(true), cl::Hidden)
static cl::opt< unsigned > LargeIntervalFreqThreshold("large-interval-freq-threshold", cl::Hidden, cl::desc("For a large interval, if it is coalesced with other live " "intervals many times more than the threshold, stop its " "coalescing to control the compile time. "), cl::init(256))
static bool definesFullReg(const MachineInstr &MI, Register Reg)
Returns true if MI defines the full vreg Reg, as opposed to just defining a subregister.
static cl::opt< unsigned > LateRematUpdateThreshold("late-remat-update-threshold", cl::Hidden, cl::desc("During rematerialization for a copy, if the def instruction has " "many other copy uses to be rematerialized, delay the multiple " "separate live interval update work and do them all at once after " "all those rematerialization are done. It will save a lot of " "repeated work. "), cl::init(100))
static cl::opt< bool > UseTerminalRule("terminal-rule", cl::desc("Apply the terminal rule"), cl::init(false), cl::Hidden)
SI Optimize VGPR LiveRange
unsigned OpIndex
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static DenseMap< Register, std::vector< std::pair< SlotIndex, MachineInstr * > > > buildVRegToDbgValueMap(MachineFunction &MF, const LiveIntervals *Liveness)
static void shrinkToUses(LiveInterval &LI, LiveIntervals &LIS)
Value * RHS
Value * LHS
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 & addPreservedID(const void *ID)
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this 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
bool test(unsigned Idx) const
Definition BitVector.h:480
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A helper class for register coalescers.
bool flip()
Swap SrcReg and DstReg.
bool isCoalescable(const MachineInstr *) const
Return true if MI is a copy instruction that will become an identity copy after coalescing.
bool setRegisters(const MachineInstr *)
Set registers to match the copy instruction MI.
A debug info location.
Definition DebugLoc.h:124
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:167
bool erase(const KeyT &Val)
Definition DenseMap.h:311
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:222
bool isAsCheapAsAMove(const MachineInstr &MI) const override
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
LLVM_ABI void removeEmptySubRanges()
Removes all subranges without any segments (subranges without segments are not considered valid and s...
Register reg() const
bool hasSubRanges() const
Returns true if subregister liveness information is available.
SubRange * createSubRangeFrom(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, const LiveRange &CopyFrom)
Like createSubRange() but the new range is filled with a copy of the liveness information in CopyFrom...
iterator_range< subrange_iterator > subranges()
LLVM_ABI void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, std::function< void(LiveInterval::SubRange &)> Apply, const SlotIndexes &Indexes, const TargetRegisterInfo &TRI, unsigned ComposeSubRegIdx=0)
Refines the subranges to support LaneMask.
LLVM_ABI void computeSubRangeUndefs(SmallVectorImpl< SlotIndex > &Undefs, LaneBitmask LaneMask, const MachineRegisterInfo &MRI, const SlotIndexes &Indexes) const
For a given lane mask LaneMask, compute indexes at which the lane is marked undefined by subregister ...
SubRange * createSubRange(BumpPtrAllocator &Allocator, LaneBitmask LaneMask)
Creates a new empty subregister live range.
LLVM_ABI void clearSubRanges()
Removes all subregister liveness information.
bool hasInterval(Register Reg) const
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Return the first index in the given basic block.
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
LLVM_ABI bool hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const
Returns true if VNI is killed by any PHI-def values in LI.
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
LLVM_ABI bool checkRegMaskInterference(const LiveInterval &LI, BitVector &UsableRegs)
Test if LI is live across any register mask instructions, and compute a bit mask of physical register...
SlotIndexes * getSlotIndexes() const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
VNInfo::Allocator & getVNInfoAllocator()
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const
Return the last index in the given basic block.
LiveRange & getRegUnit(unsigned Unit)
Return the live range for register unit Unit.
LiveRange * getCachedRegUnit(unsigned Unit)
Return the live range for register unit Unit if it has already been computed, or nullptr if it hasn't...
LiveInterval & getInterval(Register Reg)
LLVM_ABI void pruneValue(LiveRange &LR, SlotIndex Kill, SmallVectorImpl< SlotIndex > *EndPoints)
If LR has a live value at Kill, prune its live range by removing any liveness reachable from Kill.
void removeInterval(Register Reg)
Interval removal.
LLVM_ABI MachineBasicBlock * intervalIsInOneMBB(const LiveInterval &LI) const
If LI is confined to a single basic block, return a pointer to that block.
LLVM_ABI void removeVRegDefAt(LiveInterval &LI, SlotIndex Pos)
Remove value number and related live segments of LI and its subranges that start at position Pos.
LLVM_ABI bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr * > *dead=nullptr)
After removing some uses of a register, shrink its live range to just the remaining uses.
LLVM_ABI void extendToIndices(LiveRange &LR, ArrayRef< SlotIndex > Indices, ArrayRef< SlotIndex > Undefs)
Extend the live range LR to reach all points in Indices.
LLVM_ABI void dump() const
LLVM_ABI void removePhysRegDefAt(MCRegister Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
bool isLiveInToMBB(const LiveRange &LR, const MachineBasicBlock *mbb) const
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
Result of a LiveRange query.
VNInfo * valueOutOrDead() const
Returns the value alive at the end of the instruction, if any.
VNInfo * valueIn() const
Return the value that is live-in to the instruction.
VNInfo * valueOut() const
Return the value leaving the instruction, if any.
VNInfo * valueDefined() const
Return the value defined by this instruction, if any.
SlotIndex endPoint() const
Return the end point of the last live range segment to interact with the instruction,...
bool isKill() const
Return true if the live-in value is killed by this instruction.
Callback methods for LiveRangeEdit owners.
SlotIndex rematerializeAt(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, const Remat &RM, const TargetRegisterInfo &, bool Late=false, unsigned SubIdx=0, MachineInstr *ReplaceIndexMI=nullptr)
rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an instruction into MBB before...
void eliminateDeadDefs(SmallVectorImpl< MachineInstr * > &Dead, ArrayRef< Register > RegsBeingSpilled={})
eliminateDeadDefs - Try to delete machine instructions that are now dead (allDefsAreDead returns true...
This class represents the liveness of a register, stack slot, etc.
VNInfo * getValNumInfo(unsigned ValNo)
getValNumInfo - Returns pointer to the specified val#.
LLVM_ABI iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
Segments::iterator iterator
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
LLVM_ABI void join(LiveRange &Other, const int *ValNoAssignments, const int *RHSValNoAssignments, SmallVectorImpl< VNInfo * > &NewVNInfo)
join - Join two live ranges (this, and other) together.
bool liveAt(SlotIndex index) const
LLVM_ABI VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc)
createDeadDef - Make sure the range has a value defined at Def.
LLVM_ABI void removeValNo(VNInfo *ValNo)
removeValNo - Remove all the segments defined by the specified value#.
bool empty() const
bool overlaps(const LiveRange &other) const
overlaps - Return true if the intersection of the two live ranges is not empty.
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarily including Idx,...
bool verify() const
Walk the range and assert if any invariants fail to hold.
LLVM_ABI VNInfo * MergeValueNumberInto(VNInfo *V1, VNInfo *V2)
MergeValueNumberInto - This method is called when two value numbers are found to be equivalent.
unsigned getNumValNums() const
iterator begin()
VNInfoList valnos
bool containsOneValue() const
size_t size() const
iterator FindSegmentContaining(SlotIndex Idx)
Return an iterator to the segment that contains the specified index, or end() if there is none.
void assign(const LiveRange &Other, BumpPtrAllocator &Allocator)
Copies values numbers and live segments from Other into this range.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
LLVM_ABI iterator find(SlotIndex Pos)
find - Return an iterator pointing to the first segment that ends after Pos, or end().
Describe properties that are true of each instruction in the target description file.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
MCRegUnitRootIterator enumerates the root registers of a register unit.
bool isValid() const
Check if the iterator is at the end of the list.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
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.
LLVM_ABI bool hasEHPadSuccessor() const
bool isEHPad() const
Returns true if the block is a landing pad.
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 getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
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 StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Analysis pass which computes a MachineDominatorTree.
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool exposesReturnsTwice() const
exposesReturnsTwice - Returns true if the function calls setjmp or any other similar functions with a...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool verify(Pass *p=nullptr, const char *Banner=nullptr, raw_ostream *OS=nullptr, bool AbortOnError=true) const
Run the current MachineFunction through the machine code verifier, useful for debugger use.
DenseMap< unsigned, DebugPHIRegallocPos > DebugPHIPositions
Map of debug instruction numbers to the position of their PHI instructions during register allocation...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_ABI void setRegisterDefReadUndef(Register Reg, bool IsUndef=true)
Mark all subregister defs of register Reg with the undef flag.
bool isImplicitDef() const
bool isCopy() const
const MachineBasicBlock * getParent() const
bool isCopyLike() const
Return true if the instruction behaves like a copy.
filtered_mop_range all_defs()
Returns an iterator range over all operands that are (explicit or implicit) register defs.
LLVM_ABI std::pair< bool, bool > readsWritesVirtualRegister(Register Reg, SmallVectorImpl< unsigned > *Ops=nullptr) const
Return a pair of bools (reads, writes) indicating if this instruction reads or writes Reg.
bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx=nullptr) const
Return true if the use operand of the specified index is tied to a def operand.
LLVM_ABI bool isSafeToMove(bool &SawStore) const
Return true if it is safe to move this instruction.
bool isDebugInstr() const
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
bool isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx=nullptr) const
Given the index of a register def operand, check if the register def is tied to a source operand,...
bool isFullCopy() const
LLVM_ABI int findRegisterUseOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isKill=false) const
Returns the operand index that is a use of the specific register or -1 if it is not found.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
bool isCommutable(QueryType Type=IgnoreBundle) const
Return true if this may be a 2- or 3-address instruction (of the form "X = op Y, Z,...
mop_range operands()
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
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...
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI int findRegisterDefOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false) const
Returns the operand index that is a def of the specified register or -1 if it is not found.
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
LLVM_ABI bool allDefsAreDead() const
Return true if all the defs of this instruction are dead.
Analysis pass that exposes the MachineLoopInfo for a machine function.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
LLVM_ABI void substVirtReg(Register Reg, unsigned SubIdx, const TargetRegisterInfo &)
substVirtReg - Substitute the current register with the virtual subregister Reg:SubReg.
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
LLVM_ABI void substPhysReg(MCRegister Reg, const TargetRegisterInfo &)
substPhysReg - Substitute the current register with the physical register Reg, taking any existing Su...
void setIsUndef(bool Val=true)
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
defusechain_instr_iterator< true, true, false, true > reg_instr_iterator
reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses of the specified register,...
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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
bool isProperSubClass(const TargetRegisterClass *RC) const
isProperSubClass - Returns true if RC has a legal super-class with more allocatable registers.
unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const
getNumAllocatableRegs - Returns the number of actually allocatable registers in RC in the current fun...
LLVM_ABI void runOnMachineFunction(const MachineFunction &MF, bool Rev=false)
runOnFunction - Prepare to answer questions about MF.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Wrapper class representing virtual and physical registers.
Definition Register.h:19
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:102
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
constexpr unsigned id() const
Definition Register.h:95
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
bool isEarlyClobber() const
isEarlyClobber - Returns true if this is an early-clobber slot.
bool isValid() const
Returns true if this is a valid index.
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
SlotIndexes pass.
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
Returns the basic block which the given index falls in.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the index past the last valid index in the given basic block.
SlotIndex getNextNonNullIndex(SlotIndex Index)
Returns the next non-null index, if one exists.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
SlotIndex getIndexBefore(const MachineInstr &MI) const
getIndexBefore - Returns the index of the last indexed instruction before MI, or the start index of i...
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction for the given index, or null if the given index has no instruction associated...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
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.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
static const unsigned CommuteAnyOperandIndex
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual bool enableJoinGlobalCopies() const
True if the subtarget should enable joining global copies.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
VNInfo - Value Number Information.
void markUnused()
Mark this value as unused.
BumpPtrAllocator Allocator
bool isUnused() const
Returns true if this value is unused.
unsigned id
The ID number of this value.
SlotIndex def
The index of the defining instruction.
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
static bool allUsesAvailableAt(const MachineInstr *MI, SlotIndex UseIdx, const LiveIntervals &LIS, const MachineRegisterInfo &MRI, const TargetInstrInfo &TII)
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
self_iterator getIterator()
Definition ilist_node.h:123
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This namespace contains all of the command line option processing machinery.
Definition CommandLine.h:53
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
iterator end() const
Definition BasicBlock.h:89
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI char & RegisterCoalescerID
RegisterCoalescer - This pass merges live ranges to eliminate copies.
LLVM_ABI char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
LLVM_ABI Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2138
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
Printable PrintLaneMask(LaneBitmask LaneMask)
Create Printable object to print LaneBitmasks on a raw_ostream.
Definition LaneBitmask.h:92
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2078
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2009
LLVM_ABI void initializeRegisterCoalescerLegacyPass(PassRegistry &)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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:1734
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1624
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
unsigned MCRegUnit
Register units are used to compute register aliasing.
Definition MCRegister.h:30
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Success
The lock was released successfully.
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
DWARFExpression::Operation Op
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
Definition STLExtras.h:1409
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1899
LLVM_ABI void eraseInstrs(ArrayRef< MachineInstr * > DeadInstrs, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition Utils.cpp:1705
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition STLExtras.h:1584
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
static constexpr LaneBitmask getLane(unsigned Lane)
Definition LaneBitmask.h:83
static constexpr LaneBitmask getAll()
Definition LaneBitmask.h:82
constexpr bool any() const
Definition LaneBitmask.h:53
static constexpr LaneBitmask getNone()
Definition LaneBitmask.h:81
Remat - Information needed to rematerialize at a specific location.
This represents a simple continuous liveness interval for a value.