RenameIndependentSubregs.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //===-- RenameIndependentSubregs.cpp - Live Interval Analysis -------------===//
  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. /// Rename independent subregisters looks for virtual registers with
  10. /// independently used subregisters and renames them to new virtual registers.
  11. /// Example: In the following:
  12. /// %0:sub0<read-undef> = ...
  13. /// %0:sub1 = ...
  14. /// use %0:sub0
  15. /// %0:sub0 = ...
  16. /// use %0:sub0
  17. /// use %0:sub1
  18. /// sub0 and sub1 are never used together, and we have two independent sub0
  19. /// definitions. This pass will rename to:
  20. /// %0:sub0<read-undef> = ...
  21. /// %1:sub1<read-undef> = ...
  22. /// use %1:sub1
  23. /// %2:sub1<read-undef> = ...
  24. /// use %2:sub1
  25. /// use %0:sub0
  26. //
  27. //===----------------------------------------------------------------------===//
  28. #include "LiveRangeUtils.h"
  29. #include "PHIEliminationUtils.h"
  30. #include "llvm/CodeGen/LiveInterval.h"
  31. #include "llvm/CodeGen/LiveIntervals.h"
  32. #include "llvm/CodeGen/MachineFunctionPass.h"
  33. #include "llvm/CodeGen/MachineInstrBuilder.h"
  34. #include "llvm/CodeGen/MachineRegisterInfo.h"
  35. #include "llvm/CodeGen/Passes.h"
  36. #include "llvm/CodeGen/TargetInstrInfo.h"
  37. #include "llvm/InitializePasses.h"
  38. using namespace llvm;
  39. #define DEBUG_TYPE "rename-independent-subregs"
  40. namespace {
  41. class RenameIndependentSubregs : public MachineFunctionPass {
  42. public:
  43. static char ID;
  44. RenameIndependentSubregs() : MachineFunctionPass(ID) {}
  45. StringRef getPassName() const override {
  46. return "Rename Disconnected Subregister Components";
  47. }
  48. void getAnalysisUsage(AnalysisUsage &AU) const override {
  49. AU.setPreservesCFG();
  50. AU.addRequired<LiveIntervals>();
  51. AU.addPreserved<LiveIntervals>();
  52. AU.addRequired<SlotIndexes>();
  53. AU.addPreserved<SlotIndexes>();
  54. MachineFunctionPass::getAnalysisUsage(AU);
  55. }
  56. bool runOnMachineFunction(MachineFunction &MF) override;
  57. private:
  58. struct SubRangeInfo {
  59. ConnectedVNInfoEqClasses ConEQ;
  60. LiveInterval::SubRange *SR;
  61. unsigned Index;
  62. SubRangeInfo(LiveIntervals &LIS, LiveInterval::SubRange &SR,
  63. unsigned Index)
  64. : ConEQ(LIS), SR(&SR), Index(Index) {}
  65. };
  66. /// Split unrelated subregister components and rename them to new vregs.
  67. bool renameComponents(LiveInterval &LI) const;
  68. /// Build a vector of SubRange infos and a union find set of
  69. /// equivalence classes.
  70. /// Returns true if more than 1 equivalence class was found.
  71. bool findComponents(IntEqClasses &Classes,
  72. SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  73. LiveInterval &LI) const;
  74. /// Distribute the LiveInterval segments into the new LiveIntervals
  75. /// belonging to their class.
  76. void distribute(const IntEqClasses &Classes,
  77. const SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  78. const SmallVectorImpl<LiveInterval*> &Intervals) const;
  79. /// Constructs main liverange and add missing undef+dead flags.
  80. void computeMainRangesFixFlags(const IntEqClasses &Classes,
  81. const SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  82. const SmallVectorImpl<LiveInterval*> &Intervals) const;
  83. /// Rewrite Machine Operands to use the new vreg belonging to their class.
  84. void rewriteOperands(const IntEqClasses &Classes,
  85. const SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  86. const SmallVectorImpl<LiveInterval*> &Intervals) const;
  87. LiveIntervals *LIS;
  88. MachineRegisterInfo *MRI;
  89. const TargetInstrInfo *TII;
  90. };
  91. } // end anonymous namespace
  92. char RenameIndependentSubregs::ID;
  93. char &llvm::RenameIndependentSubregsID = RenameIndependentSubregs::ID;
  94. INITIALIZE_PASS_BEGIN(RenameIndependentSubregs, DEBUG_TYPE,
  95. "Rename Independent Subregisters", false, false)
  96. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  97. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  98. INITIALIZE_PASS_END(RenameIndependentSubregs, DEBUG_TYPE,
  99. "Rename Independent Subregisters", false, false)
  100. bool RenameIndependentSubregs::renameComponents(LiveInterval &LI) const {
  101. // Shortcut: We cannot have split components with a single definition.
  102. if (LI.valnos.size() < 2)
  103. return false;
  104. SmallVector<SubRangeInfo, 4> SubRangeInfos;
  105. IntEqClasses Classes;
  106. if (!findComponents(Classes, SubRangeInfos, LI))
  107. return false;
  108. // Create a new VReg for each class.
  109. unsigned Reg = LI.reg();
  110. const TargetRegisterClass *RegClass = MRI->getRegClass(Reg);
  111. SmallVector<LiveInterval*, 4> Intervals;
  112. Intervals.push_back(&LI);
  113. LLVM_DEBUG(dbgs() << printReg(Reg) << ": Found " << Classes.getNumClasses()
  114. << " equivalence classes.\n");
  115. LLVM_DEBUG(dbgs() << printReg(Reg) << ": Splitting into newly created:");
  116. for (unsigned I = 1, NumClasses = Classes.getNumClasses(); I < NumClasses;
  117. ++I) {
  118. Register NewVReg = MRI->createVirtualRegister(RegClass);
  119. LiveInterval &NewLI = LIS->createEmptyInterval(NewVReg);
  120. Intervals.push_back(&NewLI);
  121. LLVM_DEBUG(dbgs() << ' ' << printReg(NewVReg));
  122. }
  123. LLVM_DEBUG(dbgs() << '\n');
  124. rewriteOperands(Classes, SubRangeInfos, Intervals);
  125. distribute(Classes, SubRangeInfos, Intervals);
  126. computeMainRangesFixFlags(Classes, SubRangeInfos, Intervals);
  127. return true;
  128. }
  129. bool RenameIndependentSubregs::findComponents(IntEqClasses &Classes,
  130. SmallVectorImpl<RenameIndependentSubregs::SubRangeInfo> &SubRangeInfos,
  131. LiveInterval &LI) const {
  132. // First step: Create connected components for the VNInfos inside the
  133. // subranges and count the global number of such components.
  134. unsigned NumComponents = 0;
  135. for (LiveInterval::SubRange &SR : LI.subranges()) {
  136. SubRangeInfos.push_back(SubRangeInfo(*LIS, SR, NumComponents));
  137. ConnectedVNInfoEqClasses &ConEQ = SubRangeInfos.back().ConEQ;
  138. unsigned NumSubComponents = ConEQ.Classify(SR);
  139. NumComponents += NumSubComponents;
  140. }
  141. // Shortcut: With only 1 subrange, the normal separate component tests are
  142. // enough and we do not need to perform the union-find on the subregister
  143. // segments.
  144. if (SubRangeInfos.size() < 2)
  145. return false;
  146. // Next step: Build union-find structure over all subranges and merge classes
  147. // across subranges when they are affected by the same MachineOperand.
  148. const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
  149. Classes.grow(NumComponents);
  150. unsigned Reg = LI.reg();
  151. for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
  152. if (!MO.isDef() && !MO.readsReg())
  153. continue;
  154. unsigned SubRegIdx = MO.getSubReg();
  155. LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubRegIdx);
  156. unsigned MergedID = ~0u;
  157. for (RenameIndependentSubregs::SubRangeInfo &SRInfo : SubRangeInfos) {
  158. const LiveInterval::SubRange &SR = *SRInfo.SR;
  159. if ((SR.LaneMask & LaneMask).none())
  160. continue;
  161. SlotIndex Pos = LIS->getInstructionIndex(*MO.getParent());
  162. Pos = MO.isDef() ? Pos.getRegSlot(MO.isEarlyClobber())
  163. : Pos.getBaseIndex();
  164. const VNInfo *VNI = SR.getVNInfoAt(Pos);
  165. if (VNI == nullptr)
  166. continue;
  167. // Map to local representant ID.
  168. unsigned LocalID = SRInfo.ConEQ.getEqClass(VNI);
  169. // Global ID
  170. unsigned ID = LocalID + SRInfo.Index;
  171. // Merge other sets
  172. MergedID = MergedID == ~0u ? ID : Classes.join(MergedID, ID);
  173. }
  174. }
  175. // Early exit if we ended up with a single equivalence class.
  176. Classes.compress();
  177. unsigned NumClasses = Classes.getNumClasses();
  178. return NumClasses > 1;
  179. }
  180. void RenameIndependentSubregs::rewriteOperands(const IntEqClasses &Classes,
  181. const SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  182. const SmallVectorImpl<LiveInterval*> &Intervals) const {
  183. const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
  184. unsigned Reg = Intervals[0]->reg();
  185. for (MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(Reg),
  186. E = MRI->reg_nodbg_end(); I != E; ) {
  187. MachineOperand &MO = *I++;
  188. if (!MO.isDef() && !MO.readsReg())
  189. continue;
  190. auto *MI = MO.getParent();
  191. SlotIndex Pos = LIS->getInstructionIndex(*MI);
  192. Pos = MO.isDef() ? Pos.getRegSlot(MO.isEarlyClobber())
  193. : Pos.getBaseIndex();
  194. unsigned SubRegIdx = MO.getSubReg();
  195. LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubRegIdx);
  196. unsigned ID = ~0u;
  197. for (const SubRangeInfo &SRInfo : SubRangeInfos) {
  198. const LiveInterval::SubRange &SR = *SRInfo.SR;
  199. if ((SR.LaneMask & LaneMask).none())
  200. continue;
  201. const VNInfo *VNI = SR.getVNInfoAt(Pos);
  202. if (VNI == nullptr)
  203. continue;
  204. // Map to local representant ID.
  205. unsigned LocalID = SRInfo.ConEQ.getEqClass(VNI);
  206. // Global ID
  207. ID = Classes[LocalID + SRInfo.Index];
  208. break;
  209. }
  210. unsigned VReg = Intervals[ID]->reg();
  211. MO.setReg(VReg);
  212. if (MO.isTied() && Reg != VReg) {
  213. /// Undef use operands are not tracked in the equivalence class,
  214. /// but need to be updated if they are tied; take care to only
  215. /// update the tied operand.
  216. unsigned OperandNo = MI->getOperandNo(&MO);
  217. unsigned TiedIdx = MI->findTiedOperandIdx(OperandNo);
  218. MI->getOperand(TiedIdx).setReg(VReg);
  219. // above substitution breaks the iterator, so restart.
  220. I = MRI->reg_nodbg_begin(Reg);
  221. }
  222. }
  223. // TODO: We could attempt to recompute new register classes while visiting
  224. // the operands: Some of the split register may be fine with less constraint
  225. // classes than the original vreg.
  226. }
  227. void RenameIndependentSubregs::distribute(const IntEqClasses &Classes,
  228. const SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  229. const SmallVectorImpl<LiveInterval*> &Intervals) const {
  230. unsigned NumClasses = Classes.getNumClasses();
  231. SmallVector<unsigned, 8> VNIMapping;
  232. SmallVector<LiveInterval::SubRange*, 8> SubRanges;
  233. BumpPtrAllocator &Allocator = LIS->getVNInfoAllocator();
  234. for (const SubRangeInfo &SRInfo : SubRangeInfos) {
  235. LiveInterval::SubRange &SR = *SRInfo.SR;
  236. unsigned NumValNos = SR.valnos.size();
  237. VNIMapping.clear();
  238. VNIMapping.reserve(NumValNos);
  239. SubRanges.clear();
  240. SubRanges.resize(NumClasses-1, nullptr);
  241. for (unsigned I = 0; I < NumValNos; ++I) {
  242. const VNInfo &VNI = *SR.valnos[I];
  243. unsigned LocalID = SRInfo.ConEQ.getEqClass(&VNI);
  244. unsigned ID = Classes[LocalID + SRInfo.Index];
  245. VNIMapping.push_back(ID);
  246. if (ID > 0 && SubRanges[ID-1] == nullptr)
  247. SubRanges[ID-1] = Intervals[ID]->createSubRange(Allocator, SR.LaneMask);
  248. }
  249. DistributeRange(SR, SubRanges.data(), VNIMapping);
  250. }
  251. }
  252. static bool subRangeLiveAt(const LiveInterval &LI, SlotIndex Pos) {
  253. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  254. if (SR.liveAt(Pos))
  255. return true;
  256. }
  257. return false;
  258. }
  259. void RenameIndependentSubregs::computeMainRangesFixFlags(
  260. const IntEqClasses &Classes,
  261. const SmallVectorImpl<SubRangeInfo> &SubRangeInfos,
  262. const SmallVectorImpl<LiveInterval*> &Intervals) const {
  263. BumpPtrAllocator &Allocator = LIS->getVNInfoAllocator();
  264. const SlotIndexes &Indexes = *LIS->getSlotIndexes();
  265. for (size_t I = 0, E = Intervals.size(); I < E; ++I) {
  266. LiveInterval &LI = *Intervals[I];
  267. unsigned Reg = LI.reg();
  268. LI.removeEmptySubRanges();
  269. // There must be a def (or live-in) before every use. Splitting vregs may
  270. // violate this principle as the splitted vreg may not have a definition on
  271. // every path. Fix this by creating IMPLICIT_DEF instruction as necessary.
  272. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  273. // Search for "PHI" value numbers in the subranges. We must find a live
  274. // value in each predecessor block, add an IMPLICIT_DEF where it is
  275. // missing.
  276. for (unsigned I = 0; I < SR.valnos.size(); ++I) {
  277. const VNInfo &VNI = *SR.valnos[I];
  278. if (VNI.isUnused() || !VNI.isPHIDef())
  279. continue;
  280. SlotIndex Def = VNI.def;
  281. MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(Def);
  282. for (MachineBasicBlock *PredMBB : MBB.predecessors()) {
  283. SlotIndex PredEnd = Indexes.getMBBEndIdx(PredMBB);
  284. if (subRangeLiveAt(LI, PredEnd.getPrevSlot()))
  285. continue;
  286. MachineBasicBlock::iterator InsertPos =
  287. llvm::findPHICopyInsertPoint(PredMBB, &MBB, Reg);
  288. const MCInstrDesc &MCDesc = TII->get(TargetOpcode::IMPLICIT_DEF);
  289. MachineInstrBuilder ImpDef = BuildMI(*PredMBB, InsertPos,
  290. DebugLoc(), MCDesc, Reg);
  291. SlotIndex DefIdx = LIS->InsertMachineInstrInMaps(*ImpDef);
  292. SlotIndex RegDefIdx = DefIdx.getRegSlot();
  293. for (LiveInterval::SubRange &SR : LI.subranges()) {
  294. VNInfo *SRVNI = SR.getNextValue(RegDefIdx, Allocator);
  295. SR.addSegment(LiveRange::Segment(RegDefIdx, PredEnd, SRVNI));
  296. }
  297. }
  298. }
  299. }
  300. for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
  301. if (!MO.isDef())
  302. continue;
  303. unsigned SubRegIdx = MO.getSubReg();
  304. if (SubRegIdx == 0)
  305. continue;
  306. // After assigning the new vreg we may not have any other sublanes living
  307. // in and out of the instruction anymore. We need to add new dead and
  308. // undef flags in these cases.
  309. if (!MO.isUndef()) {
  310. SlotIndex Pos = LIS->getInstructionIndex(*MO.getParent());
  311. if (!subRangeLiveAt(LI, Pos))
  312. MO.setIsUndef();
  313. }
  314. if (!MO.isDead()) {
  315. SlotIndex Pos = LIS->getInstructionIndex(*MO.getParent()).getDeadSlot();
  316. if (!subRangeLiveAt(LI, Pos))
  317. MO.setIsDead();
  318. }
  319. }
  320. if (I == 0)
  321. LI.clear();
  322. LIS->constructMainRangeFromSubranges(LI);
  323. // A def of a subregister may be a use of other register lanes. Replacing
  324. // such a def with a def of a different register will eliminate the use,
  325. // and may cause the recorded live range to be larger than the actual
  326. // liveness in the program IR.
  327. LIS->shrinkToUses(&LI);
  328. }
  329. }
  330. bool RenameIndependentSubregs::runOnMachineFunction(MachineFunction &MF) {
  331. // Skip renaming if liveness of subregister is not tracked.
  332. MRI = &MF.getRegInfo();
  333. if (!MRI->subRegLivenessEnabled())
  334. return false;
  335. LLVM_DEBUG(dbgs() << "Renaming independent subregister live ranges in "
  336. << MF.getName() << '\n');
  337. LIS = &getAnalysis<LiveIntervals>();
  338. TII = MF.getSubtarget().getInstrInfo();
  339. // Iterate over all vregs. Note that we query getNumVirtRegs() the newly
  340. // created vregs end up with higher numbers but do not need to be visited as
  341. // there can't be any further splitting.
  342. bool Changed = false;
  343. for (size_t I = 0, E = MRI->getNumVirtRegs(); I < E; ++I) {
  344. unsigned Reg = Register::index2VirtReg(I);
  345. if (!LIS->hasInterval(Reg))
  346. continue;
  347. LiveInterval &LI = LIS->getInterval(Reg);
  348. if (!LI.hasSubRanges())
  349. continue;
  350. Changed |= renameComponents(LI);
  351. }
  352. return Changed;
  353. }