LiveIntervalCalc.cpp 7.6 KB

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