LiveIntervalCalc.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //===- LiveIntervalCalc.cpp - Calculate live interval --------------------===//
  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. // Implementation of the LiveIntervalCalc class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/LiveIntervalCalc.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/iterator_range.h"
  15. #include "llvm/CodeGen/LiveInterval.h"
  16. #include "llvm/CodeGen/MachineInstr.h"
  17. #include "llvm/CodeGen/MachineOperand.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/CodeGen/SlotIndexes.h"
  20. #include "llvm/CodeGen/TargetRegisterInfo.h"
  21. #include "llvm/MC/LaneBitmask.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include <cassert>
  24. using namespace llvm;
  25. #define DEBUG_TYPE "regalloc"
  26. // Reserve an address that indicates a value that is known to be "undef".
  27. static VNInfo UndefVNI(0xbad, SlotIndex());
  28. static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
  29. LiveRange &LR, const MachineOperand &MO) {
  30. const MachineInstr &MI = *MO.getParent();
  31. SlotIndex DefIdx =
  32. Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
  33. // Create the def in LR. This may find an existing def.
  34. LR.createDeadDef(DefIdx, Alloc);
  35. }
  36. void LiveIntervalCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
  37. const MachineRegisterInfo *MRI = getRegInfo();
  38. SlotIndexes *Indexes = getIndexes();
  39. VNInfo::Allocator *Alloc = getVNAlloc();
  40. assert(MRI && Indexes && "call reset() first");
  41. // Step 1: Create minimal live segments for every definition of Reg.
  42. // Visit all def operands. If the same instruction has multiple defs of Reg,
  43. // createDeadDef() will deduplicate.
  44. const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
  45. Register Reg = LI.reg();
  46. for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
  47. if (!MO.isDef() && !MO.readsReg())
  48. continue;
  49. unsigned SubReg = MO.getSubReg();
  50. if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
  51. LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
  52. : MRI->getMaxLaneMaskForVReg(Reg);
  53. // If this is the first time we see a subregister def, initialize
  54. // subranges by creating a copy of the main range.
  55. if (!LI.hasSubRanges() && !LI.empty()) {
  56. LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
  57. LI.createSubRangeFrom(*Alloc, ClassMask, LI);
  58. }
  59. LI.refineSubRanges(
  60. *Alloc, SubMask,
  61. [&MO, Indexes, Alloc](LiveInterval::SubRange &SR) {
  62. if (MO.isDef())
  63. createDeadDef(*Indexes, *Alloc, SR, MO);
  64. },
  65. *Indexes, TRI);
  66. }
  67. // Create the def in the main liverange. We do not have to do this if
  68. // subranges are tracked as we recreate the main range later in this case.
  69. if (MO.isDef() && !LI.hasSubRanges())
  70. createDeadDef(*Indexes, *Alloc, LI, MO);
  71. }
  72. // We may have created empty live ranges for partially undefined uses, we
  73. // can't keep them because we won't find defs in them later.
  74. LI.removeEmptySubRanges();
  75. const MachineFunction *MF = getMachineFunction();
  76. MachineDominatorTree *DomTree = getDomTree();
  77. // Step 2: Extend live segments to all uses, constructing SSA form as
  78. // necessary.
  79. if (LI.hasSubRanges()) {
  80. for (LiveInterval::SubRange &S : LI.subranges()) {
  81. LiveIntervalCalc SubLIC;
  82. SubLIC.reset(MF, Indexes, DomTree, Alloc);
  83. SubLIC.extendToUses(S, Reg, S.LaneMask, &LI);
  84. }
  85. LI.clear();
  86. constructMainRangeFromSubranges(LI);
  87. } else {
  88. resetLiveOutMap();
  89. extendToUses(LI, Reg, LaneBitmask::getAll());
  90. }
  91. }
  92. void LiveIntervalCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
  93. // First create dead defs at all defs found in subranges.
  94. LiveRange &MainRange = LI;
  95. assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
  96. "Expect empty main liverange");
  97. VNInfo::Allocator *Alloc = getVNAlloc();
  98. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  99. for (const VNInfo *VNI : SR.valnos) {
  100. if (!VNI->isUnused() && !VNI->isPHIDef())
  101. MainRange.createDeadDef(VNI->def, *Alloc);
  102. }
  103. }
  104. resetLiveOutMap();
  105. extendToUses(MainRange, LI.reg(), LaneBitmask::getAll(), &LI);
  106. }
  107. void LiveIntervalCalc::createDeadDefs(LiveRange &LR, Register Reg) {
  108. const MachineRegisterInfo *MRI = getRegInfo();
  109. SlotIndexes *Indexes = getIndexes();
  110. VNInfo::Allocator *Alloc = getVNAlloc();
  111. assert(MRI && Indexes && "call reset() first");
  112. // Visit all def operands. If the same instruction has multiple defs of Reg,
  113. // LR.createDeadDef() will deduplicate.
  114. for (MachineOperand &MO : MRI->def_operands(Reg))
  115. createDeadDef(*Indexes, *Alloc, LR, MO);
  116. }
  117. void LiveIntervalCalc::extendToUses(LiveRange &LR, Register Reg,
  118. LaneBitmask Mask, LiveInterval *LI) {
  119. const MachineRegisterInfo *MRI = getRegInfo();
  120. SlotIndexes *Indexes = getIndexes();
  121. SmallVector<SlotIndex, 4> Undefs;
  122. if (LI != nullptr)
  123. LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
  124. // Visit all operands that read Reg. This may include partial defs.
  125. bool IsSubRange = !Mask.all();
  126. const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
  127. for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
  128. // Clear all kill flags. They will be reinserted after register allocation
  129. // by LiveIntervals::addKillFlags().
  130. if (MO.isUse())
  131. MO.setIsKill(false);
  132. // MO::readsReg returns "true" for subregister defs. This is for keeping
  133. // liveness of the entire register (i.e. for the main range of the live
  134. // interval). For subranges, definitions of non-overlapping subregisters
  135. // do not count as uses.
  136. if (!MO.readsReg() || (IsSubRange && MO.isDef()))
  137. continue;
  138. unsigned SubReg = MO.getSubReg();
  139. if (SubReg != 0) {
  140. LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
  141. if (MO.isDef())
  142. SLM = ~SLM;
  143. // Ignore uses not reading the current (sub)range.
  144. if ((SLM & Mask).none())
  145. continue;
  146. }
  147. // Determine the actual place of the use.
  148. const MachineInstr *MI = MO.getParent();
  149. unsigned OpNo = (&MO - &MI->getOperand(0));
  150. SlotIndex UseIdx;
  151. if (MI->isPHI()) {
  152. assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
  153. // The actual place where a phi operand is used is the end of the pred
  154. // MBB. PHI operands are paired: (Reg, PredMBB).
  155. UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo + 1).getMBB());
  156. } else {
  157. // Check for early-clobber redefs.
  158. bool isEarlyClobber = false;
  159. unsigned DefIdx;
  160. if (MO.isDef())
  161. isEarlyClobber = MO.isEarlyClobber();
  162. else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
  163. // FIXME: This would be a lot easier if tied early-clobber uses also
  164. // had an early-clobber flag.
  165. isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
  166. }
  167. UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
  168. }
  169. // MI is reading Reg. We may have visited MI before if it happens to be
  170. // reading Reg multiple times. That is OK, extend() is idempotent.
  171. extend(LR, UseIdx, Reg, Undefs);
  172. }
  173. }