1a06bfe05SJan Sjodin //===- AMDGPUMachineCFGStructurizer.cpp - Machine code if conversion pass. ===// 2a06bfe05SJan Sjodin // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a06bfe05SJan Sjodin // 7a06bfe05SJan Sjodin //===----------------------------------------------------------------------===// 8a06bfe05SJan Sjodin // 9a06bfe05SJan Sjodin // This file implements the machine instruction level CFG structurizer pass. 10a06bfe05SJan Sjodin // 11a06bfe05SJan Sjodin //===----------------------------------------------------------------------===// 12a06bfe05SJan Sjodin 13a06bfe05SJan Sjodin #include "AMDGPU.h" 14560d7e04Sdfukalov #include "GCNSubtarget.h" 15a06bfe05SJan Sjodin #include "llvm/ADT/DenseSet.h" 16a06bfe05SJan Sjodin #include "llvm/ADT/PostOrderIterator.h" 17a06bfe05SJan Sjodin #include "llvm/ADT/SetVector.h" 18a06bfe05SJan Sjodin #include "llvm/ADT/SmallPtrSet.h" 19a06bfe05SJan Sjodin #include "llvm/CodeGen/MachineBasicBlock.h" 20a06bfe05SJan Sjodin #include "llvm/CodeGen/MachineInstrBuilder.h" 21a06bfe05SJan Sjodin #include "llvm/CodeGen/MachineRegionInfo.h" 22a06bfe05SJan Sjodin #include "llvm/CodeGen/MachineRegisterInfo.h" 23b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetRegisterInfo.h" 2405da2fe5SReid Kleckner #include "llvm/InitializePasses.h" 25d16eff81SEugene Zelenko 26a06bfe05SJan Sjodin using namespace llvm; 27a06bfe05SJan Sjodin 28a06bfe05SJan Sjodin #define DEBUG_TYPE "amdgpucfgstructurizer" 29a06bfe05SJan Sjodin 30a06bfe05SJan Sjodin namespace { 31d16eff81SEugene Zelenko 32a06bfe05SJan Sjodin class PHILinearizeDestIterator; 33a06bfe05SJan Sjodin 34a06bfe05SJan Sjodin class PHILinearize { 35a06bfe05SJan Sjodin friend class PHILinearizeDestIterator; 36a06bfe05SJan Sjodin 37a06bfe05SJan Sjodin public: 38d16eff81SEugene Zelenko using PHISourceT = std::pair<unsigned, MachineBasicBlock *>; 39a06bfe05SJan Sjodin 40a06bfe05SJan Sjodin private: 41d16eff81SEugene Zelenko using PHISourcesT = DenseSet<PHISourceT>; 42d16eff81SEugene Zelenko using PHIInfoElementT = struct { 43a06bfe05SJan Sjodin unsigned DestReg; 44a06bfe05SJan Sjodin DebugLoc DL; 45a06bfe05SJan Sjodin PHISourcesT Sources; 46d16eff81SEugene Zelenko }; 47d16eff81SEugene Zelenko using PHIInfoT = SmallPtrSet<PHIInfoElementT *, 2>; 48a06bfe05SJan Sjodin PHIInfoT PHIInfo; 49a06bfe05SJan Sjodin 50a06bfe05SJan Sjodin static unsigned phiInfoElementGetDest(PHIInfoElementT *Info); 51a06bfe05SJan Sjodin static void phiInfoElementSetDef(PHIInfoElementT *Info, unsigned NewDef); 52a06bfe05SJan Sjodin static PHISourcesT &phiInfoElementGetSources(PHIInfoElementT *Info); 53a06bfe05SJan Sjodin static void phiInfoElementAddSource(PHIInfoElementT *Info, unsigned SourceReg, 54a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB); 55a06bfe05SJan Sjodin static void phiInfoElementRemoveSource(PHIInfoElementT *Info, 56a06bfe05SJan Sjodin unsigned SourceReg, 57a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB); 58a06bfe05SJan Sjodin PHIInfoElementT *findPHIInfoElement(unsigned DestReg); 59a06bfe05SJan Sjodin PHIInfoElementT *findPHIInfoElementFromSource(unsigned SourceReg, 60a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB); 61a06bfe05SJan Sjodin 62a06bfe05SJan Sjodin public: 63a06bfe05SJan Sjodin bool findSourcesFromMBB(MachineBasicBlock *SourceMBB, 64a06bfe05SJan Sjodin SmallVector<unsigned, 4> &Sources); 65a06bfe05SJan Sjodin void addDest(unsigned DestReg, const DebugLoc &DL); 66a06bfe05SJan Sjodin void replaceDef(unsigned OldDestReg, unsigned NewDestReg); 67a06bfe05SJan Sjodin void deleteDef(unsigned DestReg); 68a06bfe05SJan Sjodin void addSource(unsigned DestReg, unsigned SourceReg, 69a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB); 70a06bfe05SJan Sjodin void removeSource(unsigned DestReg, unsigned SourceReg, 71a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB = nullptr); 72a06bfe05SJan Sjodin bool findDest(unsigned SourceReg, MachineBasicBlock *SourceMBB, 73a06bfe05SJan Sjodin unsigned &DestReg); 74a06bfe05SJan Sjodin bool isSource(unsigned Reg, MachineBasicBlock *SourceMBB = nullptr); 75a06bfe05SJan Sjodin unsigned getNumSources(unsigned DestReg); 76a06bfe05SJan Sjodin void dump(MachineRegisterInfo *MRI); 77a06bfe05SJan Sjodin void clear(); 78a06bfe05SJan Sjodin 79d16eff81SEugene Zelenko using source_iterator = PHISourcesT::iterator; 80d16eff81SEugene Zelenko using dest_iterator = PHILinearizeDestIterator; 81a06bfe05SJan Sjodin 82a06bfe05SJan Sjodin dest_iterator dests_begin(); 83a06bfe05SJan Sjodin dest_iterator dests_end(); 84a06bfe05SJan Sjodin 85a06bfe05SJan Sjodin source_iterator sources_begin(unsigned Reg); 86a06bfe05SJan Sjodin source_iterator sources_end(unsigned Reg); 87a06bfe05SJan Sjodin }; 88a06bfe05SJan Sjodin 89a06bfe05SJan Sjodin class PHILinearizeDestIterator { 90a06bfe05SJan Sjodin private: 91a06bfe05SJan Sjodin PHILinearize::PHIInfoT::iterator Iter; 92a06bfe05SJan Sjodin 93a06bfe05SJan Sjodin public: 94d16eff81SEugene Zelenko PHILinearizeDestIterator(PHILinearize::PHIInfoT::iterator I) : Iter(I) {} 95d16eff81SEugene Zelenko 96a06bfe05SJan Sjodin unsigned operator*() { return PHILinearize::phiInfoElementGetDest(*Iter); } 97a06bfe05SJan Sjodin PHILinearizeDestIterator &operator++() { 98a06bfe05SJan Sjodin ++Iter; 99a06bfe05SJan Sjodin return *this; 100a06bfe05SJan Sjodin } 101a06bfe05SJan Sjodin bool operator==(const PHILinearizeDestIterator &I) const { 102a06bfe05SJan Sjodin return I.Iter == Iter; 103a06bfe05SJan Sjodin } 104a06bfe05SJan Sjodin bool operator!=(const PHILinearizeDestIterator &I) const { 105a06bfe05SJan Sjodin return I.Iter != Iter; 106a06bfe05SJan Sjodin } 107a06bfe05SJan Sjodin }; 108a06bfe05SJan Sjodin 109d16eff81SEugene Zelenko } // end anonymous namespace 110d16eff81SEugene Zelenko 111a06bfe05SJan Sjodin unsigned PHILinearize::phiInfoElementGetDest(PHIInfoElementT *Info) { 112a06bfe05SJan Sjodin return Info->DestReg; 113a06bfe05SJan Sjodin } 114a06bfe05SJan Sjodin 115a06bfe05SJan Sjodin void PHILinearize::phiInfoElementSetDef(PHIInfoElementT *Info, 116a06bfe05SJan Sjodin unsigned NewDef) { 117a06bfe05SJan Sjodin Info->DestReg = NewDef; 118a06bfe05SJan Sjodin } 119a06bfe05SJan Sjodin 120a06bfe05SJan Sjodin PHILinearize::PHISourcesT & 121a06bfe05SJan Sjodin PHILinearize::phiInfoElementGetSources(PHIInfoElementT *Info) { 122a06bfe05SJan Sjodin return Info->Sources; 123a06bfe05SJan Sjodin } 124a06bfe05SJan Sjodin 125a06bfe05SJan Sjodin void PHILinearize::phiInfoElementAddSource(PHIInfoElementT *Info, 126a06bfe05SJan Sjodin unsigned SourceReg, 127a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB) { 128a06bfe05SJan Sjodin // Assertion ensures we don't use the same SourceMBB for the 129a06bfe05SJan Sjodin // sources, because we cannot have different registers with 130a06bfe05SJan Sjodin // identical predecessors, but we can have the same register for 131a06bfe05SJan Sjodin // multiple predecessors. 132994a43d2SNAKAMURA Takumi #if !defined(NDEBUG) 133a06bfe05SJan Sjodin for (auto SI : phiInfoElementGetSources(Info)) { 134a06bfe05SJan Sjodin assert((SI.second != SourceMBB || SourceReg == SI.first)); 135a06bfe05SJan Sjodin } 136994a43d2SNAKAMURA Takumi #endif 137a06bfe05SJan Sjodin 138a06bfe05SJan Sjodin phiInfoElementGetSources(Info).insert(PHISourceT(SourceReg, SourceMBB)); 139a06bfe05SJan Sjodin } 140a06bfe05SJan Sjodin 141a06bfe05SJan Sjodin void PHILinearize::phiInfoElementRemoveSource(PHIInfoElementT *Info, 142a06bfe05SJan Sjodin unsigned SourceReg, 143a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB) { 144a06bfe05SJan Sjodin auto &Sources = phiInfoElementGetSources(Info); 145a06bfe05SJan Sjodin SmallVector<PHISourceT, 4> ElimiatedSources; 146a06bfe05SJan Sjodin for (auto SI : Sources) { 147a06bfe05SJan Sjodin if (SI.first == SourceReg && 148a06bfe05SJan Sjodin (SI.second == nullptr || SI.second == SourceMBB)) { 149a06bfe05SJan Sjodin ElimiatedSources.push_back(PHISourceT(SI.first, SI.second)); 150a06bfe05SJan Sjodin } 151a06bfe05SJan Sjodin } 152a06bfe05SJan Sjodin 153a06bfe05SJan Sjodin for (auto &Source : ElimiatedSources) { 154a06bfe05SJan Sjodin Sources.erase(Source); 155a06bfe05SJan Sjodin } 156a06bfe05SJan Sjodin } 157a06bfe05SJan Sjodin 158a06bfe05SJan Sjodin PHILinearize::PHIInfoElementT * 159a06bfe05SJan Sjodin PHILinearize::findPHIInfoElement(unsigned DestReg) { 160a06bfe05SJan Sjodin for (auto I : PHIInfo) { 161a06bfe05SJan Sjodin if (phiInfoElementGetDest(I) == DestReg) { 162a06bfe05SJan Sjodin return I; 163a06bfe05SJan Sjodin } 164a06bfe05SJan Sjodin } 165a06bfe05SJan Sjodin return nullptr; 166a06bfe05SJan Sjodin } 167a06bfe05SJan Sjodin 168a06bfe05SJan Sjodin PHILinearize::PHIInfoElementT * 169a06bfe05SJan Sjodin PHILinearize::findPHIInfoElementFromSource(unsigned SourceReg, 170a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB) { 171a06bfe05SJan Sjodin for (auto I : PHIInfo) { 172a06bfe05SJan Sjodin for (auto SI : phiInfoElementGetSources(I)) { 173a06bfe05SJan Sjodin if (SI.first == SourceReg && 174a06bfe05SJan Sjodin (SI.second == nullptr || SI.second == SourceMBB)) { 175a06bfe05SJan Sjodin return I; 176a06bfe05SJan Sjodin } 177a06bfe05SJan Sjodin } 178a06bfe05SJan Sjodin } 179a06bfe05SJan Sjodin return nullptr; 180a06bfe05SJan Sjodin } 181a06bfe05SJan Sjodin 182a06bfe05SJan Sjodin bool PHILinearize::findSourcesFromMBB(MachineBasicBlock *SourceMBB, 183a06bfe05SJan Sjodin SmallVector<unsigned, 4> &Sources) { 184a06bfe05SJan Sjodin bool FoundSource = false; 185a06bfe05SJan Sjodin for (auto I : PHIInfo) { 186a06bfe05SJan Sjodin for (auto SI : phiInfoElementGetSources(I)) { 187a06bfe05SJan Sjodin if (SI.second == SourceMBB) { 188a06bfe05SJan Sjodin FoundSource = true; 189a06bfe05SJan Sjodin Sources.push_back(SI.first); 190a06bfe05SJan Sjodin } 191a06bfe05SJan Sjodin } 192a06bfe05SJan Sjodin } 193a06bfe05SJan Sjodin return FoundSource; 194a06bfe05SJan Sjodin } 195a06bfe05SJan Sjodin 196a06bfe05SJan Sjodin void PHILinearize::addDest(unsigned DestReg, const DebugLoc &DL) { 197*dc6e8dfdSJacob Lambert assert(findPHIInfoElement(DestReg) == nullptr && "Dest already exists"); 198a06bfe05SJan Sjodin PHISourcesT EmptySet; 199a06bfe05SJan Sjodin PHIInfoElementT *NewElement = new PHIInfoElementT(); 200a06bfe05SJan Sjodin NewElement->DestReg = DestReg; 201a06bfe05SJan Sjodin NewElement->DL = DL; 202a06bfe05SJan Sjodin NewElement->Sources = EmptySet; 203a06bfe05SJan Sjodin PHIInfo.insert(NewElement); 204a06bfe05SJan Sjodin } 205a06bfe05SJan Sjodin 206a06bfe05SJan Sjodin void PHILinearize::replaceDef(unsigned OldDestReg, unsigned NewDestReg) { 207a06bfe05SJan Sjodin phiInfoElementSetDef(findPHIInfoElement(OldDestReg), NewDestReg); 208a06bfe05SJan Sjodin } 209a06bfe05SJan Sjodin 210a06bfe05SJan Sjodin void PHILinearize::deleteDef(unsigned DestReg) { 211a06bfe05SJan Sjodin PHIInfoElementT *InfoElement = findPHIInfoElement(DestReg); 212a06bfe05SJan Sjodin PHIInfo.erase(InfoElement); 213a06bfe05SJan Sjodin delete InfoElement; 214a06bfe05SJan Sjodin } 215a06bfe05SJan Sjodin 216a06bfe05SJan Sjodin void PHILinearize::addSource(unsigned DestReg, unsigned SourceReg, 217a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB) { 218a06bfe05SJan Sjodin phiInfoElementAddSource(findPHIInfoElement(DestReg), SourceReg, SourceMBB); 219a06bfe05SJan Sjodin } 220a06bfe05SJan Sjodin 221a06bfe05SJan Sjodin void PHILinearize::removeSource(unsigned DestReg, unsigned SourceReg, 222a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB) { 223a06bfe05SJan Sjodin phiInfoElementRemoveSource(findPHIInfoElement(DestReg), SourceReg, SourceMBB); 224a06bfe05SJan Sjodin } 225a06bfe05SJan Sjodin 226a06bfe05SJan Sjodin bool PHILinearize::findDest(unsigned SourceReg, MachineBasicBlock *SourceMBB, 227a06bfe05SJan Sjodin unsigned &DestReg) { 228a06bfe05SJan Sjodin PHIInfoElementT *InfoElement = 229a06bfe05SJan Sjodin findPHIInfoElementFromSource(SourceReg, SourceMBB); 230a06bfe05SJan Sjodin if (InfoElement != nullptr) { 231a06bfe05SJan Sjodin DestReg = phiInfoElementGetDest(InfoElement); 232a06bfe05SJan Sjodin return true; 233a06bfe05SJan Sjodin } 234a06bfe05SJan Sjodin return false; 235a06bfe05SJan Sjodin } 236a06bfe05SJan Sjodin 237a06bfe05SJan Sjodin bool PHILinearize::isSource(unsigned Reg, MachineBasicBlock *SourceMBB) { 238a06bfe05SJan Sjodin unsigned DestReg; 239a06bfe05SJan Sjodin return findDest(Reg, SourceMBB, DestReg); 240a06bfe05SJan Sjodin } 241a06bfe05SJan Sjodin 242a06bfe05SJan Sjodin unsigned PHILinearize::getNumSources(unsigned DestReg) { 243a06bfe05SJan Sjodin return phiInfoElementGetSources(findPHIInfoElement(DestReg)).size(); 244a06bfe05SJan Sjodin } 245a06bfe05SJan Sjodin 246615eb470SAaron Ballman #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2476b3216aaSFlorian Hahn LLVM_DUMP_METHOD void PHILinearize::dump(MachineRegisterInfo *MRI) { 248a06bfe05SJan Sjodin const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); 249a06bfe05SJan Sjodin dbgs() << "=PHIInfo Start=\n"; 250a06bfe05SJan Sjodin for (auto PII : this->PHIInfo) { 251a06bfe05SJan Sjodin PHIInfoElementT &Element = *PII; 2529d419d3bSFrancis Visoiu Mistrih dbgs() << "Dest: " << printReg(Element.DestReg, TRI) 253a06bfe05SJan Sjodin << " Sources: {"; 254a06bfe05SJan Sjodin for (auto &SI : Element.Sources) { 25525528d6dSFrancis Visoiu Mistrih dbgs() << printReg(SI.first, TRI) << '(' << printMBBReference(*SI.second) 25625528d6dSFrancis Visoiu Mistrih << "),"; 257a06bfe05SJan Sjodin } 258a06bfe05SJan Sjodin dbgs() << "}\n"; 259a06bfe05SJan Sjodin } 260a06bfe05SJan Sjodin dbgs() << "=PHIInfo End=\n"; 261a06bfe05SJan Sjodin } 2626b3216aaSFlorian Hahn #endif 263a06bfe05SJan Sjodin 264a06bfe05SJan Sjodin void PHILinearize::clear() { PHIInfo = PHIInfoT(); } 265a06bfe05SJan Sjodin 266a06bfe05SJan Sjodin PHILinearize::dest_iterator PHILinearize::dests_begin() { 267a06bfe05SJan Sjodin return PHILinearizeDestIterator(PHIInfo.begin()); 268a06bfe05SJan Sjodin } 269a06bfe05SJan Sjodin 270a06bfe05SJan Sjodin PHILinearize::dest_iterator PHILinearize::dests_end() { 271a06bfe05SJan Sjodin return PHILinearizeDestIterator(PHIInfo.end()); 272a06bfe05SJan Sjodin } 273a06bfe05SJan Sjodin 274a06bfe05SJan Sjodin PHILinearize::source_iterator PHILinearize::sources_begin(unsigned Reg) { 275a06bfe05SJan Sjodin auto InfoElement = findPHIInfoElement(Reg); 276a06bfe05SJan Sjodin return phiInfoElementGetSources(InfoElement).begin(); 277a06bfe05SJan Sjodin } 278d16eff81SEugene Zelenko 279a06bfe05SJan Sjodin PHILinearize::source_iterator PHILinearize::sources_end(unsigned Reg) { 280a06bfe05SJan Sjodin auto InfoElement = findPHIInfoElement(Reg); 281a06bfe05SJan Sjodin return phiInfoElementGetSources(InfoElement).end(); 282a06bfe05SJan Sjodin } 283a06bfe05SJan Sjodin 284a06bfe05SJan Sjodin static unsigned getPHINumInputs(MachineInstr &PHI) { 285a06bfe05SJan Sjodin assert(PHI.isPHI()); 286a06bfe05SJan Sjodin return (PHI.getNumOperands() - 1) / 2; 287a06bfe05SJan Sjodin } 288a06bfe05SJan Sjodin 289a06bfe05SJan Sjodin static MachineBasicBlock *getPHIPred(MachineInstr &PHI, unsigned Index) { 290a06bfe05SJan Sjodin assert(PHI.isPHI()); 291a06bfe05SJan Sjodin return PHI.getOperand(Index * 2 + 2).getMBB(); 292a06bfe05SJan Sjodin } 293a06bfe05SJan Sjodin 294a06bfe05SJan Sjodin static void setPhiPred(MachineInstr &PHI, unsigned Index, 295a06bfe05SJan Sjodin MachineBasicBlock *NewPred) { 296a06bfe05SJan Sjodin PHI.getOperand(Index * 2 + 2).setMBB(NewPred); 297a06bfe05SJan Sjodin } 298a06bfe05SJan Sjodin 299a06bfe05SJan Sjodin static unsigned getPHISourceReg(MachineInstr &PHI, unsigned Index) { 300a06bfe05SJan Sjodin assert(PHI.isPHI()); 301a06bfe05SJan Sjodin return PHI.getOperand(Index * 2 + 1).getReg(); 302a06bfe05SJan Sjodin } 303a06bfe05SJan Sjodin 304a06bfe05SJan Sjodin static unsigned getPHIDestReg(MachineInstr &PHI) { 305a06bfe05SJan Sjodin assert(PHI.isPHI()); 306a06bfe05SJan Sjodin return PHI.getOperand(0).getReg(); 307a06bfe05SJan Sjodin } 308a06bfe05SJan Sjodin 309d16eff81SEugene Zelenko namespace { 310d16eff81SEugene Zelenko 311d16eff81SEugene Zelenko class RegionMRT; 312d16eff81SEugene Zelenko class MBBMRT; 313d16eff81SEugene Zelenko 314a06bfe05SJan Sjodin class LinearizedRegion { 315a06bfe05SJan Sjodin protected: 316a06bfe05SJan Sjodin MachineBasicBlock *Entry; 317a06bfe05SJan Sjodin // The exit block is part of the region, and is the last 318a06bfe05SJan Sjodin // merge block before exiting the region. 319a06bfe05SJan Sjodin MachineBasicBlock *Exit; 320a06bfe05SJan Sjodin DenseSet<unsigned> LiveOuts; 321a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 1> MBBs; 322a06bfe05SJan Sjodin bool HasLoop; 323a06bfe05SJan Sjodin LinearizedRegion *Parent; 324a06bfe05SJan Sjodin RegionMRT *RMRT; 325a06bfe05SJan Sjodin 32634978602SJay Foad void storeLiveOutReg(MachineBasicBlock *MBB, Register Reg, 327a06bfe05SJan Sjodin MachineInstr *DefInstr, const MachineRegisterInfo *MRI, 328a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); 329a06bfe05SJan Sjodin 33034978602SJay Foad void storeLiveOutRegRegion(RegionMRT *Region, Register Reg, 331a06bfe05SJan Sjodin MachineInstr *DefInstr, 332a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 333a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 334a06bfe05SJan Sjodin PHILinearize &PHIInfo); 335a06bfe05SJan Sjodin 336a06bfe05SJan Sjodin void storeMBBLiveOuts(MachineBasicBlock *MBB, const MachineRegisterInfo *MRI, 337a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, PHILinearize &PHIInfo, 338a06bfe05SJan Sjodin RegionMRT *TopRegion); 339a06bfe05SJan Sjodin 340a06bfe05SJan Sjodin void storeLiveOuts(MachineBasicBlock *MBB, const MachineRegisterInfo *MRI, 341a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); 342a06bfe05SJan Sjodin 343a06bfe05SJan Sjodin void storeLiveOuts(RegionMRT *Region, const MachineRegisterInfo *MRI, 344a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, PHILinearize &PHIInfo, 345a06bfe05SJan Sjodin RegionMRT *TopRegion = nullptr); 346a06bfe05SJan Sjodin 347a06bfe05SJan Sjodin public: 348d16eff81SEugene Zelenko LinearizedRegion(); 349d16eff81SEugene Zelenko LinearizedRegion(MachineBasicBlock *MBB, const MachineRegisterInfo *MRI, 350d16eff81SEugene Zelenko const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); 351d16eff81SEugene Zelenko ~LinearizedRegion() = default; 352d16eff81SEugene Zelenko 353a06bfe05SJan Sjodin void setRegionMRT(RegionMRT *Region) { RMRT = Region; } 354a06bfe05SJan Sjodin 355a06bfe05SJan Sjodin RegionMRT *getRegionMRT() { return RMRT; } 356a06bfe05SJan Sjodin 357a06bfe05SJan Sjodin void setParent(LinearizedRegion *P) { Parent = P; } 358a06bfe05SJan Sjodin 359a06bfe05SJan Sjodin LinearizedRegion *getParent() { return Parent; } 360a06bfe05SJan Sjodin 361a06bfe05SJan Sjodin void print(raw_ostream &OS, const TargetRegisterInfo *TRI = nullptr); 362a06bfe05SJan Sjodin 363a06bfe05SJan Sjodin void setBBSelectRegIn(unsigned Reg); 364a06bfe05SJan Sjodin 365a06bfe05SJan Sjodin unsigned getBBSelectRegIn(); 366a06bfe05SJan Sjodin 367a06bfe05SJan Sjodin void setBBSelectRegOut(unsigned Reg, bool IsLiveOut); 368a06bfe05SJan Sjodin 369a06bfe05SJan Sjodin unsigned getBBSelectRegOut(); 370a06bfe05SJan Sjodin 371a06bfe05SJan Sjodin void setHasLoop(bool Value); 372a06bfe05SJan Sjodin 373a06bfe05SJan Sjodin bool getHasLoop(); 374a06bfe05SJan Sjodin 375a06bfe05SJan Sjodin void addLiveOut(unsigned VReg); 376a06bfe05SJan Sjodin 377a06bfe05SJan Sjodin void removeLiveOut(unsigned Reg); 378a06bfe05SJan Sjodin 379a06bfe05SJan Sjodin void replaceLiveOut(unsigned OldReg, unsigned NewReg); 380a06bfe05SJan Sjodin 38134978602SJay Foad void replaceRegister(unsigned Register, class Register NewRegister, 382a06bfe05SJan Sjodin MachineRegisterInfo *MRI, bool ReplaceInside, 383a06bfe05SJan Sjodin bool ReplaceOutside, bool IncludeLoopPHIs); 384a06bfe05SJan Sjodin 385a06bfe05SJan Sjodin void replaceRegisterInsideRegion(unsigned Register, unsigned NewRegister, 386a06bfe05SJan Sjodin bool IncludeLoopPHIs, 387a06bfe05SJan Sjodin MachineRegisterInfo *MRI); 388a06bfe05SJan Sjodin 389a06bfe05SJan Sjodin void replaceRegisterOutsideRegion(unsigned Register, unsigned NewRegister, 390a06bfe05SJan Sjodin bool IncludeLoopPHIs, 391a06bfe05SJan Sjodin MachineRegisterInfo *MRI); 392a06bfe05SJan Sjodin 393a06bfe05SJan Sjodin DenseSet<unsigned> *getLiveOuts(); 394a06bfe05SJan Sjodin 395a06bfe05SJan Sjodin void setEntry(MachineBasicBlock *NewEntry); 396a06bfe05SJan Sjodin 397a06bfe05SJan Sjodin MachineBasicBlock *getEntry(); 398a06bfe05SJan Sjodin 399a06bfe05SJan Sjodin void setExit(MachineBasicBlock *NewExit); 400a06bfe05SJan Sjodin 401a06bfe05SJan Sjodin MachineBasicBlock *getExit(); 402a06bfe05SJan Sjodin 403a06bfe05SJan Sjodin void addMBB(MachineBasicBlock *MBB); 404a06bfe05SJan Sjodin 405a06bfe05SJan Sjodin void addMBBs(LinearizedRegion *InnerRegion); 406a06bfe05SJan Sjodin 407a06bfe05SJan Sjodin bool contains(MachineBasicBlock *MBB); 408a06bfe05SJan Sjodin 409a06bfe05SJan Sjodin bool isLiveOut(unsigned Reg); 410a06bfe05SJan Sjodin 411a06bfe05SJan Sjodin bool hasNoDef(unsigned Reg, MachineRegisterInfo *MRI); 412a06bfe05SJan Sjodin 413a06bfe05SJan Sjodin void removeFalseRegisterKills(MachineRegisterInfo *MRI); 414a06bfe05SJan Sjodin 415a06bfe05SJan Sjodin void initLiveOut(RegionMRT *Region, const MachineRegisterInfo *MRI, 416a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); 417a06bfe05SJan Sjodin }; 418a06bfe05SJan Sjodin 419a06bfe05SJan Sjodin class MRT { 420a06bfe05SJan Sjodin protected: 421a06bfe05SJan Sjodin RegionMRT *Parent; 422a06bfe05SJan Sjodin unsigned BBSelectRegIn; 423a06bfe05SJan Sjodin unsigned BBSelectRegOut; 424a06bfe05SJan Sjodin 425a06bfe05SJan Sjodin public: 426d16eff81SEugene Zelenko virtual ~MRT() = default; 427d16eff81SEugene Zelenko 428a06bfe05SJan Sjodin unsigned getBBSelectRegIn() { return BBSelectRegIn; } 429a06bfe05SJan Sjodin 430a06bfe05SJan Sjodin unsigned getBBSelectRegOut() { return BBSelectRegOut; } 431a06bfe05SJan Sjodin 432a06bfe05SJan Sjodin void setBBSelectRegIn(unsigned Reg) { BBSelectRegIn = Reg; } 433a06bfe05SJan Sjodin 434a06bfe05SJan Sjodin void setBBSelectRegOut(unsigned Reg) { BBSelectRegOut = Reg; } 435a06bfe05SJan Sjodin 436a06bfe05SJan Sjodin virtual RegionMRT *getRegionMRT() { return nullptr; } 437a06bfe05SJan Sjodin 438a06bfe05SJan Sjodin virtual MBBMRT *getMBBMRT() { return nullptr; } 439a06bfe05SJan Sjodin 440a06bfe05SJan Sjodin bool isRegion() { return getRegionMRT() != nullptr; } 441a06bfe05SJan Sjodin 442a06bfe05SJan Sjodin bool isMBB() { return getMBBMRT() != nullptr; } 443a06bfe05SJan Sjodin 444a06bfe05SJan Sjodin bool isRoot() { return Parent == nullptr; } 445a06bfe05SJan Sjodin 446a06bfe05SJan Sjodin void setParent(RegionMRT *Region) { Parent = Region; } 447a06bfe05SJan Sjodin 448a06bfe05SJan Sjodin RegionMRT *getParent() { return Parent; } 449a06bfe05SJan Sjodin 450a06bfe05SJan Sjodin static MachineBasicBlock * 451a06bfe05SJan Sjodin initializeMRT(MachineFunction &MF, const MachineRegionInfo *RegionInfo, 452a06bfe05SJan Sjodin DenseMap<MachineRegion *, RegionMRT *> &RegionMap); 453a06bfe05SJan Sjodin 454a06bfe05SJan Sjodin static RegionMRT *buildMRT(MachineFunction &MF, 455a06bfe05SJan Sjodin const MachineRegionInfo *RegionInfo, 456a06bfe05SJan Sjodin const SIInstrInfo *TII, 457a06bfe05SJan Sjodin MachineRegisterInfo *MRI); 458a06bfe05SJan Sjodin 459a06bfe05SJan Sjodin virtual void dump(const TargetRegisterInfo *TRI, int depth = 0) = 0; 460a06bfe05SJan Sjodin 461a06bfe05SJan Sjodin void dumpDepth(int depth) { 462a06bfe05SJan Sjodin for (int i = depth; i > 0; --i) { 463a06bfe05SJan Sjodin dbgs() << " "; 464a06bfe05SJan Sjodin } 465a06bfe05SJan Sjodin } 466a06bfe05SJan Sjodin }; 467a06bfe05SJan Sjodin 468a06bfe05SJan Sjodin class MBBMRT : public MRT { 469a06bfe05SJan Sjodin MachineBasicBlock *MBB; 470a06bfe05SJan Sjodin 471a06bfe05SJan Sjodin public: 472a06bfe05SJan Sjodin MBBMRT(MachineBasicBlock *BB) : MBB(BB) { 473a06bfe05SJan Sjodin setParent(nullptr); 474a06bfe05SJan Sjodin setBBSelectRegOut(0); 475a06bfe05SJan Sjodin setBBSelectRegIn(0); 476a06bfe05SJan Sjodin } 477d16eff81SEugene Zelenko 478d16eff81SEugene Zelenko MBBMRT *getMBBMRT() override { return this; } 479d16eff81SEugene Zelenko 480d16eff81SEugene Zelenko MachineBasicBlock *getMBB() { return MBB; } 481d16eff81SEugene Zelenko 482d16eff81SEugene Zelenko void dump(const TargetRegisterInfo *TRI, int depth = 0) override { 483d16eff81SEugene Zelenko dumpDepth(depth); 484d16eff81SEugene Zelenko dbgs() << "MBB: " << getMBB()->getNumber(); 4859d419d3bSFrancis Visoiu Mistrih dbgs() << " In: " << printReg(getBBSelectRegIn(), TRI); 4869d419d3bSFrancis Visoiu Mistrih dbgs() << ", Out: " << printReg(getBBSelectRegOut(), TRI) << "\n"; 487d16eff81SEugene Zelenko } 488a06bfe05SJan Sjodin }; 489a06bfe05SJan Sjodin 490a06bfe05SJan Sjodin class RegionMRT : public MRT { 491a06bfe05SJan Sjodin protected: 492a06bfe05SJan Sjodin MachineRegion *Region; 493d16eff81SEugene Zelenko LinearizedRegion *LRegion = nullptr; 494d16eff81SEugene Zelenko MachineBasicBlock *Succ = nullptr; 495a06bfe05SJan Sjodin SetVector<MRT *> Children; 496a06bfe05SJan Sjodin 497a06bfe05SJan Sjodin public: 498d16eff81SEugene Zelenko RegionMRT(MachineRegion *MachineRegion) : Region(MachineRegion) { 499d16eff81SEugene Zelenko setParent(nullptr); 500d16eff81SEugene Zelenko setBBSelectRegOut(0); 501d16eff81SEugene Zelenko setBBSelectRegIn(0); 502d16eff81SEugene Zelenko } 503d16eff81SEugene Zelenko 504d16eff81SEugene Zelenko ~RegionMRT() override { 505d16eff81SEugene Zelenko if (LRegion) { 506d16eff81SEugene Zelenko delete LRegion; 507d16eff81SEugene Zelenko } 508d16eff81SEugene Zelenko 509d16eff81SEugene Zelenko for (auto CI : Children) { 510d16eff81SEugene Zelenko delete &(*CI); 511d16eff81SEugene Zelenko } 512d16eff81SEugene Zelenko } 513d16eff81SEugene Zelenko 514d16eff81SEugene Zelenko RegionMRT *getRegionMRT() override { return this; } 515a06bfe05SJan Sjodin 516a06bfe05SJan Sjodin void setLinearizedRegion(LinearizedRegion *LinearizeRegion) { 517a06bfe05SJan Sjodin LRegion = LinearizeRegion; 518a06bfe05SJan Sjodin } 519a06bfe05SJan Sjodin 520a06bfe05SJan Sjodin LinearizedRegion *getLinearizedRegion() { return LRegion; } 521a06bfe05SJan Sjodin 522a06bfe05SJan Sjodin MachineRegion *getMachineRegion() { return Region; } 523a06bfe05SJan Sjodin 524a06bfe05SJan Sjodin unsigned getInnerOutputRegister() { 525a06bfe05SJan Sjodin return (*(Children.begin()))->getBBSelectRegOut(); 526a06bfe05SJan Sjodin } 527a06bfe05SJan Sjodin 528a06bfe05SJan Sjodin void addChild(MRT *Tree) { Children.insert(Tree); } 529a06bfe05SJan Sjodin 530a06bfe05SJan Sjodin SetVector<MRT *> *getChildren() { return &Children; } 531a06bfe05SJan Sjodin 532d16eff81SEugene Zelenko void dump(const TargetRegisterInfo *TRI, int depth = 0) override { 533a06bfe05SJan Sjodin dumpDepth(depth); 534a06bfe05SJan Sjodin dbgs() << "Region: " << (void *)Region; 5359d419d3bSFrancis Visoiu Mistrih dbgs() << " In: " << printReg(getBBSelectRegIn(), TRI); 5369d419d3bSFrancis Visoiu Mistrih dbgs() << ", Out: " << printReg(getBBSelectRegOut(), TRI) << "\n"; 537a06bfe05SJan Sjodin 538a06bfe05SJan Sjodin dumpDepth(depth); 539a06bfe05SJan Sjodin if (getSucc()) 540a06bfe05SJan Sjodin dbgs() << "Succ: " << getSucc()->getNumber() << "\n"; 541a06bfe05SJan Sjodin else 542a06bfe05SJan Sjodin dbgs() << "Succ: none \n"; 543a06bfe05SJan Sjodin for (auto MRTI : Children) { 544a06bfe05SJan Sjodin MRTI->dump(TRI, depth + 1); 545a06bfe05SJan Sjodin } 546a06bfe05SJan Sjodin } 547a06bfe05SJan Sjodin 548a06bfe05SJan Sjodin MRT *getEntryTree() { return Children.back(); } 549a06bfe05SJan Sjodin 550a06bfe05SJan Sjodin MRT *getExitTree() { return Children.front(); } 551a06bfe05SJan Sjodin 552a06bfe05SJan Sjodin MachineBasicBlock *getEntry() { 553a06bfe05SJan Sjodin MRT *Tree = Children.back(); 554a06bfe05SJan Sjodin return (Tree->isRegion()) ? Tree->getRegionMRT()->getEntry() 555a06bfe05SJan Sjodin : Tree->getMBBMRT()->getMBB(); 556a06bfe05SJan Sjodin } 557a06bfe05SJan Sjodin 558a06bfe05SJan Sjodin MachineBasicBlock *getExit() { 559a06bfe05SJan Sjodin MRT *Tree = Children.front(); 560a06bfe05SJan Sjodin return (Tree->isRegion()) ? Tree->getRegionMRT()->getExit() 561a06bfe05SJan Sjodin : Tree->getMBBMRT()->getMBB(); 562a06bfe05SJan Sjodin } 563a06bfe05SJan Sjodin 564a06bfe05SJan Sjodin void setSucc(MachineBasicBlock *MBB) { Succ = MBB; } 565a06bfe05SJan Sjodin 566a06bfe05SJan Sjodin MachineBasicBlock *getSucc() { return Succ; } 567a06bfe05SJan Sjodin 568a06bfe05SJan Sjodin bool contains(MachineBasicBlock *MBB) { 569a06bfe05SJan Sjodin for (auto CI : Children) { 570a06bfe05SJan Sjodin if (CI->isMBB()) { 571a06bfe05SJan Sjodin if (MBB == CI->getMBBMRT()->getMBB()) { 572a06bfe05SJan Sjodin return true; 573a06bfe05SJan Sjodin } 574a06bfe05SJan Sjodin } else { 575a06bfe05SJan Sjodin if (CI->getRegionMRT()->contains(MBB)) { 576a06bfe05SJan Sjodin return true; 577a06bfe05SJan Sjodin } else if (CI->getRegionMRT()->getLinearizedRegion() != nullptr && 578a06bfe05SJan Sjodin CI->getRegionMRT()->getLinearizedRegion()->contains(MBB)) { 579a06bfe05SJan Sjodin return true; 580a06bfe05SJan Sjodin } 581a06bfe05SJan Sjodin } 582a06bfe05SJan Sjodin } 583a06bfe05SJan Sjodin return false; 584a06bfe05SJan Sjodin } 585a06bfe05SJan Sjodin 586a06bfe05SJan Sjodin void replaceLiveOutReg(unsigned Register, unsigned NewRegister) { 587a06bfe05SJan Sjodin LinearizedRegion *LRegion = getLinearizedRegion(); 588a06bfe05SJan Sjodin LRegion->replaceLiveOut(Register, NewRegister); 589a06bfe05SJan Sjodin for (auto &CI : Children) { 590a06bfe05SJan Sjodin if (CI->isRegion()) { 591a06bfe05SJan Sjodin CI->getRegionMRT()->replaceLiveOutReg(Register, NewRegister); 592a06bfe05SJan Sjodin } 593a06bfe05SJan Sjodin } 594a06bfe05SJan Sjodin } 595a06bfe05SJan Sjodin }; 596a06bfe05SJan Sjodin 597d16eff81SEugene Zelenko } // end anonymous namespace 598d16eff81SEugene Zelenko 599a06bfe05SJan Sjodin static unsigned createBBSelectReg(const SIInstrInfo *TII, 600a06bfe05SJan Sjodin MachineRegisterInfo *MRI) { 601a06bfe05SJan Sjodin return MRI->createVirtualRegister(TII->getPreferredSelectRegClass(32)); 602a06bfe05SJan Sjodin } 603a06bfe05SJan Sjodin 604a06bfe05SJan Sjodin MachineBasicBlock * 605a06bfe05SJan Sjodin MRT::initializeMRT(MachineFunction &MF, const MachineRegionInfo *RegionInfo, 606a06bfe05SJan Sjodin DenseMap<MachineRegion *, RegionMRT *> &RegionMap) { 607a06bfe05SJan Sjodin for (auto &MFI : MF) { 608a06bfe05SJan Sjodin MachineBasicBlock *ExitMBB = &MFI; 609c9fca53aSKazu Hirata if (ExitMBB->succ_empty()) { 610a06bfe05SJan Sjodin return ExitMBB; 611a06bfe05SJan Sjodin } 612a06bfe05SJan Sjodin } 613a06bfe05SJan Sjodin llvm_unreachable("CFG has no exit block"); 614a06bfe05SJan Sjodin return nullptr; 615a06bfe05SJan Sjodin } 616a06bfe05SJan Sjodin 617a06bfe05SJan Sjodin RegionMRT *MRT::buildMRT(MachineFunction &MF, 618a06bfe05SJan Sjodin const MachineRegionInfo *RegionInfo, 619a06bfe05SJan Sjodin const SIInstrInfo *TII, MachineRegisterInfo *MRI) { 620a06bfe05SJan Sjodin SmallPtrSet<MachineRegion *, 4> PlacedRegions; 621a06bfe05SJan Sjodin DenseMap<MachineRegion *, RegionMRT *> RegionMap; 622a06bfe05SJan Sjodin MachineRegion *TopLevelRegion = RegionInfo->getTopLevelRegion(); 623a06bfe05SJan Sjodin RegionMRT *Result = new RegionMRT(TopLevelRegion); 624a06bfe05SJan Sjodin RegionMap[TopLevelRegion] = Result; 625a06bfe05SJan Sjodin 626a06bfe05SJan Sjodin // Insert the exit block first, we need it to be the merge node 627a06bfe05SJan Sjodin // for the top level region. 628a06bfe05SJan Sjodin MachineBasicBlock *Exit = initializeMRT(MF, RegionInfo, RegionMap); 629a06bfe05SJan Sjodin 630a06bfe05SJan Sjodin unsigned BBSelectRegIn = createBBSelectReg(TII, MRI); 631a06bfe05SJan Sjodin MBBMRT *ExitMRT = new MBBMRT(Exit); 632a06bfe05SJan Sjodin RegionMap[RegionInfo->getRegionFor(Exit)]->addChild(ExitMRT); 633a06bfe05SJan Sjodin ExitMRT->setBBSelectRegIn(BBSelectRegIn); 634a06bfe05SJan Sjodin 635a06bfe05SJan Sjodin for (auto MBBI : post_order(&(MF.front()))) { 636a06bfe05SJan Sjodin MachineBasicBlock *MBB = &(*MBBI); 637a06bfe05SJan Sjodin 638a06bfe05SJan Sjodin // Skip Exit since we already added it 639a06bfe05SJan Sjodin if (MBB == Exit) { 640a06bfe05SJan Sjodin continue; 641a06bfe05SJan Sjodin } 642a06bfe05SJan Sjodin 643d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Visiting " << printMBBReference(*MBB) << "\n"); 644a06bfe05SJan Sjodin MBBMRT *NewMBB = new MBBMRT(MBB); 645a06bfe05SJan Sjodin MachineRegion *Region = RegionInfo->getRegionFor(MBB); 646a06bfe05SJan Sjodin 647a06bfe05SJan Sjodin // Ensure we have the MRT region 648a06bfe05SJan Sjodin if (RegionMap.count(Region) == 0) { 649a06bfe05SJan Sjodin RegionMRT *NewMRTRegion = new RegionMRT(Region); 650a06bfe05SJan Sjodin RegionMap[Region] = NewMRTRegion; 651a06bfe05SJan Sjodin 652a06bfe05SJan Sjodin // Ensure all parents are in the RegionMap 653a06bfe05SJan Sjodin MachineRegion *Parent = Region->getParent(); 654a06bfe05SJan Sjodin while (RegionMap.count(Parent) == 0) { 655a06bfe05SJan Sjodin RegionMRT *NewMRTParent = new RegionMRT(Parent); 656a06bfe05SJan Sjodin NewMRTParent->addChild(NewMRTRegion); 657a06bfe05SJan Sjodin NewMRTRegion->setParent(NewMRTParent); 658a06bfe05SJan Sjodin RegionMap[Parent] = NewMRTParent; 659a06bfe05SJan Sjodin NewMRTRegion = NewMRTParent; 660a06bfe05SJan Sjodin Parent = Parent->getParent(); 661a06bfe05SJan Sjodin } 662a06bfe05SJan Sjodin RegionMap[Parent]->addChild(NewMRTRegion); 663a06bfe05SJan Sjodin NewMRTRegion->setParent(RegionMap[Parent]); 664a06bfe05SJan Sjodin } 665a06bfe05SJan Sjodin 666a06bfe05SJan Sjodin // Add MBB to Region MRT 667a06bfe05SJan Sjodin RegionMap[Region]->addChild(NewMBB); 668a06bfe05SJan Sjodin NewMBB->setParent(RegionMap[Region]); 669a06bfe05SJan Sjodin RegionMap[Region]->setSucc(Region->getExit()); 670a06bfe05SJan Sjodin } 671a06bfe05SJan Sjodin return Result; 672a06bfe05SJan Sjodin } 673a06bfe05SJan Sjodin 67434978602SJay Foad void LinearizedRegion::storeLiveOutReg(MachineBasicBlock *MBB, Register Reg, 675a06bfe05SJan Sjodin MachineInstr *DefInstr, 676a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 677a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 678a06bfe05SJan Sjodin PHILinearize &PHIInfo) { 67934978602SJay Foad if (Reg.isVirtual()) { 680d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Considering Register: " << printReg(Reg, TRI) 681d34e60caSNicola Zaghen << "\n"); 682a06bfe05SJan Sjodin // If this is a source register to a PHI we are chaining, it 683a06bfe05SJan Sjodin // must be live out. 684a06bfe05SJan Sjodin if (PHIInfo.isSource(Reg)) { 685d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add LiveOut (PHI): " << printReg(Reg, TRI) << "\n"); 686a06bfe05SJan Sjodin addLiveOut(Reg); 687a06bfe05SJan Sjodin } else { 688a06bfe05SJan Sjodin // If this is live out of the MBB 689a06bfe05SJan Sjodin for (auto &UI : MRI->use_operands(Reg)) { 690a06bfe05SJan Sjodin if (UI.getParent()->getParent() != MBB) { 691d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add LiveOut (MBB " << printMBBReference(*MBB) 6929d419d3bSFrancis Visoiu Mistrih << "): " << printReg(Reg, TRI) << "\n"); 693a06bfe05SJan Sjodin addLiveOut(Reg); 694a06bfe05SJan Sjodin } else { 695a06bfe05SJan Sjodin // If the use is in the same MBB we have to make sure 696a06bfe05SJan Sjodin // it is after the def, otherwise it is live out in a loop 697a06bfe05SJan Sjodin MachineInstr *UseInstr = UI.getParent(); 698a06bfe05SJan Sjodin for (MachineBasicBlock::instr_iterator 699a06bfe05SJan Sjodin MII = UseInstr->getIterator(), 700a06bfe05SJan Sjodin MIE = UseInstr->getParent()->instr_end(); 701a06bfe05SJan Sjodin MII != MIE; ++MII) { 702a06bfe05SJan Sjodin if ((&(*MII)) == DefInstr) { 703d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add LiveOut (Loop): " << printReg(Reg, TRI) 704a06bfe05SJan Sjodin << "\n"); 705a06bfe05SJan Sjodin addLiveOut(Reg); 706a06bfe05SJan Sjodin } 707a06bfe05SJan Sjodin } 708a06bfe05SJan Sjodin } 709a06bfe05SJan Sjodin } 710a06bfe05SJan Sjodin } 711a06bfe05SJan Sjodin } 712a06bfe05SJan Sjodin } 713a06bfe05SJan Sjodin 71434978602SJay Foad void LinearizedRegion::storeLiveOutRegRegion(RegionMRT *Region, Register Reg, 715a06bfe05SJan Sjodin MachineInstr *DefInstr, 716a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 717a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 718a06bfe05SJan Sjodin PHILinearize &PHIInfo) { 71934978602SJay Foad if (Reg.isVirtual()) { 720d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Considering Register: " << printReg(Reg, TRI) 721d34e60caSNicola Zaghen << "\n"); 722a06bfe05SJan Sjodin for (auto &UI : MRI->use_operands(Reg)) { 723a06bfe05SJan Sjodin if (!Region->contains(UI.getParent()->getParent())) { 724d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add LiveOut (Region " << (void *)Region 7259d419d3bSFrancis Visoiu Mistrih << "): " << printReg(Reg, TRI) << "\n"); 726a06bfe05SJan Sjodin addLiveOut(Reg); 727a06bfe05SJan Sjodin } 728a06bfe05SJan Sjodin } 729a06bfe05SJan Sjodin } 730a06bfe05SJan Sjodin } 731a06bfe05SJan Sjodin 732a06bfe05SJan Sjodin void LinearizedRegion::storeLiveOuts(MachineBasicBlock *MBB, 733a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 734a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 735a06bfe05SJan Sjodin PHILinearize &PHIInfo) { 736d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "-Store Live Outs Begin (" << printMBBReference(*MBB) 73725528d6dSFrancis Visoiu Mistrih << ")-\n"); 738a06bfe05SJan Sjodin for (auto &II : *MBB) { 739a06bfe05SJan Sjodin for (auto &RI : II.defs()) { 740a06bfe05SJan Sjodin storeLiveOutReg(MBB, RI.getReg(), RI.getParent(), MRI, TRI, PHIInfo); 741a06bfe05SJan Sjodin } 742a06bfe05SJan Sjodin for (auto &IRI : II.implicit_operands()) { 743a06bfe05SJan Sjodin if (IRI.isDef()) { 744a06bfe05SJan Sjodin storeLiveOutReg(MBB, IRI.getReg(), IRI.getParent(), MRI, TRI, PHIInfo); 745a06bfe05SJan Sjodin } 746a06bfe05SJan Sjodin } 747a06bfe05SJan Sjodin } 748a06bfe05SJan Sjodin 749a06bfe05SJan Sjodin // If we have a successor with a PHI, source coming from this MBB we have to 750a06bfe05SJan Sjodin // add the register as live out 751a06bfe05SJan Sjodin for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), 752a06bfe05SJan Sjodin E = MBB->succ_end(); 753a06bfe05SJan Sjodin SI != E; ++SI) { 754a06bfe05SJan Sjodin for (auto &II : *(*SI)) { 755a06bfe05SJan Sjodin if (II.isPHI()) { 756a06bfe05SJan Sjodin MachineInstr &PHI = II; 757a06bfe05SJan Sjodin int numPreds = getPHINumInputs(PHI); 758a06bfe05SJan Sjodin for (int i = 0; i < numPreds; ++i) { 759a06bfe05SJan Sjodin if (getPHIPred(PHI, i) == MBB) { 760a06bfe05SJan Sjodin unsigned PHIReg = getPHISourceReg(PHI, i); 761d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() 762d34e60caSNicola Zaghen << "Add LiveOut (PhiSource " << printMBBReference(*MBB) 76325528d6dSFrancis Visoiu Mistrih << " -> " << printMBBReference(*(*SI)) 7649d419d3bSFrancis Visoiu Mistrih << "): " << printReg(PHIReg, TRI) << "\n"); 765a06bfe05SJan Sjodin addLiveOut(PHIReg); 766a06bfe05SJan Sjodin } 767a06bfe05SJan Sjodin } 768a06bfe05SJan Sjodin } 769a06bfe05SJan Sjodin } 770a06bfe05SJan Sjodin } 771a06bfe05SJan Sjodin 772d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "-Store Live Outs Endn-\n"); 773a06bfe05SJan Sjodin } 774a06bfe05SJan Sjodin 775a06bfe05SJan Sjodin void LinearizedRegion::storeMBBLiveOuts(MachineBasicBlock *MBB, 776a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 777a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 778a06bfe05SJan Sjodin PHILinearize &PHIInfo, 779a06bfe05SJan Sjodin RegionMRT *TopRegion) { 780a06bfe05SJan Sjodin for (auto &II : *MBB) { 781a06bfe05SJan Sjodin for (auto &RI : II.defs()) { 782a06bfe05SJan Sjodin storeLiveOutRegRegion(TopRegion, RI.getReg(), RI.getParent(), MRI, TRI, 783a06bfe05SJan Sjodin PHIInfo); 784a06bfe05SJan Sjodin } 785a06bfe05SJan Sjodin for (auto &IRI : II.implicit_operands()) { 786a06bfe05SJan Sjodin if (IRI.isDef()) { 787a06bfe05SJan Sjodin storeLiveOutRegRegion(TopRegion, IRI.getReg(), IRI.getParent(), MRI, 788a06bfe05SJan Sjodin TRI, PHIInfo); 789a06bfe05SJan Sjodin } 790a06bfe05SJan Sjodin } 791a06bfe05SJan Sjodin } 792a06bfe05SJan Sjodin } 793a06bfe05SJan Sjodin 794a06bfe05SJan Sjodin void LinearizedRegion::storeLiveOuts(RegionMRT *Region, 795a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 796a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 797a06bfe05SJan Sjodin PHILinearize &PHIInfo, 798a06bfe05SJan Sjodin RegionMRT *CurrentTopRegion) { 799a06bfe05SJan Sjodin MachineBasicBlock *Exit = Region->getSucc(); 800a06bfe05SJan Sjodin 801a06bfe05SJan Sjodin RegionMRT *TopRegion = 802a06bfe05SJan Sjodin CurrentTopRegion == nullptr ? Region : CurrentTopRegion; 803a06bfe05SJan Sjodin 804a06bfe05SJan Sjodin // Check if exit is end of function, if so, no live outs. 805a06bfe05SJan Sjodin if (Exit == nullptr) 806a06bfe05SJan Sjodin return; 807a06bfe05SJan Sjodin 808a06bfe05SJan Sjodin auto Children = Region->getChildren(); 809a06bfe05SJan Sjodin for (auto CI : *Children) { 810a06bfe05SJan Sjodin if (CI->isMBB()) { 811a06bfe05SJan Sjodin auto MBB = CI->getMBBMRT()->getMBB(); 812a06bfe05SJan Sjodin storeMBBLiveOuts(MBB, MRI, TRI, PHIInfo, TopRegion); 813a06bfe05SJan Sjodin } else { 814a06bfe05SJan Sjodin LinearizedRegion *SubRegion = CI->getRegionMRT()->getLinearizedRegion(); 815a06bfe05SJan Sjodin // We should be limited to only store registers that are live out from the 816*dc6e8dfdSJacob Lambert // linearized region 817a06bfe05SJan Sjodin for (auto MBBI : SubRegion->MBBs) { 818a06bfe05SJan Sjodin storeMBBLiveOuts(MBBI, MRI, TRI, PHIInfo, TopRegion); 819a06bfe05SJan Sjodin } 820a06bfe05SJan Sjodin } 821a06bfe05SJan Sjodin } 822a06bfe05SJan Sjodin 823a06bfe05SJan Sjodin if (CurrentTopRegion == nullptr) { 824a06bfe05SJan Sjodin auto Succ = Region->getSucc(); 825a06bfe05SJan Sjodin for (auto &II : *Succ) { 826a06bfe05SJan Sjodin if (II.isPHI()) { 827a06bfe05SJan Sjodin MachineInstr &PHI = II; 828a06bfe05SJan Sjodin int numPreds = getPHINumInputs(PHI); 829a06bfe05SJan Sjodin for (int i = 0; i < numPreds; ++i) { 830a06bfe05SJan Sjodin if (Region->contains(getPHIPred(PHI, i))) { 831a06bfe05SJan Sjodin unsigned PHIReg = getPHISourceReg(PHI, i); 832d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add Region LiveOut (" << (void *)Region 8339d419d3bSFrancis Visoiu Mistrih << "): " << printReg(PHIReg, TRI) << "\n"); 834a06bfe05SJan Sjodin addLiveOut(PHIReg); 835a06bfe05SJan Sjodin } 836a06bfe05SJan Sjodin } 837a06bfe05SJan Sjodin } 838a06bfe05SJan Sjodin } 839a06bfe05SJan Sjodin } 840a06bfe05SJan Sjodin } 841a06bfe05SJan Sjodin 8426b3216aaSFlorian Hahn #ifndef NDEBUG 843a06bfe05SJan Sjodin void LinearizedRegion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { 844a06bfe05SJan Sjodin OS << "Linearized Region {"; 845a06bfe05SJan Sjodin bool IsFirst = true; 8462d903cc9SMark de Wever for (auto MBB : MBBs) { 847a06bfe05SJan Sjodin if (IsFirst) { 848a06bfe05SJan Sjodin IsFirst = false; 849a06bfe05SJan Sjodin } else { 850a06bfe05SJan Sjodin OS << " ,"; 851a06bfe05SJan Sjodin } 852a06bfe05SJan Sjodin OS << MBB->getNumber(); 853a06bfe05SJan Sjodin } 854a06bfe05SJan Sjodin OS << "} (" << Entry->getNumber() << ", " 855a06bfe05SJan Sjodin << (Exit == nullptr ? -1 : Exit->getNumber()) 8569d419d3bSFrancis Visoiu Mistrih << "): In:" << printReg(getBBSelectRegIn(), TRI) 8579d419d3bSFrancis Visoiu Mistrih << " Out:" << printReg(getBBSelectRegOut(), TRI) << " {"; 858a06bfe05SJan Sjodin for (auto &LI : LiveOuts) { 8599d419d3bSFrancis Visoiu Mistrih OS << printReg(LI, TRI) << " "; 860a06bfe05SJan Sjodin } 861a06bfe05SJan Sjodin OS << "} \n"; 862a06bfe05SJan Sjodin } 8636b3216aaSFlorian Hahn #endif 864a06bfe05SJan Sjodin 865a06bfe05SJan Sjodin unsigned LinearizedRegion::getBBSelectRegIn() { 866a06bfe05SJan Sjodin return getRegionMRT()->getBBSelectRegIn(); 867a06bfe05SJan Sjodin } 868a06bfe05SJan Sjodin 869a06bfe05SJan Sjodin unsigned LinearizedRegion::getBBSelectRegOut() { 870a06bfe05SJan Sjodin return getRegionMRT()->getBBSelectRegOut(); 871a06bfe05SJan Sjodin } 872a06bfe05SJan Sjodin 873a06bfe05SJan Sjodin void LinearizedRegion::setHasLoop(bool Value) { HasLoop = Value; } 874a06bfe05SJan Sjodin 875a06bfe05SJan Sjodin bool LinearizedRegion::getHasLoop() { return HasLoop; } 876a06bfe05SJan Sjodin 877a06bfe05SJan Sjodin void LinearizedRegion::addLiveOut(unsigned VReg) { LiveOuts.insert(VReg); } 878a06bfe05SJan Sjodin 879a06bfe05SJan Sjodin void LinearizedRegion::removeLiveOut(unsigned Reg) { 880a06bfe05SJan Sjodin if (isLiveOut(Reg)) 881a06bfe05SJan Sjodin LiveOuts.erase(Reg); 882a06bfe05SJan Sjodin } 883a06bfe05SJan Sjodin 884a06bfe05SJan Sjodin void LinearizedRegion::replaceLiveOut(unsigned OldReg, unsigned NewReg) { 885a06bfe05SJan Sjodin if (isLiveOut(OldReg)) { 886a06bfe05SJan Sjodin removeLiveOut(OldReg); 887a06bfe05SJan Sjodin addLiveOut(NewReg); 888a06bfe05SJan Sjodin } 889a06bfe05SJan Sjodin } 890a06bfe05SJan Sjodin 89134978602SJay Foad void LinearizedRegion::replaceRegister(unsigned Register, 89234978602SJay Foad class Register NewRegister, 893a06bfe05SJan Sjodin MachineRegisterInfo *MRI, 894a06bfe05SJan Sjodin bool ReplaceInside, bool ReplaceOutside, 895a06bfe05SJan Sjodin bool IncludeLoopPHI) { 896a06bfe05SJan Sjodin assert(Register != NewRegister && "Cannot replace a reg with itself"); 897a06bfe05SJan Sjodin 898d34e60caSNicola Zaghen LLVM_DEBUG( 899*dc6e8dfdSJacob Lambert dbgs() << "Preparing to replace register (region): " 9009d419d3bSFrancis Visoiu Mistrih << printReg(Register, MRI->getTargetRegisterInfo()) << " with " 9019d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) << "\n"); 902a06bfe05SJan Sjodin 903a06bfe05SJan Sjodin // If we are replacing outside, we also need to update the LiveOuts 904a06bfe05SJan Sjodin if (ReplaceOutside && 905a06bfe05SJan Sjodin (isLiveOut(Register) || this->getParent()->isLiveOut(Register))) { 906a06bfe05SJan Sjodin LinearizedRegion *Current = this; 907a06bfe05SJan Sjodin while (Current != nullptr && Current->getEntry() != nullptr) { 908d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Region before register replace\n"); 909d34e60caSNicola Zaghen LLVM_DEBUG(Current->print(dbgs(), MRI->getTargetRegisterInfo())); 910a06bfe05SJan Sjodin Current->replaceLiveOut(Register, NewRegister); 911d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Region after register replace\n"); 912d34e60caSNicola Zaghen LLVM_DEBUG(Current->print(dbgs(), MRI->getTargetRegisterInfo())); 913a06bfe05SJan Sjodin Current = Current->getParent(); 914a06bfe05SJan Sjodin } 915a06bfe05SJan Sjodin } 916a06bfe05SJan Sjodin 917a06bfe05SJan Sjodin for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Register), 918a06bfe05SJan Sjodin E = MRI->reg_end(); 919a06bfe05SJan Sjodin I != E;) { 920a06bfe05SJan Sjodin MachineOperand &O = *I; 921a06bfe05SJan Sjodin ++I; 922a06bfe05SJan Sjodin 923a06bfe05SJan Sjodin // We don't rewrite defs. 924a06bfe05SJan Sjodin if (O.isDef()) 925a06bfe05SJan Sjodin continue; 926a06bfe05SJan Sjodin 927a06bfe05SJan Sjodin bool IsInside = contains(O.getParent()->getParent()); 928a06bfe05SJan Sjodin bool IsLoopPHI = IsInside && (O.getParent()->isPHI() && 929a06bfe05SJan Sjodin O.getParent()->getParent() == getEntry()); 930a06bfe05SJan Sjodin bool ShouldReplace = (IsInside && ReplaceInside) || 931a06bfe05SJan Sjodin (!IsInside && ReplaceOutside) || 932a06bfe05SJan Sjodin (IncludeLoopPHI && IsLoopPHI); 933a06bfe05SJan Sjodin if (ShouldReplace) { 934a06bfe05SJan Sjodin 93534978602SJay Foad if (NewRegister.isPhysical()) { 936d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Trying to substitute physical register: " 9379d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) 938a06bfe05SJan Sjodin << "\n"); 939a06bfe05SJan Sjodin llvm_unreachable("Cannot substitute physical registers"); 940a06bfe05SJan Sjodin } else { 941d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replacing register (region): " 9429d419d3bSFrancis Visoiu Mistrih << printReg(Register, MRI->getTargetRegisterInfo()) 943a06bfe05SJan Sjodin << " with " 9449d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) 945a06bfe05SJan Sjodin << "\n"); 946a06bfe05SJan Sjodin O.setReg(NewRegister); 947a06bfe05SJan Sjodin } 948a06bfe05SJan Sjodin } 949a06bfe05SJan Sjodin } 950a06bfe05SJan Sjodin } 951a06bfe05SJan Sjodin 952a06bfe05SJan Sjodin void LinearizedRegion::replaceRegisterInsideRegion(unsigned Register, 953a06bfe05SJan Sjodin unsigned NewRegister, 954a06bfe05SJan Sjodin bool IncludeLoopPHIs, 955a06bfe05SJan Sjodin MachineRegisterInfo *MRI) { 956a06bfe05SJan Sjodin replaceRegister(Register, NewRegister, MRI, true, false, IncludeLoopPHIs); 957a06bfe05SJan Sjodin } 958a06bfe05SJan Sjodin 959a06bfe05SJan Sjodin void LinearizedRegion::replaceRegisterOutsideRegion(unsigned Register, 960a06bfe05SJan Sjodin unsigned NewRegister, 961a06bfe05SJan Sjodin bool IncludeLoopPHIs, 962a06bfe05SJan Sjodin MachineRegisterInfo *MRI) { 963a06bfe05SJan Sjodin replaceRegister(Register, NewRegister, MRI, false, true, IncludeLoopPHIs); 964a06bfe05SJan Sjodin } 965a06bfe05SJan Sjodin 966a06bfe05SJan Sjodin DenseSet<unsigned> *LinearizedRegion::getLiveOuts() { return &LiveOuts; } 967a06bfe05SJan Sjodin 968a06bfe05SJan Sjodin void LinearizedRegion::setEntry(MachineBasicBlock *NewEntry) { 969a06bfe05SJan Sjodin Entry = NewEntry; 970a06bfe05SJan Sjodin } 971a06bfe05SJan Sjodin 972a06bfe05SJan Sjodin MachineBasicBlock *LinearizedRegion::getEntry() { return Entry; } 973a06bfe05SJan Sjodin 974a06bfe05SJan Sjodin void LinearizedRegion::setExit(MachineBasicBlock *NewExit) { Exit = NewExit; } 975a06bfe05SJan Sjodin 976a06bfe05SJan Sjodin MachineBasicBlock *LinearizedRegion::getExit() { return Exit; } 977a06bfe05SJan Sjodin 978a06bfe05SJan Sjodin void LinearizedRegion::addMBB(MachineBasicBlock *MBB) { MBBs.insert(MBB); } 979a06bfe05SJan Sjodin 980a06bfe05SJan Sjodin void LinearizedRegion::addMBBs(LinearizedRegion *InnerRegion) { 9812d903cc9SMark de Wever for (auto MBB : InnerRegion->MBBs) { 982a06bfe05SJan Sjodin addMBB(MBB); 983a06bfe05SJan Sjodin } 984a06bfe05SJan Sjodin } 985a06bfe05SJan Sjodin 986a06bfe05SJan Sjodin bool LinearizedRegion::contains(MachineBasicBlock *MBB) { 987b7c5e0b0SKazu Hirata return MBBs.contains(MBB); 988a06bfe05SJan Sjodin } 989a06bfe05SJan Sjodin 990a06bfe05SJan Sjodin bool LinearizedRegion::isLiveOut(unsigned Reg) { 991b7c5e0b0SKazu Hirata return LiveOuts.contains(Reg); 992a06bfe05SJan Sjodin } 993a06bfe05SJan Sjodin 994a06bfe05SJan Sjodin bool LinearizedRegion::hasNoDef(unsigned Reg, MachineRegisterInfo *MRI) { 995a06bfe05SJan Sjodin return MRI->def_begin(Reg) == MRI->def_end(); 996a06bfe05SJan Sjodin } 997a06bfe05SJan Sjodin 998a06bfe05SJan Sjodin // After the code has been structurized, what was flagged as kills 999a06bfe05SJan Sjodin // before are no longer register kills. 1000a06bfe05SJan Sjodin void LinearizedRegion::removeFalseRegisterKills(MachineRegisterInfo *MRI) { 1001a06bfe05SJan Sjodin const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); 100212961ff0SDaniel Sanders (void)TRI; // It's used by LLVM_DEBUG. 100312961ff0SDaniel Sanders 1004a06bfe05SJan Sjodin for (auto MBBI : MBBs) { 1005a06bfe05SJan Sjodin MachineBasicBlock *MBB = MBBI; 1006a06bfe05SJan Sjodin for (auto &II : *MBB) { 1007a06bfe05SJan Sjodin for (auto &RI : II.uses()) { 1008a06bfe05SJan Sjodin if (RI.isReg()) { 10090c476111SDaniel Sanders Register Reg = RI.getReg(); 101034978602SJay Foad if (Reg.isVirtual()) { 1011a06bfe05SJan Sjodin if (hasNoDef(Reg, MRI)) 1012a06bfe05SJan Sjodin continue; 1013a06bfe05SJan Sjodin if (!MRI->hasOneDef(Reg)) { 1014d34e60caSNicola Zaghen LLVM_DEBUG(this->getEntry()->getParent()->dump()); 1015d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(Reg, TRI) << "\n"); 1016a06bfe05SJan Sjodin } 1017a06bfe05SJan Sjodin 1018a06bfe05SJan Sjodin if (MRI->def_begin(Reg) == MRI->def_end()) { 1019d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " 10209d419d3bSFrancis Visoiu Mistrih << printReg(Reg, MRI->getTargetRegisterInfo()) 1021a06bfe05SJan Sjodin << " has NO defs\n"); 1022a06bfe05SJan Sjodin } else if (!MRI->hasOneDef(Reg)) { 1023d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " 10249d419d3bSFrancis Visoiu Mistrih << printReg(Reg, MRI->getTargetRegisterInfo()) 1025a06bfe05SJan Sjodin << " has multiple defs\n"); 1026a06bfe05SJan Sjodin } 1027a06bfe05SJan Sjodin 1028a06bfe05SJan Sjodin assert(MRI->hasOneDef(Reg) && "Register has multiple definitions"); 1029a06bfe05SJan Sjodin MachineOperand *Def = &(*(MRI->def_begin(Reg))); 1030a06bfe05SJan Sjodin MachineOperand *UseOperand = &(RI); 1031a06bfe05SJan Sjodin bool UseIsOutsideDefMBB = Def->getParent()->getParent() != MBB; 1032a06bfe05SJan Sjodin if (UseIsOutsideDefMBB && UseOperand->isKill()) { 1033d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Removing kill flag on register: " 10349d419d3bSFrancis Visoiu Mistrih << printReg(Reg, TRI) << "\n"); 1035a06bfe05SJan Sjodin UseOperand->setIsKill(false); 1036a06bfe05SJan Sjodin } 1037a06bfe05SJan Sjodin } 1038a06bfe05SJan Sjodin } 1039a06bfe05SJan Sjodin } 1040a06bfe05SJan Sjodin } 1041a06bfe05SJan Sjodin } 1042a06bfe05SJan Sjodin } 1043a06bfe05SJan Sjodin 1044a06bfe05SJan Sjodin void LinearizedRegion::initLiveOut(RegionMRT *Region, 1045a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 1046a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 1047a06bfe05SJan Sjodin PHILinearize &PHIInfo) { 1048a06bfe05SJan Sjodin storeLiveOuts(Region, MRI, TRI, PHIInfo); 1049a06bfe05SJan Sjodin } 1050a06bfe05SJan Sjodin 1051a06bfe05SJan Sjodin LinearizedRegion::LinearizedRegion(MachineBasicBlock *MBB, 1052a06bfe05SJan Sjodin const MachineRegisterInfo *MRI, 1053a06bfe05SJan Sjodin const TargetRegisterInfo *TRI, 1054a06bfe05SJan Sjodin PHILinearize &PHIInfo) { 1055a06bfe05SJan Sjodin setEntry(MBB); 1056a06bfe05SJan Sjodin setExit(MBB); 1057a06bfe05SJan Sjodin storeLiveOuts(MBB, MRI, TRI, PHIInfo); 1058a06bfe05SJan Sjodin MBBs.insert(MBB); 1059a06bfe05SJan Sjodin Parent = nullptr; 1060a06bfe05SJan Sjodin } 1061a06bfe05SJan Sjodin 1062a06bfe05SJan Sjodin LinearizedRegion::LinearizedRegion() { 1063a06bfe05SJan Sjodin setEntry(nullptr); 1064a06bfe05SJan Sjodin setExit(nullptr); 1065a06bfe05SJan Sjodin Parent = nullptr; 1066a06bfe05SJan Sjodin } 1067a06bfe05SJan Sjodin 1068d16eff81SEugene Zelenko namespace { 1069a06bfe05SJan Sjodin 1070a06bfe05SJan Sjodin class AMDGPUMachineCFGStructurizer : public MachineFunctionPass { 1071a06bfe05SJan Sjodin private: 1072a06bfe05SJan Sjodin const MachineRegionInfo *Regions; 1073a06bfe05SJan Sjodin const SIInstrInfo *TII; 1074a06bfe05SJan Sjodin const TargetRegisterInfo *TRI; 1075a06bfe05SJan Sjodin MachineRegisterInfo *MRI; 1076a06bfe05SJan Sjodin unsigned BBSelectRegister; 1077a06bfe05SJan Sjodin PHILinearize PHIInfo; 1078a06bfe05SJan Sjodin DenseMap<MachineBasicBlock *, MachineBasicBlock *> FallthroughMap; 1079d16eff81SEugene Zelenko RegionMRT *RMRT; 1080a06bfe05SJan Sjodin 1081a06bfe05SJan Sjodin void getPHIRegionIndices(RegionMRT *Region, MachineInstr &PHI, 1082a06bfe05SJan Sjodin SmallVector<unsigned, 2> &RegionIndices); 1083a06bfe05SJan Sjodin void getPHIRegionIndices(LinearizedRegion *Region, MachineInstr &PHI, 1084a06bfe05SJan Sjodin SmallVector<unsigned, 2> &RegionIndices); 1085a06bfe05SJan Sjodin void getPHINonRegionIndices(LinearizedRegion *Region, MachineInstr &PHI, 1086a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHINonRegionIndices); 1087a06bfe05SJan Sjodin 1088a06bfe05SJan Sjodin void storePHILinearizationInfoDest( 1089a06bfe05SJan Sjodin unsigned LDestReg, MachineInstr &PHI, 1090a06bfe05SJan Sjodin SmallVector<unsigned, 2> *RegionIndices = nullptr); 1091a06bfe05SJan Sjodin 1092a06bfe05SJan Sjodin unsigned storePHILinearizationInfo(MachineInstr &PHI, 1093a06bfe05SJan Sjodin SmallVector<unsigned, 2> *RegionIndices); 1094a06bfe05SJan Sjodin 1095a06bfe05SJan Sjodin void extractKilledPHIs(MachineBasicBlock *MBB); 1096a06bfe05SJan Sjodin 1097a06bfe05SJan Sjodin bool shrinkPHI(MachineInstr &PHI, SmallVector<unsigned, 2> &PHIIndices, 1098a06bfe05SJan Sjodin unsigned *ReplaceReg); 1099a06bfe05SJan Sjodin 1100a06bfe05SJan Sjodin bool shrinkPHI(MachineInstr &PHI, unsigned CombinedSourceReg, 1101a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB, 1102a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIIndices, unsigned *ReplaceReg); 1103a06bfe05SJan Sjodin 1104a06bfe05SJan Sjodin void replacePHI(MachineInstr &PHI, unsigned CombinedSourceReg, 1105a06bfe05SJan Sjodin MachineBasicBlock *LastMerge, 1106a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices); 1107a06bfe05SJan Sjodin void replaceEntryPHI(MachineInstr &PHI, unsigned CombinedSourceReg, 1108a06bfe05SJan Sjodin MachineBasicBlock *IfMBB, 1109a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices); 1110a06bfe05SJan Sjodin void replaceLiveOutRegs(MachineInstr &PHI, 1111a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices, 1112a06bfe05SJan Sjodin unsigned CombinedSourceReg, 1113a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1114a06bfe05SJan Sjodin void rewriteRegionExitPHI(RegionMRT *Region, MachineBasicBlock *LastMerge, 1115a06bfe05SJan Sjodin MachineInstr &PHI, LinearizedRegion *LRegion); 1116a06bfe05SJan Sjodin 1117a06bfe05SJan Sjodin void rewriteRegionExitPHIs(RegionMRT *Region, MachineBasicBlock *LastMerge, 1118a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1119a06bfe05SJan Sjodin void rewriteRegionEntryPHI(LinearizedRegion *Region, MachineBasicBlock *IfMBB, 1120a06bfe05SJan Sjodin MachineInstr &PHI); 1121a06bfe05SJan Sjodin void rewriteRegionEntryPHIs(LinearizedRegion *Region, 1122a06bfe05SJan Sjodin MachineBasicBlock *IfMBB); 1123a06bfe05SJan Sjodin 1124a06bfe05SJan Sjodin bool regionIsSimpleIf(RegionMRT *Region); 1125a06bfe05SJan Sjodin 1126a06bfe05SJan Sjodin void transformSimpleIfRegion(RegionMRT *Region); 1127a06bfe05SJan Sjodin 1128a06bfe05SJan Sjodin void eliminateDeadBranchOperands(MachineBasicBlock::instr_iterator &II); 1129a06bfe05SJan Sjodin 1130a06bfe05SJan Sjodin void insertUnconditionalBranch(MachineBasicBlock *MBB, 1131a06bfe05SJan Sjodin MachineBasicBlock *Dest, 1132a06bfe05SJan Sjodin const DebugLoc &DL = DebugLoc()); 1133a06bfe05SJan Sjodin 1134a06bfe05SJan Sjodin MachineBasicBlock *createLinearizedExitBlock(RegionMRT *Region); 1135a06bfe05SJan Sjodin 1136a06bfe05SJan Sjodin void insertMergePHI(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, 1137a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, unsigned DestRegister, 1138a06bfe05SJan Sjodin unsigned IfSourceRegister, unsigned CodeSourceRegister, 1139a06bfe05SJan Sjodin bool IsUndefIfSource = false); 1140a06bfe05SJan Sjodin 1141a06bfe05SJan Sjodin MachineBasicBlock *createIfBlock(MachineBasicBlock *MergeBB, 1142a06bfe05SJan Sjodin MachineBasicBlock *CodeBBStart, 1143a06bfe05SJan Sjodin MachineBasicBlock *CodeBBEnd, 1144a06bfe05SJan Sjodin MachineBasicBlock *SelectBB, unsigned IfReg, 1145a06bfe05SJan Sjodin bool InheritPreds); 1146a06bfe05SJan Sjodin 1147a06bfe05SJan Sjodin void prunePHIInfo(MachineBasicBlock *MBB); 1148a06bfe05SJan Sjodin void createEntryPHI(LinearizedRegion *CurrentRegion, unsigned DestReg); 1149a06bfe05SJan Sjodin 1150a06bfe05SJan Sjodin void createEntryPHIs(LinearizedRegion *CurrentRegion); 1151a06bfe05SJan Sjodin void resolvePHIInfos(MachineBasicBlock *FunctionEntry); 1152a06bfe05SJan Sjodin 115334978602SJay Foad void replaceRegisterWith(unsigned Register, class Register NewRegister); 1154a06bfe05SJan Sjodin 1155a06bfe05SJan Sjodin MachineBasicBlock *createIfRegion(MachineBasicBlock *MergeBB, 1156a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 1157a06bfe05SJan Sjodin LinearizedRegion *LRegion, 1158a06bfe05SJan Sjodin unsigned BBSelectRegIn, 1159a06bfe05SJan Sjodin unsigned BBSelectRegOut); 1160a06bfe05SJan Sjodin 1161a06bfe05SJan Sjodin MachineBasicBlock * 1162a06bfe05SJan Sjodin createIfRegion(MachineBasicBlock *MergeMBB, LinearizedRegion *InnerRegion, 1163a06bfe05SJan Sjodin LinearizedRegion *CurrentRegion, MachineBasicBlock *SelectBB, 1164a06bfe05SJan Sjodin unsigned BBSelectRegIn, unsigned BBSelectRegOut); 1165a06bfe05SJan Sjodin void ensureCondIsNotKilled(SmallVector<MachineOperand, 1> Cond); 1166a06bfe05SJan Sjodin 1167a06bfe05SJan Sjodin void rewriteCodeBBTerminator(MachineBasicBlock *CodeBB, 1168a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1169a06bfe05SJan Sjodin unsigned BBSelectReg); 1170a06bfe05SJan Sjodin 1171a06bfe05SJan Sjodin MachineInstr *getDefInstr(unsigned Reg); 1172a06bfe05SJan Sjodin void insertChainedPHI(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, 1173a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1174a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, unsigned DestReg, 1175a06bfe05SJan Sjodin unsigned SourceReg); 1176a06bfe05SJan Sjodin bool containsDef(MachineBasicBlock *MBB, LinearizedRegion *InnerRegion, 1177a06bfe05SJan Sjodin unsigned Register); 1178a06bfe05SJan Sjodin void rewriteLiveOutRegs(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, 1179a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1180a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 1181a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1182a06bfe05SJan Sjodin 1183a06bfe05SJan Sjodin void splitLoopPHI(MachineInstr &PHI, MachineBasicBlock *Entry, 1184a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc, LinearizedRegion *LRegion); 1185a06bfe05SJan Sjodin void splitLoopPHIs(MachineBasicBlock *Entry, MachineBasicBlock *EntrySucc, 1186a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1187a06bfe05SJan Sjodin 1188a06bfe05SJan Sjodin MachineBasicBlock *splitExit(LinearizedRegion *LRegion); 1189a06bfe05SJan Sjodin 1190a06bfe05SJan Sjodin MachineBasicBlock *splitEntry(LinearizedRegion *LRegion); 1191a06bfe05SJan Sjodin 1192a06bfe05SJan Sjodin LinearizedRegion *initLinearizedRegion(RegionMRT *Region); 1193a06bfe05SJan Sjodin 1194a06bfe05SJan Sjodin bool structurizeComplexRegion(RegionMRT *Region); 1195a06bfe05SJan Sjodin 1196a06bfe05SJan Sjodin bool structurizeRegion(RegionMRT *Region); 1197a06bfe05SJan Sjodin 1198a06bfe05SJan Sjodin bool structurizeRegions(RegionMRT *Region, bool isTopRegion); 1199a06bfe05SJan Sjodin 1200a06bfe05SJan Sjodin public: 1201a06bfe05SJan Sjodin static char ID; 1202a06bfe05SJan Sjodin 1203d16eff81SEugene Zelenko AMDGPUMachineCFGStructurizer() : MachineFunctionPass(ID) { 1204d16eff81SEugene Zelenko initializeAMDGPUMachineCFGStructurizerPass(*PassRegistry::getPassRegistry()); 1205d16eff81SEugene Zelenko } 1206d16eff81SEugene Zelenko 1207a06bfe05SJan Sjodin void getAnalysisUsage(AnalysisUsage &AU) const override { 1208a06bfe05SJan Sjodin AU.addRequired<MachineRegionInfoPass>(); 1209a06bfe05SJan Sjodin MachineFunctionPass::getAnalysisUsage(AU); 1210a06bfe05SJan Sjodin } 1211a06bfe05SJan Sjodin 1212a06bfe05SJan Sjodin void initFallthroughMap(MachineFunction &MF); 1213a06bfe05SJan Sjodin 1214a06bfe05SJan Sjodin void createLinearizedRegion(RegionMRT *Region, unsigned SelectOut); 1215a06bfe05SJan Sjodin 1216a06bfe05SJan Sjodin unsigned initializeSelectRegisters(MRT *MRT, unsigned ExistingExitReg, 1217a06bfe05SJan Sjodin MachineRegisterInfo *MRI, 1218a06bfe05SJan Sjodin const SIInstrInfo *TII); 1219a06bfe05SJan Sjodin 1220a06bfe05SJan Sjodin void setRegionMRT(RegionMRT *RegionTree) { RMRT = RegionTree; } 1221a06bfe05SJan Sjodin 1222a06bfe05SJan Sjodin RegionMRT *getRegionMRT() { return RMRT; } 1223a06bfe05SJan Sjodin 1224a06bfe05SJan Sjodin bool runOnMachineFunction(MachineFunction &MF) override; 1225a06bfe05SJan Sjodin }; 1226d16eff81SEugene Zelenko 1227d16eff81SEugene Zelenko } // end anonymous namespace 1228a06bfe05SJan Sjodin 1229a06bfe05SJan Sjodin char AMDGPUMachineCFGStructurizer::ID = 0; 1230a06bfe05SJan Sjodin 1231a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::regionIsSimpleIf(RegionMRT *Region) { 1232a06bfe05SJan Sjodin MachineBasicBlock *Entry = Region->getEntry(); 1233a06bfe05SJan Sjodin MachineBasicBlock *Succ = Region->getSucc(); 1234a06bfe05SJan Sjodin bool FoundBypass = false; 1235a06bfe05SJan Sjodin bool FoundIf = false; 1236a06bfe05SJan Sjodin 1237a06bfe05SJan Sjodin if (Entry->succ_size() != 2) { 1238a06bfe05SJan Sjodin return false; 1239a06bfe05SJan Sjodin } 1240a06bfe05SJan Sjodin 1241a06bfe05SJan Sjodin for (MachineBasicBlock::const_succ_iterator SI = Entry->succ_begin(), 1242a06bfe05SJan Sjodin E = Entry->succ_end(); 1243a06bfe05SJan Sjodin SI != E; ++SI) { 1244a06bfe05SJan Sjodin MachineBasicBlock *Current = *SI; 1245a06bfe05SJan Sjodin 1246a06bfe05SJan Sjodin if (Current == Succ) { 1247a06bfe05SJan Sjodin FoundBypass = true; 1248a06bfe05SJan Sjodin } else if ((Current->succ_size() == 1) && 1249a06bfe05SJan Sjodin *(Current->succ_begin()) == Succ) { 1250a06bfe05SJan Sjodin FoundIf = true; 1251a06bfe05SJan Sjodin } 1252a06bfe05SJan Sjodin } 1253a06bfe05SJan Sjodin 1254a06bfe05SJan Sjodin return FoundIf && FoundBypass; 1255a06bfe05SJan Sjodin } 1256a06bfe05SJan Sjodin 1257a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::transformSimpleIfRegion(RegionMRT *Region) { 1258a06bfe05SJan Sjodin MachineBasicBlock *Entry = Region->getEntry(); 1259a06bfe05SJan Sjodin MachineBasicBlock *Exit = Region->getExit(); 1260a06bfe05SJan Sjodin TII->convertNonUniformIfRegion(Entry, Exit); 1261a06bfe05SJan Sjodin } 1262a06bfe05SJan Sjodin 1263a06bfe05SJan Sjodin static void fixMBBTerminator(MachineBasicBlock *MBB) { 1264a06bfe05SJan Sjodin if (MBB->succ_size() == 1) { 1265a06bfe05SJan Sjodin auto *Succ = *(MBB->succ_begin()); 1266a06bfe05SJan Sjodin for (auto &TI : MBB->terminators()) { 1267a06bfe05SJan Sjodin for (auto &UI : TI.uses()) { 1268a06bfe05SJan Sjodin if (UI.isMBB() && UI.getMBB() != Succ) { 1269a06bfe05SJan Sjodin UI.setMBB(Succ); 1270a06bfe05SJan Sjodin } 1271a06bfe05SJan Sjodin } 1272a06bfe05SJan Sjodin } 1273a06bfe05SJan Sjodin } 1274a06bfe05SJan Sjodin } 1275a06bfe05SJan Sjodin 1276a06bfe05SJan Sjodin static void fixRegionTerminator(RegionMRT *Region) { 1277a06bfe05SJan Sjodin MachineBasicBlock *InternalSucc = nullptr; 1278a06bfe05SJan Sjodin MachineBasicBlock *ExternalSucc = nullptr; 1279a06bfe05SJan Sjodin LinearizedRegion *LRegion = Region->getLinearizedRegion(); 1280a06bfe05SJan Sjodin auto Exit = LRegion->getExit(); 1281a06bfe05SJan Sjodin 1282a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 2> Successors; 1283a06bfe05SJan Sjodin for (MachineBasicBlock::const_succ_iterator SI = Exit->succ_begin(), 1284a06bfe05SJan Sjodin SE = Exit->succ_end(); 1285a06bfe05SJan Sjodin SI != SE; ++SI) { 1286a06bfe05SJan Sjodin MachineBasicBlock *Succ = *SI; 1287a06bfe05SJan Sjodin if (LRegion->contains(Succ)) { 1288a06bfe05SJan Sjodin // Do not allow re-assign 1289a06bfe05SJan Sjodin assert(InternalSucc == nullptr); 1290a06bfe05SJan Sjodin InternalSucc = Succ; 1291a06bfe05SJan Sjodin } else { 1292a06bfe05SJan Sjodin // Do not allow re-assign 1293a06bfe05SJan Sjodin assert(ExternalSucc == nullptr); 1294a06bfe05SJan Sjodin ExternalSucc = Succ; 1295a06bfe05SJan Sjodin } 1296a06bfe05SJan Sjodin } 1297a06bfe05SJan Sjodin 1298a06bfe05SJan Sjodin for (auto &TI : Exit->terminators()) { 1299a06bfe05SJan Sjodin for (auto &UI : TI.uses()) { 1300a06bfe05SJan Sjodin if (UI.isMBB()) { 1301a06bfe05SJan Sjodin auto Target = UI.getMBB(); 1302a06bfe05SJan Sjodin if (Target != InternalSucc && Target != ExternalSucc) { 1303a06bfe05SJan Sjodin UI.setMBB(ExternalSucc); 1304a06bfe05SJan Sjodin } 1305a06bfe05SJan Sjodin } 1306a06bfe05SJan Sjodin } 1307a06bfe05SJan Sjodin } 1308a06bfe05SJan Sjodin } 1309a06bfe05SJan Sjodin 1310a06bfe05SJan Sjodin // If a region region is just a sequence of regions (and the exit 1311a06bfe05SJan Sjodin // block in the case of the top level region), we can simply skip 1312a06bfe05SJan Sjodin // linearizing it, because it is already linear 1313a06bfe05SJan Sjodin bool regionIsSequence(RegionMRT *Region) { 1314a06bfe05SJan Sjodin auto Children = Region->getChildren(); 1315a06bfe05SJan Sjodin for (auto CI : *Children) { 1316a06bfe05SJan Sjodin if (!CI->isRegion()) { 1317a06bfe05SJan Sjodin if (CI->getMBBMRT()->getMBB()->succ_size() > 1) { 1318a06bfe05SJan Sjodin return false; 1319a06bfe05SJan Sjodin } 1320a06bfe05SJan Sjodin } 1321a06bfe05SJan Sjodin } 1322a06bfe05SJan Sjodin return true; 1323a06bfe05SJan Sjodin } 1324a06bfe05SJan Sjodin 1325a06bfe05SJan Sjodin void fixupRegionExits(RegionMRT *Region) { 1326a06bfe05SJan Sjodin auto Children = Region->getChildren(); 1327a06bfe05SJan Sjodin for (auto CI : *Children) { 1328a06bfe05SJan Sjodin if (!CI->isRegion()) { 1329a06bfe05SJan Sjodin fixMBBTerminator(CI->getMBBMRT()->getMBB()); 1330a06bfe05SJan Sjodin } else { 1331a06bfe05SJan Sjodin fixRegionTerminator(CI->getRegionMRT()); 1332a06bfe05SJan Sjodin } 1333a06bfe05SJan Sjodin } 1334a06bfe05SJan Sjodin } 1335a06bfe05SJan Sjodin 1336a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::getPHIRegionIndices( 1337a06bfe05SJan Sjodin RegionMRT *Region, MachineInstr &PHI, 1338a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1339a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1340a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1341a06bfe05SJan Sjodin MachineBasicBlock *Pred = getPHIPred(PHI, i); 1342a06bfe05SJan Sjodin if (Region->contains(Pred)) { 1343a06bfe05SJan Sjodin PHIRegionIndices.push_back(i); 1344a06bfe05SJan Sjodin } 1345a06bfe05SJan Sjodin } 1346a06bfe05SJan Sjodin } 1347a06bfe05SJan Sjodin 1348a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::getPHIRegionIndices( 1349a06bfe05SJan Sjodin LinearizedRegion *Region, MachineInstr &PHI, 1350a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1351a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1352a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1353a06bfe05SJan Sjodin MachineBasicBlock *Pred = getPHIPred(PHI, i); 1354a06bfe05SJan Sjodin if (Region->contains(Pred)) { 1355a06bfe05SJan Sjodin PHIRegionIndices.push_back(i); 1356a06bfe05SJan Sjodin } 1357a06bfe05SJan Sjodin } 1358a06bfe05SJan Sjodin } 1359a06bfe05SJan Sjodin 1360a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::getPHINonRegionIndices( 1361a06bfe05SJan Sjodin LinearizedRegion *Region, MachineInstr &PHI, 1362a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHINonRegionIndices) { 1363a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1364a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1365a06bfe05SJan Sjodin MachineBasicBlock *Pred = getPHIPred(PHI, i); 1366a06bfe05SJan Sjodin if (!Region->contains(Pred)) { 1367a06bfe05SJan Sjodin PHINonRegionIndices.push_back(i); 1368a06bfe05SJan Sjodin } 1369a06bfe05SJan Sjodin } 1370a06bfe05SJan Sjodin } 1371a06bfe05SJan Sjodin 1372a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::storePHILinearizationInfoDest( 1373a06bfe05SJan Sjodin unsigned LDestReg, MachineInstr &PHI, 1374a06bfe05SJan Sjodin SmallVector<unsigned, 2> *RegionIndices) { 1375a06bfe05SJan Sjodin if (RegionIndices) { 1376a06bfe05SJan Sjodin for (auto i : *RegionIndices) { 1377a06bfe05SJan Sjodin PHIInfo.addSource(LDestReg, getPHISourceReg(PHI, i), getPHIPred(PHI, i)); 1378a06bfe05SJan Sjodin } 1379a06bfe05SJan Sjodin } else { 1380a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1381a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1382a06bfe05SJan Sjodin PHIInfo.addSource(LDestReg, getPHISourceReg(PHI, i), getPHIPred(PHI, i)); 1383a06bfe05SJan Sjodin } 1384a06bfe05SJan Sjodin } 1385a06bfe05SJan Sjodin } 1386a06bfe05SJan Sjodin 1387a06bfe05SJan Sjodin unsigned AMDGPUMachineCFGStructurizer::storePHILinearizationInfo( 1388a06bfe05SJan Sjodin MachineInstr &PHI, SmallVector<unsigned, 2> *RegionIndices) { 1389a06bfe05SJan Sjodin unsigned DestReg = getPHIDestReg(PHI); 13900c476111SDaniel Sanders Register LinearizeDestReg = 1391a06bfe05SJan Sjodin MRI->createVirtualRegister(MRI->getRegClass(DestReg)); 1392a06bfe05SJan Sjodin PHIInfo.addDest(LinearizeDestReg, PHI.getDebugLoc()); 1393a06bfe05SJan Sjodin storePHILinearizationInfoDest(LinearizeDestReg, PHI, RegionIndices); 1394a06bfe05SJan Sjodin return LinearizeDestReg; 1395a06bfe05SJan Sjodin } 1396a06bfe05SJan Sjodin 1397a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::extractKilledPHIs(MachineBasicBlock *MBB) { 1398a06bfe05SJan Sjodin // We need to create a new chain for the killed phi, but there is no 1399a06bfe05SJan Sjodin // need to do the renaming outside or inside the block. 1400a06bfe05SJan Sjodin SmallPtrSet<MachineInstr *, 2> PHIs; 1401a06bfe05SJan Sjodin for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(), 1402a06bfe05SJan Sjodin E = MBB->instr_end(); 1403a06bfe05SJan Sjodin I != E; ++I) { 1404a06bfe05SJan Sjodin MachineInstr &Instr = *I; 1405a06bfe05SJan Sjodin if (Instr.isPHI()) { 1406a06bfe05SJan Sjodin unsigned PHIDestReg = getPHIDestReg(Instr); 1407*dc6e8dfdSJacob Lambert LLVM_DEBUG(dbgs() << "Extracting killed phi:\n"); 1408d34e60caSNicola Zaghen LLVM_DEBUG(Instr.dump()); 1409a06bfe05SJan Sjodin PHIs.insert(&Instr); 1410a06bfe05SJan Sjodin PHIInfo.addDest(PHIDestReg, Instr.getDebugLoc()); 1411a06bfe05SJan Sjodin storePHILinearizationInfoDest(PHIDestReg, Instr); 1412a06bfe05SJan Sjodin } 1413a06bfe05SJan Sjodin } 1414a06bfe05SJan Sjodin 1415a06bfe05SJan Sjodin for (auto PI : PHIs) { 1416a06bfe05SJan Sjodin PI->eraseFromParent(); 1417a06bfe05SJan Sjodin } 1418a06bfe05SJan Sjodin } 1419a06bfe05SJan Sjodin 1420a06bfe05SJan Sjodin static bool isPHIRegionIndex(SmallVector<unsigned, 2> PHIRegionIndices, 1421a06bfe05SJan Sjodin unsigned Index) { 1422b4c0d610SKazu Hirata return llvm::is_contained(PHIRegionIndices, Index); 1423a06bfe05SJan Sjodin } 1424a06bfe05SJan Sjodin 1425a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::shrinkPHI(MachineInstr &PHI, 1426a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIIndices, 1427a06bfe05SJan Sjodin unsigned *ReplaceReg) { 1428a06bfe05SJan Sjodin return shrinkPHI(PHI, 0, nullptr, PHIIndices, ReplaceReg); 1429a06bfe05SJan Sjodin } 1430a06bfe05SJan Sjodin 1431a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::shrinkPHI(MachineInstr &PHI, 1432a06bfe05SJan Sjodin unsigned CombinedSourceReg, 1433a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB, 1434a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIIndices, 1435a06bfe05SJan Sjodin unsigned *ReplaceReg) { 1436d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Shrink PHI: "); 1437d34e60caSNicola Zaghen LLVM_DEBUG(PHI.dump()); 1438d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " to " << printReg(getPHIDestReg(PHI), TRI) 1439d34e60caSNicola Zaghen << " = PHI("); 1440a06bfe05SJan Sjodin 1441a06bfe05SJan Sjodin bool Replaced = false; 1442a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1443a06bfe05SJan Sjodin int SingleExternalEntryIndex = -1; 1444a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1445a06bfe05SJan Sjodin if (!isPHIRegionIndex(PHIIndices, i)) { 1446a06bfe05SJan Sjodin if (SingleExternalEntryIndex == -1) { 1447a06bfe05SJan Sjodin // Single entry 1448a06bfe05SJan Sjodin SingleExternalEntryIndex = i; 1449a06bfe05SJan Sjodin } else { 1450a06bfe05SJan Sjodin // Multiple entries 1451a06bfe05SJan Sjodin SingleExternalEntryIndex = -2; 1452a06bfe05SJan Sjodin } 1453a06bfe05SJan Sjodin } 1454a06bfe05SJan Sjodin } 1455a06bfe05SJan Sjodin 1456a06bfe05SJan Sjodin if (SingleExternalEntryIndex > -1) { 1457a06bfe05SJan Sjodin *ReplaceReg = getPHISourceReg(PHI, SingleExternalEntryIndex); 1458a06bfe05SJan Sjodin // We should not rewrite the code, we should only pick up the single value 1459a06bfe05SJan Sjodin // that represents the shrunk PHI. 1460a06bfe05SJan Sjodin Replaced = true; 1461a06bfe05SJan Sjodin } else { 1462a06bfe05SJan Sjodin MachineBasicBlock *MBB = PHI.getParent(); 1463a06bfe05SJan Sjodin MachineInstrBuilder MIB = 1464a06bfe05SJan Sjodin BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), 1465a06bfe05SJan Sjodin getPHIDestReg(PHI)); 1466a06bfe05SJan Sjodin if (SourceMBB) { 1467a06bfe05SJan Sjodin MIB.addReg(CombinedSourceReg); 1468a06bfe05SJan Sjodin MIB.addMBB(SourceMBB); 1469d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", " 147025528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourceMBB)); 1471a06bfe05SJan Sjodin } 1472a06bfe05SJan Sjodin 1473a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1474a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIIndices, i)) { 1475a06bfe05SJan Sjodin continue; 1476a06bfe05SJan Sjodin } 1477a06bfe05SJan Sjodin unsigned SourceReg = getPHISourceReg(PHI, i); 1478a06bfe05SJan Sjodin MachineBasicBlock *SourcePred = getPHIPred(PHI, i); 1479a06bfe05SJan Sjodin MIB.addReg(SourceReg); 1480a06bfe05SJan Sjodin MIB.addMBB(SourcePred); 1481d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 148225528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourcePred)); 1483a06bfe05SJan Sjodin } 1484d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 1485a06bfe05SJan Sjodin } 1486a06bfe05SJan Sjodin PHI.eraseFromParent(); 1487a06bfe05SJan Sjodin return Replaced; 1488a06bfe05SJan Sjodin } 1489a06bfe05SJan Sjodin 1490a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::replacePHI( 1491a06bfe05SJan Sjodin MachineInstr &PHI, unsigned CombinedSourceReg, MachineBasicBlock *LastMerge, 1492a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1493d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replace PHI: "); 1494d34e60caSNicola Zaghen LLVM_DEBUG(PHI.dump()); 1495d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " with " << printReg(getPHIDestReg(PHI), TRI) 1496d34e60caSNicola Zaghen << " = PHI("); 1497a06bfe05SJan Sjodin 1498a06bfe05SJan Sjodin bool HasExternalEdge = false; 1499a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1500a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1501a06bfe05SJan Sjodin if (!isPHIRegionIndex(PHIRegionIndices, i)) { 1502a06bfe05SJan Sjodin HasExternalEdge = true; 1503a06bfe05SJan Sjodin } 1504a06bfe05SJan Sjodin } 1505a06bfe05SJan Sjodin 1506a06bfe05SJan Sjodin if (HasExternalEdge) { 1507a06bfe05SJan Sjodin MachineBasicBlock *MBB = PHI.getParent(); 1508a06bfe05SJan Sjodin MachineInstrBuilder MIB = 1509a06bfe05SJan Sjodin BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), 1510a06bfe05SJan Sjodin getPHIDestReg(PHI)); 1511a06bfe05SJan Sjodin MIB.addReg(CombinedSourceReg); 1512a06bfe05SJan Sjodin MIB.addMBB(LastMerge); 1513d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", " 151425528d6dSFrancis Visoiu Mistrih << printMBBReference(*LastMerge)); 1515a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1516a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIRegionIndices, i)) { 1517a06bfe05SJan Sjodin continue; 1518a06bfe05SJan Sjodin } 1519a06bfe05SJan Sjodin unsigned SourceReg = getPHISourceReg(PHI, i); 1520a06bfe05SJan Sjodin MachineBasicBlock *SourcePred = getPHIPred(PHI, i); 1521a06bfe05SJan Sjodin MIB.addReg(SourceReg); 1522a06bfe05SJan Sjodin MIB.addMBB(SourcePred); 1523d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 152425528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourcePred)); 1525a06bfe05SJan Sjodin } 1526d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 1527a06bfe05SJan Sjodin } else { 1528a06bfe05SJan Sjodin replaceRegisterWith(getPHIDestReg(PHI), CombinedSourceReg); 1529a06bfe05SJan Sjodin } 1530a06bfe05SJan Sjodin PHI.eraseFromParent(); 1531a06bfe05SJan Sjodin } 1532a06bfe05SJan Sjodin 1533a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::replaceEntryPHI( 1534a06bfe05SJan Sjodin MachineInstr &PHI, unsigned CombinedSourceReg, MachineBasicBlock *IfMBB, 1535a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1536d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replace entry PHI: "); 1537d34e60caSNicola Zaghen LLVM_DEBUG(PHI.dump()); 1538d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " with "); 1539a06bfe05SJan Sjodin 1540a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1541a06bfe05SJan Sjodin unsigned NumNonRegionInputs = NumInputs; 1542a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1543a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIRegionIndices, i)) { 1544a06bfe05SJan Sjodin NumNonRegionInputs--; 1545a06bfe05SJan Sjodin } 1546a06bfe05SJan Sjodin } 1547a06bfe05SJan Sjodin 1548a06bfe05SJan Sjodin if (NumNonRegionInputs == 0) { 1549a06bfe05SJan Sjodin auto DestReg = getPHIDestReg(PHI); 1550a06bfe05SJan Sjodin replaceRegisterWith(DestReg, CombinedSourceReg); 1551d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " register " << printReg(CombinedSourceReg, TRI) 1552d34e60caSNicola Zaghen << "\n"); 1553a06bfe05SJan Sjodin PHI.eraseFromParent(); 1554a06bfe05SJan Sjodin } else { 1555d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(getPHIDestReg(PHI), TRI) << " = PHI("); 1556a06bfe05SJan Sjodin MachineBasicBlock *MBB = PHI.getParent(); 1557a06bfe05SJan Sjodin MachineInstrBuilder MIB = 1558a06bfe05SJan Sjodin BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), 1559a06bfe05SJan Sjodin getPHIDestReg(PHI)); 1560a06bfe05SJan Sjodin MIB.addReg(CombinedSourceReg); 1561a06bfe05SJan Sjodin MIB.addMBB(IfMBB); 1562d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", " 156325528d6dSFrancis Visoiu Mistrih << printMBBReference(*IfMBB)); 1564a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1565a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1566a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIRegionIndices, i)) { 1567a06bfe05SJan Sjodin continue; 1568a06bfe05SJan Sjodin } 1569a06bfe05SJan Sjodin unsigned SourceReg = getPHISourceReg(PHI, i); 1570a06bfe05SJan Sjodin MachineBasicBlock *SourcePred = getPHIPred(PHI, i); 1571a06bfe05SJan Sjodin MIB.addReg(SourceReg); 1572a06bfe05SJan Sjodin MIB.addMBB(SourcePred); 1573d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 157425528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourcePred)); 1575a06bfe05SJan Sjodin } 1576d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 1577a06bfe05SJan Sjodin PHI.eraseFromParent(); 1578a06bfe05SJan Sjodin } 1579a06bfe05SJan Sjodin } 1580a06bfe05SJan Sjodin 1581a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::replaceLiveOutRegs( 1582a06bfe05SJan Sjodin MachineInstr &PHI, SmallVector<unsigned, 2> &PHIRegionIndices, 1583a06bfe05SJan Sjodin unsigned CombinedSourceReg, LinearizedRegion *LRegion) { 1584a06bfe05SJan Sjodin bool WasLiveOut = false; 1585a06bfe05SJan Sjodin for (auto PII : PHIRegionIndices) { 1586a06bfe05SJan Sjodin unsigned Reg = getPHISourceReg(PHI, PII); 1587a06bfe05SJan Sjodin if (LRegion->isLiveOut(Reg)) { 1588a06bfe05SJan Sjodin bool IsDead = true; 1589a06bfe05SJan Sjodin 1590a06bfe05SJan Sjodin // Check if register is live out of the basic block 1591a06bfe05SJan Sjodin MachineBasicBlock *DefMBB = getDefInstr(Reg)->getParent(); 1592a06bfe05SJan Sjodin for (auto UI = MRI->use_begin(Reg), E = MRI->use_end(); UI != E; ++UI) { 1593a06bfe05SJan Sjodin if ((*UI).getParent()->getParent() != DefMBB) { 1594a06bfe05SJan Sjodin IsDead = false; 1595a06bfe05SJan Sjodin } 1596a06bfe05SJan Sjodin } 1597a06bfe05SJan Sjodin 1598d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " << printReg(Reg, TRI) << " is " 1599d34e60caSNicola Zaghen << (IsDead ? "dead" : "alive") 1600d34e60caSNicola Zaghen << " after PHI replace\n"); 1601a06bfe05SJan Sjodin if (IsDead) { 1602a06bfe05SJan Sjodin LRegion->removeLiveOut(Reg); 1603a06bfe05SJan Sjodin } 1604a06bfe05SJan Sjodin WasLiveOut = true; 1605a06bfe05SJan Sjodin } 1606a06bfe05SJan Sjodin } 1607a06bfe05SJan Sjodin 1608a06bfe05SJan Sjodin if (WasLiveOut) 1609a06bfe05SJan Sjodin LRegion->addLiveOut(CombinedSourceReg); 1610a06bfe05SJan Sjodin } 1611a06bfe05SJan Sjodin 1612a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionExitPHI(RegionMRT *Region, 1613a06bfe05SJan Sjodin MachineBasicBlock *LastMerge, 1614a06bfe05SJan Sjodin MachineInstr &PHI, 1615a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 1616a06bfe05SJan Sjodin SmallVector<unsigned, 2> PHIRegionIndices; 1617a06bfe05SJan Sjodin getPHIRegionIndices(Region, PHI, PHIRegionIndices); 1618a06bfe05SJan Sjodin unsigned LinearizedSourceReg = 1619a06bfe05SJan Sjodin storePHILinearizationInfo(PHI, &PHIRegionIndices); 1620a06bfe05SJan Sjodin 1621a06bfe05SJan Sjodin replacePHI(PHI, LinearizedSourceReg, LastMerge, PHIRegionIndices); 1622a06bfe05SJan Sjodin replaceLiveOutRegs(PHI, PHIRegionIndices, LinearizedSourceReg, LRegion); 1623a06bfe05SJan Sjodin } 1624a06bfe05SJan Sjodin 1625a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionEntryPHI(LinearizedRegion *Region, 1626a06bfe05SJan Sjodin MachineBasicBlock *IfMBB, 1627a06bfe05SJan Sjodin MachineInstr &PHI) { 1628a06bfe05SJan Sjodin SmallVector<unsigned, 2> PHINonRegionIndices; 1629a06bfe05SJan Sjodin getPHINonRegionIndices(Region, PHI, PHINonRegionIndices); 1630a06bfe05SJan Sjodin unsigned LinearizedSourceReg = 1631a06bfe05SJan Sjodin storePHILinearizationInfo(PHI, &PHINonRegionIndices); 1632a06bfe05SJan Sjodin replaceEntryPHI(PHI, LinearizedSourceReg, IfMBB, PHINonRegionIndices); 1633a06bfe05SJan Sjodin } 1634a06bfe05SJan Sjodin 1635a06bfe05SJan Sjodin static void collectPHIs(MachineBasicBlock *MBB, 1636a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> &PHIs) { 1637a06bfe05SJan Sjodin for (auto &BBI : *MBB) { 1638a06bfe05SJan Sjodin if (BBI.isPHI()) { 1639a06bfe05SJan Sjodin PHIs.push_back(&BBI); 1640a06bfe05SJan Sjodin } 1641a06bfe05SJan Sjodin } 1642a06bfe05SJan Sjodin } 1643a06bfe05SJan Sjodin 1644a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionExitPHIs(RegionMRT *Region, 1645a06bfe05SJan Sjodin MachineBasicBlock *LastMerge, 1646a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 1647a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> PHIs; 1648a06bfe05SJan Sjodin auto Exit = Region->getSucc(); 1649a06bfe05SJan Sjodin if (Exit == nullptr) 1650a06bfe05SJan Sjodin return; 1651a06bfe05SJan Sjodin 1652a06bfe05SJan Sjodin collectPHIs(Exit, PHIs); 1653a06bfe05SJan Sjodin 1654a06bfe05SJan Sjodin for (auto PHII : PHIs) { 1655a06bfe05SJan Sjodin rewriteRegionExitPHI(Region, LastMerge, *PHII, LRegion); 1656a06bfe05SJan Sjodin } 1657a06bfe05SJan Sjodin } 1658a06bfe05SJan Sjodin 1659a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionEntryPHIs(LinearizedRegion *Region, 1660a06bfe05SJan Sjodin MachineBasicBlock *IfMBB) { 1661a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> PHIs; 1662a06bfe05SJan Sjodin auto Entry = Region->getEntry(); 1663a06bfe05SJan Sjodin 1664a06bfe05SJan Sjodin collectPHIs(Entry, PHIs); 1665a06bfe05SJan Sjodin 1666a06bfe05SJan Sjodin for (auto PHII : PHIs) { 1667a06bfe05SJan Sjodin rewriteRegionEntryPHI(Region, IfMBB, *PHII); 1668a06bfe05SJan Sjodin } 1669a06bfe05SJan Sjodin } 1670a06bfe05SJan Sjodin 1671a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::insertUnconditionalBranch(MachineBasicBlock *MBB, 1672a06bfe05SJan Sjodin MachineBasicBlock *Dest, 1673a06bfe05SJan Sjodin const DebugLoc &DL) { 1674d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Inserting unconditional branch: " << MBB->getNumber() 1675a06bfe05SJan Sjodin << " -> " << Dest->getNumber() << "\n"); 1676a06bfe05SJan Sjodin MachineBasicBlock::instr_iterator Terminator = MBB->getFirstInstrTerminator(); 1677a06bfe05SJan Sjodin bool HasTerminator = Terminator != MBB->instr_end(); 1678a06bfe05SJan Sjodin if (HasTerminator) { 1679a06bfe05SJan Sjodin TII->ReplaceTailWithBranchTo(Terminator, Dest); 1680a06bfe05SJan Sjodin } 1681a06bfe05SJan Sjodin if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(Dest)) { 1682a06bfe05SJan Sjodin TII->insertUnconditionalBranch(*MBB, Dest, DL); 1683a06bfe05SJan Sjodin } 1684a06bfe05SJan Sjodin } 1685a06bfe05SJan Sjodin 1686a06bfe05SJan Sjodin static MachineBasicBlock *getSingleExitNode(MachineFunction &MF) { 1687a06bfe05SJan Sjodin MachineBasicBlock *result = nullptr; 1688a06bfe05SJan Sjodin for (auto &MFI : MF) { 1689c9fca53aSKazu Hirata if (MFI.succ_empty()) { 1690a06bfe05SJan Sjodin if (result == nullptr) { 1691a06bfe05SJan Sjodin result = &MFI; 1692a06bfe05SJan Sjodin } else { 1693a06bfe05SJan Sjodin return nullptr; 1694a06bfe05SJan Sjodin } 1695a06bfe05SJan Sjodin } 1696a06bfe05SJan Sjodin } 1697a06bfe05SJan Sjodin 1698a06bfe05SJan Sjodin return result; 1699a06bfe05SJan Sjodin } 1700a06bfe05SJan Sjodin 1701a06bfe05SJan Sjodin static bool hasOneExitNode(MachineFunction &MF) { 1702a06bfe05SJan Sjodin return getSingleExitNode(MF) != nullptr; 1703a06bfe05SJan Sjodin } 1704a06bfe05SJan Sjodin 1705a06bfe05SJan Sjodin MachineBasicBlock * 1706a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::createLinearizedExitBlock(RegionMRT *Region) { 1707a06bfe05SJan Sjodin auto Exit = Region->getSucc(); 1708a06bfe05SJan Sjodin 1709a06bfe05SJan Sjodin // If the exit is the end of the function, we just use the existing 1710a06bfe05SJan Sjodin MachineFunction *MF = Region->getEntry()->getParent(); 1711a06bfe05SJan Sjodin if (Exit == nullptr && hasOneExitNode(*MF)) { 1712a06bfe05SJan Sjodin return &(*(--(Region->getEntry()->getParent()->end()))); 1713a06bfe05SJan Sjodin } 1714a06bfe05SJan Sjodin 1715a06bfe05SJan Sjodin MachineBasicBlock *LastMerge = MF->CreateMachineBasicBlock(); 1716a06bfe05SJan Sjodin if (Exit == nullptr) { 1717a06bfe05SJan Sjodin MachineFunction::iterator ExitIter = MF->end(); 1718a06bfe05SJan Sjodin MF->insert(ExitIter, LastMerge); 1719a06bfe05SJan Sjodin } else { 1720a06bfe05SJan Sjodin MachineFunction::iterator ExitIter = Exit->getIterator(); 1721a06bfe05SJan Sjodin MF->insert(ExitIter, LastMerge); 1722a06bfe05SJan Sjodin LastMerge->addSuccessor(Exit); 1723a06bfe05SJan Sjodin insertUnconditionalBranch(LastMerge, Exit); 1724d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Created exit block: " << LastMerge->getNumber() 1725d34e60caSNicola Zaghen << "\n"); 1726a06bfe05SJan Sjodin } 1727a06bfe05SJan Sjodin return LastMerge; 1728a06bfe05SJan Sjodin } 1729a06bfe05SJan Sjodin 1730a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::insertMergePHI(MachineBasicBlock *IfBB, 1731a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 1732a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1733a06bfe05SJan Sjodin unsigned DestRegister, 1734a06bfe05SJan Sjodin unsigned IfSourceRegister, 1735a06bfe05SJan Sjodin unsigned CodeSourceRegister, 1736a06bfe05SJan Sjodin bool IsUndefIfSource) { 1737a06bfe05SJan Sjodin // If this is the function exit block, we don't need a phi. 1738a06bfe05SJan Sjodin if (MergeBB->succ_begin() == MergeBB->succ_end()) { 1739a06bfe05SJan Sjodin return; 1740a06bfe05SJan Sjodin } 1741d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Merge PHI (" << printMBBReference(*MergeBB) 1742a8a83d15SFrancis Visoiu Mistrih << "): " << printReg(DestRegister, TRI) << " = PHI(" 174325528d6dSFrancis Visoiu Mistrih << printReg(IfSourceRegister, TRI) << ", " 1744d34e60caSNicola Zaghen << printMBBReference(*IfBB) 1745d34e60caSNicola Zaghen << printReg(CodeSourceRegister, TRI) << ", " 1746d34e60caSNicola Zaghen << printMBBReference(*CodeBB) << ")\n"); 1747a06bfe05SJan Sjodin const DebugLoc &DL = MergeBB->findDebugLoc(MergeBB->begin()); 1748a06bfe05SJan Sjodin MachineInstrBuilder MIB = BuildMI(*MergeBB, MergeBB->instr_begin(), DL, 1749a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), DestRegister); 1750a06bfe05SJan Sjodin if (IsUndefIfSource && false) { 1751a06bfe05SJan Sjodin MIB.addReg(IfSourceRegister, RegState::Undef); 1752a06bfe05SJan Sjodin } else { 1753a06bfe05SJan Sjodin MIB.addReg(IfSourceRegister); 1754a06bfe05SJan Sjodin } 1755a06bfe05SJan Sjodin MIB.addMBB(IfBB); 1756a06bfe05SJan Sjodin MIB.addReg(CodeSourceRegister); 1757a06bfe05SJan Sjodin MIB.addMBB(CodeBB); 1758a06bfe05SJan Sjodin } 1759a06bfe05SJan Sjodin 1760a06bfe05SJan Sjodin static void removeExternalCFGSuccessors(MachineBasicBlock *MBB) { 1761a06bfe05SJan Sjodin for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(), 1762a06bfe05SJan Sjodin E = MBB->succ_end(); 1763a06bfe05SJan Sjodin PI != E; ++PI) { 1764a06bfe05SJan Sjodin if ((*PI) != MBB) { 1765a06bfe05SJan Sjodin (MBB)->removeSuccessor(*PI); 1766a06bfe05SJan Sjodin } 1767a06bfe05SJan Sjodin } 1768a06bfe05SJan Sjodin } 1769a06bfe05SJan Sjodin 1770a06bfe05SJan Sjodin static void removeExternalCFGEdges(MachineBasicBlock *StartMBB, 1771a06bfe05SJan Sjodin MachineBasicBlock *EndMBB) { 1772a06bfe05SJan Sjodin 1773*dc6e8dfdSJacob Lambert // We have to check against the StartMBB successor because a 1774a06bfe05SJan Sjodin // structurized region with a loop will have the entry block split, 1775a06bfe05SJan Sjodin // and the backedge will go to the entry successor. 1776a06bfe05SJan Sjodin DenseSet<std::pair<MachineBasicBlock *, MachineBasicBlock *>> Succs; 1777a06bfe05SJan Sjodin unsigned SuccSize = StartMBB->succ_size(); 1778a06bfe05SJan Sjodin if (SuccSize > 0) { 1779a06bfe05SJan Sjodin MachineBasicBlock *StartMBBSucc = *(StartMBB->succ_begin()); 1780a06bfe05SJan Sjodin for (MachineBasicBlock::succ_iterator PI = EndMBB->succ_begin(), 1781a06bfe05SJan Sjodin E = EndMBB->succ_end(); 1782a06bfe05SJan Sjodin PI != E; ++PI) { 1783a06bfe05SJan Sjodin // Either we have a back-edge to the entry block, or a back-edge to the 17846a391bbfSHiroshi Inoue // successor of the entry block since the block may be split. 1785a06bfe05SJan Sjodin if ((*PI) != StartMBB && 1786a06bfe05SJan Sjodin !((*PI) == StartMBBSucc && StartMBB != EndMBB && SuccSize == 1)) { 1787a06bfe05SJan Sjodin Succs.insert( 1788a06bfe05SJan Sjodin std::pair<MachineBasicBlock *, MachineBasicBlock *>(EndMBB, *PI)); 1789a06bfe05SJan Sjodin } 1790a06bfe05SJan Sjodin } 1791a06bfe05SJan Sjodin } 1792a06bfe05SJan Sjodin 1793a06bfe05SJan Sjodin for (MachineBasicBlock::pred_iterator PI = StartMBB->pred_begin(), 1794a06bfe05SJan Sjodin E = StartMBB->pred_end(); 1795a06bfe05SJan Sjodin PI != E; ++PI) { 1796a06bfe05SJan Sjodin if ((*PI) != EndMBB) { 1797a06bfe05SJan Sjodin Succs.insert( 1798a06bfe05SJan Sjodin std::pair<MachineBasicBlock *, MachineBasicBlock *>(*PI, StartMBB)); 1799a06bfe05SJan Sjodin } 1800a06bfe05SJan Sjodin } 1801a06bfe05SJan Sjodin 1802a06bfe05SJan Sjodin for (auto SI : Succs) { 1803a06bfe05SJan Sjodin std::pair<MachineBasicBlock *, MachineBasicBlock *> Edge = SI; 1804d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Removing edge: " << printMBBReference(*Edge.first) 180525528d6dSFrancis Visoiu Mistrih << " -> " << printMBBReference(*Edge.second) << "\n"); 1806a06bfe05SJan Sjodin Edge.first->removeSuccessor(Edge.second); 1807a06bfe05SJan Sjodin } 1808a06bfe05SJan Sjodin } 1809a06bfe05SJan Sjodin 1810a06bfe05SJan Sjodin MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfBlock( 1811a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, MachineBasicBlock *CodeBBStart, 1812a06bfe05SJan Sjodin MachineBasicBlock *CodeBBEnd, MachineBasicBlock *SelectBB, unsigned IfReg, 1813a06bfe05SJan Sjodin bool InheritPreds) { 1814a06bfe05SJan Sjodin MachineFunction *MF = MergeBB->getParent(); 1815a06bfe05SJan Sjodin MachineBasicBlock *IfBB = MF->CreateMachineBasicBlock(); 1816a06bfe05SJan Sjodin 1817a06bfe05SJan Sjodin if (InheritPreds) { 1818a06bfe05SJan Sjodin for (MachineBasicBlock::pred_iterator PI = CodeBBStart->pred_begin(), 1819a06bfe05SJan Sjodin E = CodeBBStart->pred_end(); 1820a06bfe05SJan Sjodin PI != E; ++PI) { 1821a06bfe05SJan Sjodin if ((*PI) != CodeBBEnd) { 1822a06bfe05SJan Sjodin MachineBasicBlock *Pred = (*PI); 1823a06bfe05SJan Sjodin Pred->addSuccessor(IfBB); 1824a06bfe05SJan Sjodin } 1825a06bfe05SJan Sjodin } 1826a06bfe05SJan Sjodin } 1827a06bfe05SJan Sjodin 1828a06bfe05SJan Sjodin removeExternalCFGEdges(CodeBBStart, CodeBBEnd); 1829a06bfe05SJan Sjodin 1830a06bfe05SJan Sjodin auto CodeBBStartI = CodeBBStart->getIterator(); 1831a06bfe05SJan Sjodin auto CodeBBEndI = CodeBBEnd->getIterator(); 1832a06bfe05SJan Sjodin auto MergeIter = MergeBB->getIterator(); 1833a06bfe05SJan Sjodin MF->insert(MergeIter, IfBB); 1834a06bfe05SJan Sjodin MF->splice(MergeIter, CodeBBStartI, ++CodeBBEndI); 1835a06bfe05SJan Sjodin IfBB->addSuccessor(MergeBB); 1836a06bfe05SJan Sjodin IfBB->addSuccessor(CodeBBStart); 1837a06bfe05SJan Sjodin 1838d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Created If block: " << IfBB->getNumber() << "\n"); 18396a391bbfSHiroshi Inoue // Ensure that the MergeBB is a successor of the CodeEndBB. 1840a06bfe05SJan Sjodin if (!CodeBBEnd->isSuccessor(MergeBB)) 1841a06bfe05SJan Sjodin CodeBBEnd->addSuccessor(MergeBB); 1842a06bfe05SJan Sjodin 1843d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Moved " << printMBBReference(*CodeBBStart) 1844d34e60caSNicola Zaghen << " through " << printMBBReference(*CodeBBEnd) << "\n"); 1845a06bfe05SJan Sjodin 1846a06bfe05SJan Sjodin // If we have a single predecessor we can find a reasonable debug location 1847a06bfe05SJan Sjodin MachineBasicBlock *SinglePred = 1848a06bfe05SJan Sjodin CodeBBStart->pred_size() == 1 ? *(CodeBBStart->pred_begin()) : nullptr; 1849a06bfe05SJan Sjodin const DebugLoc &DL = SinglePred 1850a06bfe05SJan Sjodin ? SinglePred->findDebugLoc(SinglePred->getFirstTerminator()) 1851a06bfe05SJan Sjodin : DebugLoc(); 1852a06bfe05SJan Sjodin 185398de0d22SJay Foad Register Reg = 1854a06bfe05SJan Sjodin TII->insertEQ(IfBB, IfBB->begin(), DL, IfReg, 1855a06bfe05SJan Sjodin SelectBB->getNumber() /* CodeBBStart->getNumber() */); 1856a06bfe05SJan Sjodin if (&(*(IfBB->getParent()->begin())) == IfBB) { 1857a06bfe05SJan Sjodin TII->materializeImmediate(*IfBB, IfBB->begin(), DL, IfReg, 1858a06bfe05SJan Sjodin CodeBBStart->getNumber()); 1859a06bfe05SJan Sjodin } 1860a06bfe05SJan Sjodin MachineOperand RegOp = MachineOperand::CreateReg(Reg, false, false, true); 1861a06bfe05SJan Sjodin ArrayRef<MachineOperand> Cond(RegOp); 1862a06bfe05SJan Sjodin TII->insertBranch(*IfBB, MergeBB, CodeBBStart, Cond, DL); 1863a06bfe05SJan Sjodin 1864a06bfe05SJan Sjodin return IfBB; 1865a06bfe05SJan Sjodin } 1866a06bfe05SJan Sjodin 1867a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::ensureCondIsNotKilled( 1868a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> Cond) { 1869a06bfe05SJan Sjodin if (Cond.size() != 1) 1870a06bfe05SJan Sjodin return; 1871a06bfe05SJan Sjodin if (!Cond[0].isReg()) 1872a06bfe05SJan Sjodin return; 1873a06bfe05SJan Sjodin 18740c476111SDaniel Sanders Register CondReg = Cond[0].getReg(); 1875a06bfe05SJan Sjodin for (auto UI = MRI->use_begin(CondReg), E = MRI->use_end(); UI != E; ++UI) { 1876a06bfe05SJan Sjodin (*UI).setIsKill(false); 1877a06bfe05SJan Sjodin } 1878a06bfe05SJan Sjodin } 1879a06bfe05SJan Sjodin 1880a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteCodeBBTerminator(MachineBasicBlock *CodeBB, 1881a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1882a06bfe05SJan Sjodin unsigned BBSelectReg) { 1883a06bfe05SJan Sjodin MachineBasicBlock *TrueBB = nullptr; 1884a06bfe05SJan Sjodin MachineBasicBlock *FalseBB = nullptr; 1885a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> Cond; 1886a06bfe05SJan Sjodin MachineBasicBlock *FallthroughBB = FallthroughMap[CodeBB]; 1887a06bfe05SJan Sjodin TII->analyzeBranch(*CodeBB, TrueBB, FalseBB, Cond); 1888a06bfe05SJan Sjodin 1889a06bfe05SJan Sjodin const DebugLoc &DL = CodeBB->findDebugLoc(CodeBB->getFirstTerminator()); 1890a06bfe05SJan Sjodin 1891a06bfe05SJan Sjodin if (FalseBB == nullptr && TrueBB == nullptr && FallthroughBB == nullptr) { 1892a06bfe05SJan Sjodin // This is an exit block, hence no successors. We will assign the 1893a06bfe05SJan Sjodin // bb select register to the entry block. 1894a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1895a06bfe05SJan Sjodin BBSelectReg, 1896a06bfe05SJan Sjodin CodeBB->getParent()->begin()->getNumber()); 1897a06bfe05SJan Sjodin insertUnconditionalBranch(CodeBB, MergeBB, DL); 1898a06bfe05SJan Sjodin return; 1899a06bfe05SJan Sjodin } 1900a06bfe05SJan Sjodin 1901a06bfe05SJan Sjodin if (FalseBB == nullptr && TrueBB == nullptr) { 1902a06bfe05SJan Sjodin TrueBB = FallthroughBB; 1903a06bfe05SJan Sjodin } else if (TrueBB != nullptr) { 1904a06bfe05SJan Sjodin FalseBB = 1905a06bfe05SJan Sjodin (FallthroughBB && (FallthroughBB != TrueBB)) ? FallthroughBB : FalseBB; 1906a06bfe05SJan Sjodin } 1907a06bfe05SJan Sjodin 1908a06bfe05SJan Sjodin if ((TrueBB != nullptr && FalseBB == nullptr) || (TrueBB == FalseBB)) { 1909a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1910a06bfe05SJan Sjodin BBSelectReg, TrueBB->getNumber()); 1911a06bfe05SJan Sjodin } else { 1912a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(BBSelectReg); 19130c476111SDaniel Sanders Register TrueBBReg = MRI->createVirtualRegister(RegClass); 19140c476111SDaniel Sanders Register FalseBBReg = MRI->createVirtualRegister(RegClass); 1915a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1916a06bfe05SJan Sjodin TrueBBReg, TrueBB->getNumber()); 1917a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1918a06bfe05SJan Sjodin FalseBBReg, FalseBB->getNumber()); 1919a06bfe05SJan Sjodin ensureCondIsNotKilled(Cond); 1920a06bfe05SJan Sjodin TII->insertVectorSelect(*CodeBB, CodeBB->getFirstTerminator(), DL, 1921a06bfe05SJan Sjodin BBSelectReg, Cond, TrueBBReg, FalseBBReg); 1922a06bfe05SJan Sjodin } 1923a06bfe05SJan Sjodin 1924a06bfe05SJan Sjodin insertUnconditionalBranch(CodeBB, MergeBB, DL); 1925a06bfe05SJan Sjodin } 1926a06bfe05SJan Sjodin 1927a06bfe05SJan Sjodin MachineInstr *AMDGPUMachineCFGStructurizer::getDefInstr(unsigned Reg) { 1928a06bfe05SJan Sjodin if (MRI->def_begin(Reg) == MRI->def_end()) { 1929d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " 1930d34e60caSNicola Zaghen << printReg(Reg, MRI->getTargetRegisterInfo()) 1931a06bfe05SJan Sjodin << " has NO defs\n"); 1932a06bfe05SJan Sjodin } else if (!MRI->hasOneDef(Reg)) { 1933d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " 1934d34e60caSNicola Zaghen << printReg(Reg, MRI->getTargetRegisterInfo()) 1935a06bfe05SJan Sjodin << " has multiple defs\n"); 1936d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DEFS BEGIN:\n"); 1937a06bfe05SJan Sjodin for (auto DI = MRI->def_begin(Reg), DE = MRI->def_end(); DI != DE; ++DI) { 1938d34e60caSNicola Zaghen LLVM_DEBUG(DI->getParent()->dump()); 1939a06bfe05SJan Sjodin } 1940d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DEFS END\n"); 1941a06bfe05SJan Sjodin } 1942a06bfe05SJan Sjodin 1943a06bfe05SJan Sjodin assert(MRI->hasOneDef(Reg) && "Register has multiple definitions"); 1944a06bfe05SJan Sjodin return (*(MRI->def_begin(Reg))).getParent(); 1945a06bfe05SJan Sjodin } 1946a06bfe05SJan Sjodin 1947a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::insertChainedPHI(MachineBasicBlock *IfBB, 1948a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 1949a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1950a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 1951a06bfe05SJan Sjodin unsigned DestReg, 1952a06bfe05SJan Sjodin unsigned SourceReg) { 1953a06bfe05SJan Sjodin // In this function we know we are part of a chain already, so we need 1954a06bfe05SJan Sjodin // to add the registers to the existing chain, and rename the register 1955a06bfe05SJan Sjodin // inside the region. 1956a06bfe05SJan Sjodin bool IsSingleBB = InnerRegion->getEntry() == InnerRegion->getExit(); 1957a06bfe05SJan Sjodin MachineInstr *DefInstr = getDefInstr(SourceReg); 1958a06bfe05SJan Sjodin if (DefInstr->isPHI() && DefInstr->getParent() == CodeBB && IsSingleBB) { 1959a06bfe05SJan Sjodin // Handle the case where the def is a PHI-def inside a basic 1960a06bfe05SJan Sjodin // block, then we only need to do renaming. Special care needs to 1961a06bfe05SJan Sjodin // be taken if the PHI-def is part of an existing chain, or if a 1962a06bfe05SJan Sjodin // new one needs to be created. 1963a06bfe05SJan Sjodin InnerRegion->replaceRegisterInsideRegion(SourceReg, DestReg, true, MRI); 1964a06bfe05SJan Sjodin 1965a06bfe05SJan Sjodin // We collect all PHI Information, and if we are at the region entry, 1966a06bfe05SJan Sjodin // all PHIs will be removed, and then re-introduced if needed. 1967a06bfe05SJan Sjodin storePHILinearizationInfoDest(DestReg, *DefInstr); 1968a06bfe05SJan Sjodin // We have picked up all the information we need now and can remove 1969a06bfe05SJan Sjodin // the PHI 1970a06bfe05SJan Sjodin PHIInfo.removeSource(DestReg, SourceReg, CodeBB); 1971a06bfe05SJan Sjodin DefInstr->eraseFromParent(); 1972a06bfe05SJan Sjodin } else { 1973a06bfe05SJan Sjodin // If this is not a phi-def, or it is a phi-def but from a linearized region 1974a06bfe05SJan Sjodin if (IsSingleBB && DefInstr->getParent() == InnerRegion->getEntry()) { 1975a06bfe05SJan Sjodin // If this is a single BB and the definition is in this block we 1976a06bfe05SJan Sjodin // need to replace any uses outside the region. 1977a06bfe05SJan Sjodin InnerRegion->replaceRegisterOutsideRegion(SourceReg, DestReg, false, MRI); 1978a06bfe05SJan Sjodin } 1979a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(DestReg); 19800c476111SDaniel Sanders Register NextDestReg = MRI->createVirtualRegister(RegClass); 1981a06bfe05SJan Sjodin bool IsLastDef = PHIInfo.getNumSources(DestReg) == 1; 1982d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert Chained PHI\n"); 1983a06bfe05SJan Sjodin insertMergePHI(IfBB, InnerRegion->getExit(), MergeBB, DestReg, NextDestReg, 1984a06bfe05SJan Sjodin SourceReg, IsLastDef); 1985a06bfe05SJan Sjodin 1986a06bfe05SJan Sjodin PHIInfo.removeSource(DestReg, SourceReg, CodeBB); 1987a06bfe05SJan Sjodin if (IsLastDef) { 1988a06bfe05SJan Sjodin const DebugLoc &DL = IfBB->findDebugLoc(IfBB->getFirstTerminator()); 1989a06bfe05SJan Sjodin TII->materializeImmediate(*IfBB, IfBB->getFirstTerminator(), DL, 1990a06bfe05SJan Sjodin NextDestReg, 0); 1991a06bfe05SJan Sjodin PHIInfo.deleteDef(DestReg); 1992a06bfe05SJan Sjodin } else { 1993a06bfe05SJan Sjodin PHIInfo.replaceDef(DestReg, NextDestReg); 1994a06bfe05SJan Sjodin } 1995a06bfe05SJan Sjodin } 1996a06bfe05SJan Sjodin } 1997a06bfe05SJan Sjodin 1998a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::containsDef(MachineBasicBlock *MBB, 1999a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 2000a06bfe05SJan Sjodin unsigned Register) { 2001a06bfe05SJan Sjodin return getDefInstr(Register)->getParent() == MBB || 2002a06bfe05SJan Sjodin InnerRegion->contains(getDefInstr(Register)->getParent()); 2003a06bfe05SJan Sjodin } 2004a06bfe05SJan Sjodin 2005a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteLiveOutRegs(MachineBasicBlock *IfBB, 2006a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 2007a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 2008a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 2009a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 2010a06bfe05SJan Sjodin DenseSet<unsigned> *LiveOuts = InnerRegion->getLiveOuts(); 2011a06bfe05SJan Sjodin SmallVector<unsigned, 4> OldLiveOuts; 2012a06bfe05SJan Sjodin bool IsSingleBB = InnerRegion->getEntry() == InnerRegion->getExit(); 2013a06bfe05SJan Sjodin for (auto OLI : *LiveOuts) { 2014a06bfe05SJan Sjodin OldLiveOuts.push_back(OLI); 2015a06bfe05SJan Sjodin } 2016a06bfe05SJan Sjodin 2017a06bfe05SJan Sjodin for (auto LI : OldLiveOuts) { 2018d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LiveOut: " << printReg(LI, TRI)); 2019a06bfe05SJan Sjodin if (!containsDef(CodeBB, InnerRegion, LI) || 2020a06bfe05SJan Sjodin (!IsSingleBB && (getDefInstr(LI)->getParent() == LRegion->getExit()))) { 2021*dc6e8dfdSJacob Lambert // If the register simply lives through the CodeBB, we don't have 2022a06bfe05SJan Sjodin // to rewrite anything since the register is not defined in this 2023a06bfe05SJan Sjodin // part of the code. 2024d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "- through"); 2025a06bfe05SJan Sjodin continue; 2026a06bfe05SJan Sjodin } 2027d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2028a06bfe05SJan Sjodin unsigned Reg = LI; 2029a06bfe05SJan Sjodin if (/*!PHIInfo.isSource(Reg) &&*/ Reg != InnerRegion->getBBSelectRegOut()) { 2030a06bfe05SJan Sjodin // If the register is live out, we do want to create a phi, 2031*dc6e8dfdSJacob Lambert // unless it is from the Exit block, because in that case there 2032a06bfe05SJan Sjodin // is already a PHI, and no need to create a new one. 2033a06bfe05SJan Sjodin 2034a06bfe05SJan Sjodin // If the register is just a live out def and not part of a phi 2035a06bfe05SJan Sjodin // chain, we need to create a PHI node to handle the if region, 2036a06bfe05SJan Sjodin // and replace all uses outside of the region with the new dest 2037a06bfe05SJan Sjodin // register, unless it is the outgoing BB select register. We have 2038*dc6e8dfdSJacob Lambert // already created phi nodes for these. 2039a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(Reg); 20400c476111SDaniel Sanders Register PHIDestReg = MRI->createVirtualRegister(RegClass); 20410c476111SDaniel Sanders Register IfSourceReg = MRI->createVirtualRegister(RegClass); 2042a06bfe05SJan Sjodin // Create initializer, this value is never used, but is needed 2043a06bfe05SJan Sjodin // to satisfy SSA. 2044d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Initializer for reg: " << printReg(Reg) << "\n"); 2045a06bfe05SJan Sjodin TII->materializeImmediate(*IfBB, IfBB->getFirstTerminator(), DebugLoc(), 2046a06bfe05SJan Sjodin IfSourceReg, 0); 2047a06bfe05SJan Sjodin 2048a06bfe05SJan Sjodin InnerRegion->replaceRegisterOutsideRegion(Reg, PHIDestReg, true, MRI); 2049d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert Non-Chained Live out PHI\n"); 2050a06bfe05SJan Sjodin insertMergePHI(IfBB, InnerRegion->getExit(), MergeBB, PHIDestReg, 2051a06bfe05SJan Sjodin IfSourceReg, Reg, true); 2052a06bfe05SJan Sjodin } 2053a06bfe05SJan Sjodin } 2054a06bfe05SJan Sjodin 2055a06bfe05SJan Sjodin // Handle the chained definitions in PHIInfo, checking if this basic block 2056a06bfe05SJan Sjodin // is a source block for a definition. 2057a06bfe05SJan Sjodin SmallVector<unsigned, 4> Sources; 2058a06bfe05SJan Sjodin if (PHIInfo.findSourcesFromMBB(CodeBB, Sources)) { 2059d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Inserting PHI Live Out from " 2060d34e60caSNicola Zaghen << printMBBReference(*CodeBB) << "\n"); 2061a06bfe05SJan Sjodin for (auto SI : Sources) { 2062a06bfe05SJan Sjodin unsigned DestReg; 2063a06bfe05SJan Sjodin PHIInfo.findDest(SI, CodeBB, DestReg); 2064a06bfe05SJan Sjodin insertChainedPHI(IfBB, CodeBB, MergeBB, InnerRegion, DestReg, SI); 2065a06bfe05SJan Sjodin } 2066d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insertion done.\n"); 2067a06bfe05SJan Sjodin } 2068a06bfe05SJan Sjodin 2069d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2070a06bfe05SJan Sjodin } 2071a06bfe05SJan Sjodin 2072a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::prunePHIInfo(MachineBasicBlock *MBB) { 2073d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Before PHI Prune\n"); 2074d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2075a06bfe05SJan Sjodin SmallVector<std::tuple<unsigned, unsigned, MachineBasicBlock *>, 4> 2076a06bfe05SJan Sjodin ElimiatedSources; 2077a06bfe05SJan Sjodin for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; 2078a06bfe05SJan Sjodin ++DRI) { 2079a06bfe05SJan Sjodin 2080a06bfe05SJan Sjodin unsigned DestReg = *DRI; 2081a06bfe05SJan Sjodin auto SE = PHIInfo.sources_end(DestReg); 2082a06bfe05SJan Sjodin 2083a06bfe05SJan Sjodin bool MBBContainsPHISource = false; 2084a06bfe05SJan Sjodin // Check if there is a PHI source in this MBB 2085a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2086a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2087a06bfe05SJan Sjodin MachineOperand *Def = &(*(MRI->def_begin(SourceReg))); 2088a06bfe05SJan Sjodin if (Def->getParent()->getParent() == MBB) { 2089a06bfe05SJan Sjodin MBBContainsPHISource = true; 2090a06bfe05SJan Sjodin } 2091a06bfe05SJan Sjodin } 2092a06bfe05SJan Sjodin 2093a06bfe05SJan Sjodin // If so, all other sources are useless since we know this block 2094a06bfe05SJan Sjodin // is always executed when the region is executed. 2095a06bfe05SJan Sjodin if (MBBContainsPHISource) { 2096a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2097a06bfe05SJan Sjodin PHILinearize::PHISourceT Source = *SRI; 2098a06bfe05SJan Sjodin unsigned SourceReg = Source.first; 2099a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB = Source.second; 2100a06bfe05SJan Sjodin MachineOperand *Def = &(*(MRI->def_begin(SourceReg))); 2101a06bfe05SJan Sjodin if (Def->getParent()->getParent() != MBB) { 2102a06bfe05SJan Sjodin ElimiatedSources.push_back( 2103a06bfe05SJan Sjodin std::make_tuple(DestReg, SourceReg, SourceMBB)); 2104a06bfe05SJan Sjodin } 2105a06bfe05SJan Sjodin } 2106a06bfe05SJan Sjodin } 2107a06bfe05SJan Sjodin } 2108a06bfe05SJan Sjodin 2109a06bfe05SJan Sjodin // Remove the PHI sources that are in the given MBB 2110a06bfe05SJan Sjodin for (auto &SourceInfo : ElimiatedSources) { 2111a06bfe05SJan Sjodin PHIInfo.removeSource(std::get<0>(SourceInfo), std::get<1>(SourceInfo), 2112a06bfe05SJan Sjodin std::get<2>(SourceInfo)); 2113a06bfe05SJan Sjodin } 2114d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "After PHI Prune\n"); 2115d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2116a06bfe05SJan Sjodin } 2117a06bfe05SJan Sjodin 2118a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::createEntryPHI(LinearizedRegion *CurrentRegion, 2119a06bfe05SJan Sjodin unsigned DestReg) { 2120a06bfe05SJan Sjodin MachineBasicBlock *Entry = CurrentRegion->getEntry(); 2121a06bfe05SJan Sjodin MachineBasicBlock *Exit = CurrentRegion->getExit(); 2122a06bfe05SJan Sjodin 2123d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "RegionExit: " << Exit->getNumber() << " Pred: " 2124d34e60caSNicola Zaghen << (*(Entry->pred_begin()))->getNumber() << "\n"); 2125a06bfe05SJan Sjodin 2126a06bfe05SJan Sjodin int NumSources = 0; 2127a06bfe05SJan Sjodin auto SE = PHIInfo.sources_end(DestReg); 2128a06bfe05SJan Sjodin 2129a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2130a06bfe05SJan Sjodin NumSources++; 2131a06bfe05SJan Sjodin } 2132a06bfe05SJan Sjodin 2133a06bfe05SJan Sjodin if (NumSources == 1) { 2134a06bfe05SJan Sjodin auto SRI = PHIInfo.sources_begin(DestReg); 2135a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2136a06bfe05SJan Sjodin replaceRegisterWith(DestReg, SourceReg); 2137a06bfe05SJan Sjodin } else { 2138a06bfe05SJan Sjodin const DebugLoc &DL = Entry->findDebugLoc(Entry->begin()); 2139a06bfe05SJan Sjodin MachineInstrBuilder MIB = BuildMI(*Entry, Entry->instr_begin(), DL, 2140a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), DestReg); 2141d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Entry PHI " << printReg(DestReg, TRI) << " = PHI("); 2142a06bfe05SJan Sjodin 2143a06bfe05SJan Sjodin unsigned CurrentBackedgeReg = 0; 2144a06bfe05SJan Sjodin 2145a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2146a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2147a06bfe05SJan Sjodin 2148a06bfe05SJan Sjodin if (CurrentRegion->contains((*SRI).second)) { 2149a06bfe05SJan Sjodin if (CurrentBackedgeReg == 0) { 2150a06bfe05SJan Sjodin CurrentBackedgeReg = SourceReg; 2151a06bfe05SJan Sjodin } else { 2152a06bfe05SJan Sjodin MachineInstr *PHIDefInstr = getDefInstr(SourceReg); 2153a06bfe05SJan Sjodin MachineBasicBlock *PHIDefMBB = PHIDefInstr->getParent(); 2154a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = 2155a06bfe05SJan Sjodin MRI->getRegClass(CurrentBackedgeReg); 21560c476111SDaniel Sanders Register NewBackedgeReg = MRI->createVirtualRegister(RegClass); 2157a06bfe05SJan Sjodin MachineInstrBuilder BackedgePHI = 2158a06bfe05SJan Sjodin BuildMI(*PHIDefMBB, PHIDefMBB->instr_begin(), DL, 2159a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), NewBackedgeReg); 2160a06bfe05SJan Sjodin BackedgePHI.addReg(CurrentBackedgeReg); 2161a06bfe05SJan Sjodin BackedgePHI.addMBB(getPHIPred(*PHIDefInstr, 0)); 2162a06bfe05SJan Sjodin BackedgePHI.addReg(getPHISourceReg(*PHIDefInstr, 1)); 2163a06bfe05SJan Sjodin BackedgePHI.addMBB((*SRI).second); 2164a06bfe05SJan Sjodin CurrentBackedgeReg = NewBackedgeReg; 2165d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() 2166d34e60caSNicola Zaghen << "Inserting backedge PHI: " 2167a8a83d15SFrancis Visoiu Mistrih << printReg(NewBackedgeReg, TRI) << " = PHI(" 216825528d6dSFrancis Visoiu Mistrih << printReg(CurrentBackedgeReg, TRI) << ", " 2169d34e60caSNicola Zaghen << printMBBReference(*getPHIPred(*PHIDefInstr, 0)) << ", " 2170d34e60caSNicola Zaghen << printReg(getPHISourceReg(*PHIDefInstr, 1), TRI) << ", " 2171d34e60caSNicola Zaghen << printMBBReference(*(*SRI).second)); 2172a06bfe05SJan Sjodin } 2173a06bfe05SJan Sjodin } else { 2174a06bfe05SJan Sjodin MIB.addReg(SourceReg); 2175a06bfe05SJan Sjodin MIB.addMBB((*SRI).second); 2176d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 217725528d6dSFrancis Visoiu Mistrih << printMBBReference(*(*SRI).second) << ", "); 2178a06bfe05SJan Sjodin } 2179a06bfe05SJan Sjodin } 2180a06bfe05SJan Sjodin 2181a06bfe05SJan Sjodin // Add the final backedge register source to the entry phi 2182a06bfe05SJan Sjodin if (CurrentBackedgeReg != 0) { 2183a06bfe05SJan Sjodin MIB.addReg(CurrentBackedgeReg); 2184a06bfe05SJan Sjodin MIB.addMBB(Exit); 2185d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CurrentBackedgeReg, TRI) << ", " 218625528d6dSFrancis Visoiu Mistrih << printMBBReference(*Exit) << ")\n"); 2187a06bfe05SJan Sjodin } else { 2188d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 2189a06bfe05SJan Sjodin } 2190a06bfe05SJan Sjodin } 2191a06bfe05SJan Sjodin } 2192a06bfe05SJan Sjodin 2193a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::createEntryPHIs(LinearizedRegion *CurrentRegion) { 2194d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2195a06bfe05SJan Sjodin 2196a06bfe05SJan Sjodin for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; 2197a06bfe05SJan Sjodin ++DRI) { 2198a06bfe05SJan Sjodin 2199a06bfe05SJan Sjodin unsigned DestReg = *DRI; 2200a06bfe05SJan Sjodin createEntryPHI(CurrentRegion, DestReg); 2201a06bfe05SJan Sjodin } 2202a06bfe05SJan Sjodin PHIInfo.clear(); 2203a06bfe05SJan Sjodin } 2204a06bfe05SJan Sjodin 220534978602SJay Foad void AMDGPUMachineCFGStructurizer::replaceRegisterWith( 220634978602SJay Foad unsigned Register, class Register NewRegister) { 2207a06bfe05SJan Sjodin assert(Register != NewRegister && "Cannot replace a reg with itself"); 2208a06bfe05SJan Sjodin 2209a06bfe05SJan Sjodin for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Register), 2210a06bfe05SJan Sjodin E = MRI->reg_end(); 2211a06bfe05SJan Sjodin I != E;) { 2212a06bfe05SJan Sjodin MachineOperand &O = *I; 2213a06bfe05SJan Sjodin ++I; 221434978602SJay Foad if (NewRegister.isPhysical()) { 2215d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Trying to substitute physical register: " 22169d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) 2217a06bfe05SJan Sjodin << "\n"); 2218a06bfe05SJan Sjodin llvm_unreachable("Cannot substitute physical registers"); 2219a06bfe05SJan Sjodin // We don't handle physical registers, but if we need to 2220a06bfe05SJan Sjodin // in the future This is how we do it: 2221a06bfe05SJan Sjodin // O.substPhysReg(NewRegister, *TRI); 2222a06bfe05SJan Sjodin } else { 2223d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replacing register: " 22249d419d3bSFrancis Visoiu Mistrih << printReg(Register, MRI->getTargetRegisterInfo()) 2225a06bfe05SJan Sjodin << " with " 22269d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) 2227a06bfe05SJan Sjodin << "\n"); 2228a06bfe05SJan Sjodin O.setReg(NewRegister); 2229a06bfe05SJan Sjodin } 2230a06bfe05SJan Sjodin } 2231a06bfe05SJan Sjodin PHIInfo.deleteDef(Register); 2232a06bfe05SJan Sjodin 2233a06bfe05SJan Sjodin getRegionMRT()->replaceLiveOutReg(Register, NewRegister); 2234a06bfe05SJan Sjodin 2235d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2236a06bfe05SJan Sjodin } 2237a06bfe05SJan Sjodin 2238a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::resolvePHIInfos(MachineBasicBlock *FunctionEntry) { 2239d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Resolve PHI Infos\n"); 2240d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2241a06bfe05SJan Sjodin for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; 2242a06bfe05SJan Sjodin ++DRI) { 2243a06bfe05SJan Sjodin unsigned DestReg = *DRI; 2244d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DestReg: " << printReg(DestReg, TRI) << "\n"); 2245a06bfe05SJan Sjodin auto SRI = PHIInfo.sources_begin(DestReg); 2246a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2247d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DestReg: " << printReg(DestReg, TRI) 22489d419d3bSFrancis Visoiu Mistrih << " SourceReg: " << printReg(SourceReg, TRI) << "\n"); 2249a06bfe05SJan Sjodin 2250a06bfe05SJan Sjodin assert(PHIInfo.sources_end(DestReg) == ++SRI && 2251a06bfe05SJan Sjodin "More than one phi source in entry node"); 2252a06bfe05SJan Sjodin replaceRegisterWith(DestReg, SourceReg); 2253a06bfe05SJan Sjodin } 2254a06bfe05SJan Sjodin } 2255a06bfe05SJan Sjodin 2256a06bfe05SJan Sjodin static bool isFunctionEntryBlock(MachineBasicBlock *MBB) { 2257a06bfe05SJan Sjodin return ((&(*(MBB->getParent()->begin()))) == MBB); 2258a06bfe05SJan Sjodin } 2259a06bfe05SJan Sjodin 2260a06bfe05SJan Sjodin MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfRegion( 2261a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, MachineBasicBlock *CodeBB, 2262a06bfe05SJan Sjodin LinearizedRegion *CurrentRegion, unsigned BBSelectRegIn, 2263a06bfe05SJan Sjodin unsigned BBSelectRegOut) { 2264a06bfe05SJan Sjodin if (isFunctionEntryBlock(CodeBB) && !CurrentRegion->getHasLoop()) { 2265a06bfe05SJan Sjodin // Handle non-loop function entry block. 2266a06bfe05SJan Sjodin // We need to allow loops to the entry block and then 2267a06bfe05SJan Sjodin rewriteCodeBBTerminator(CodeBB, MergeBB, BBSelectRegOut); 2268a06bfe05SJan Sjodin resolvePHIInfos(CodeBB); 2269a06bfe05SJan Sjodin removeExternalCFGSuccessors(CodeBB); 2270a06bfe05SJan Sjodin CodeBB->addSuccessor(MergeBB); 2271a06bfe05SJan Sjodin CurrentRegion->addMBB(CodeBB); 2272a06bfe05SJan Sjodin return nullptr; 2273a06bfe05SJan Sjodin } 2274a06bfe05SJan Sjodin if (CurrentRegion->getEntry() == CodeBB && !CurrentRegion->getHasLoop()) { 2275a06bfe05SJan Sjodin // Handle non-loop region entry block. 2276a06bfe05SJan Sjodin MachineFunction *MF = MergeBB->getParent(); 2277a06bfe05SJan Sjodin auto MergeIter = MergeBB->getIterator(); 2278a06bfe05SJan Sjodin auto CodeBBStartIter = CodeBB->getIterator(); 2279a06bfe05SJan Sjodin auto CodeBBEndIter = ++(CodeBB->getIterator()); 2280a06bfe05SJan Sjodin if (CodeBBEndIter != MergeIter) { 2281a06bfe05SJan Sjodin MF->splice(MergeIter, CodeBBStartIter, CodeBBEndIter); 2282a06bfe05SJan Sjodin } 2283a06bfe05SJan Sjodin rewriteCodeBBTerminator(CodeBB, MergeBB, BBSelectRegOut); 2284a06bfe05SJan Sjodin prunePHIInfo(CodeBB); 2285a06bfe05SJan Sjodin createEntryPHIs(CurrentRegion); 2286a06bfe05SJan Sjodin removeExternalCFGSuccessors(CodeBB); 2287a06bfe05SJan Sjodin CodeBB->addSuccessor(MergeBB); 2288a06bfe05SJan Sjodin CurrentRegion->addMBB(CodeBB); 2289a06bfe05SJan Sjodin return nullptr; 2290a06bfe05SJan Sjodin } else { 2291a06bfe05SJan Sjodin // Handle internal block. 2292a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(BBSelectRegIn); 22930c476111SDaniel Sanders Register CodeBBSelectReg = MRI->createVirtualRegister(RegClass); 2294a06bfe05SJan Sjodin rewriteCodeBBTerminator(CodeBB, MergeBB, CodeBBSelectReg); 2295a06bfe05SJan Sjodin bool IsRegionEntryBB = CurrentRegion->getEntry() == CodeBB; 2296a06bfe05SJan Sjodin MachineBasicBlock *IfBB = createIfBlock(MergeBB, CodeBB, CodeBB, CodeBB, 2297a06bfe05SJan Sjodin BBSelectRegIn, IsRegionEntryBB); 2298a06bfe05SJan Sjodin CurrentRegion->addMBB(IfBB); 2299a06bfe05SJan Sjodin // If this is the entry block we need to make the If block the new 2300a06bfe05SJan Sjodin // linearized region entry. 2301a06bfe05SJan Sjodin if (IsRegionEntryBB) { 2302a06bfe05SJan Sjodin CurrentRegion->setEntry(IfBB); 2303a06bfe05SJan Sjodin 2304a06bfe05SJan Sjodin if (CurrentRegion->getHasLoop()) { 2305a06bfe05SJan Sjodin MachineBasicBlock *RegionExit = CurrentRegion->getExit(); 2306a06bfe05SJan Sjodin MachineBasicBlock *ETrueBB = nullptr; 2307a06bfe05SJan Sjodin MachineBasicBlock *EFalseBB = nullptr; 2308a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> ECond; 2309a06bfe05SJan Sjodin 2310a06bfe05SJan Sjodin const DebugLoc &DL = DebugLoc(); 2311a06bfe05SJan Sjodin TII->analyzeBranch(*RegionExit, ETrueBB, EFalseBB, ECond); 2312a06bfe05SJan Sjodin TII->removeBranch(*RegionExit); 2313a06bfe05SJan Sjodin 2314a06bfe05SJan Sjodin // We need to create a backedge if there is a loop 231598de0d22SJay Foad Register Reg = TII->insertNE( 2316a06bfe05SJan Sjodin RegionExit, RegionExit->instr_end(), DL, 2317a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getInnerOutputRegister(), 2318a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getEntry()->getNumber()); 2319a06bfe05SJan Sjodin MachineOperand RegOp = 2320a06bfe05SJan Sjodin MachineOperand::CreateReg(Reg, false, false, true); 2321a06bfe05SJan Sjodin ArrayRef<MachineOperand> Cond(RegOp); 2322d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "RegionExitReg: "); 2323d34e60caSNicola Zaghen LLVM_DEBUG(Cond[0].print(dbgs(), TRI)); 2324d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2325a06bfe05SJan Sjodin TII->insertBranch(*RegionExit, CurrentRegion->getEntry(), RegionExit, 2326a06bfe05SJan Sjodin Cond, DebugLoc()); 2327a06bfe05SJan Sjodin RegionExit->addSuccessor(CurrentRegion->getEntry()); 2328a06bfe05SJan Sjodin } 2329a06bfe05SJan Sjodin } 2330a06bfe05SJan Sjodin CurrentRegion->addMBB(CodeBB); 2331a06bfe05SJan Sjodin LinearizedRegion InnerRegion(CodeBB, MRI, TRI, PHIInfo); 2332a06bfe05SJan Sjodin 2333a06bfe05SJan Sjodin InnerRegion.setParent(CurrentRegion); 2334d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert BB Select PHI (BB)\n"); 2335a06bfe05SJan Sjodin insertMergePHI(IfBB, CodeBB, MergeBB, BBSelectRegOut, BBSelectRegIn, 2336a06bfe05SJan Sjodin CodeBBSelectReg); 2337a06bfe05SJan Sjodin InnerRegion.addMBB(MergeBB); 2338a06bfe05SJan Sjodin 2339d34e60caSNicola Zaghen LLVM_DEBUG(InnerRegion.print(dbgs(), TRI)); 2340a06bfe05SJan Sjodin rewriteLiveOutRegs(IfBB, CodeBB, MergeBB, &InnerRegion, CurrentRegion); 2341a06bfe05SJan Sjodin extractKilledPHIs(CodeBB); 2342a06bfe05SJan Sjodin if (IsRegionEntryBB) { 2343a06bfe05SJan Sjodin createEntryPHIs(CurrentRegion); 2344a06bfe05SJan Sjodin } 2345a06bfe05SJan Sjodin return IfBB; 2346a06bfe05SJan Sjodin } 2347a06bfe05SJan Sjodin } 2348a06bfe05SJan Sjodin 2349a06bfe05SJan Sjodin MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfRegion( 2350a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, LinearizedRegion *InnerRegion, 2351a06bfe05SJan Sjodin LinearizedRegion *CurrentRegion, MachineBasicBlock *SelectBB, 2352a06bfe05SJan Sjodin unsigned BBSelectRegIn, unsigned BBSelectRegOut) { 2353a06bfe05SJan Sjodin unsigned CodeBBSelectReg = 2354a06bfe05SJan Sjodin InnerRegion->getRegionMRT()->getInnerOutputRegister(); 2355a06bfe05SJan Sjodin MachineBasicBlock *CodeEntryBB = InnerRegion->getEntry(); 2356a06bfe05SJan Sjodin MachineBasicBlock *CodeExitBB = InnerRegion->getExit(); 2357a06bfe05SJan Sjodin MachineBasicBlock *IfBB = createIfBlock(MergeBB, CodeEntryBB, CodeExitBB, 2358a06bfe05SJan Sjodin SelectBB, BBSelectRegIn, true); 2359a06bfe05SJan Sjodin CurrentRegion->addMBB(IfBB); 2360a06bfe05SJan Sjodin bool isEntry = CurrentRegion->getEntry() == InnerRegion->getEntry(); 2361a06bfe05SJan Sjodin if (isEntry) { 2362a06bfe05SJan Sjodin 2363a06bfe05SJan Sjodin if (CurrentRegion->getHasLoop()) { 2364a06bfe05SJan Sjodin MachineBasicBlock *RegionExit = CurrentRegion->getExit(); 2365a06bfe05SJan Sjodin MachineBasicBlock *ETrueBB = nullptr; 2366a06bfe05SJan Sjodin MachineBasicBlock *EFalseBB = nullptr; 2367a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> ECond; 2368a06bfe05SJan Sjodin 2369a06bfe05SJan Sjodin const DebugLoc &DL = DebugLoc(); 2370a06bfe05SJan Sjodin TII->analyzeBranch(*RegionExit, ETrueBB, EFalseBB, ECond); 2371a06bfe05SJan Sjodin TII->removeBranch(*RegionExit); 2372a06bfe05SJan Sjodin 2373a06bfe05SJan Sjodin // We need to create a backedge if there is a loop 237498de0d22SJay Foad Register Reg = 2375a06bfe05SJan Sjodin TII->insertNE(RegionExit, RegionExit->instr_end(), DL, 2376a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getInnerOutputRegister(), 2377a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getEntry()->getNumber()); 2378a06bfe05SJan Sjodin MachineOperand RegOp = MachineOperand::CreateReg(Reg, false, false, true); 2379a06bfe05SJan Sjodin ArrayRef<MachineOperand> Cond(RegOp); 2380d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "RegionExitReg: "); 2381d34e60caSNicola Zaghen LLVM_DEBUG(Cond[0].print(dbgs(), TRI)); 2382d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2383a06bfe05SJan Sjodin TII->insertBranch(*RegionExit, CurrentRegion->getEntry(), RegionExit, 2384a06bfe05SJan Sjodin Cond, DebugLoc()); 2385a06bfe05SJan Sjodin RegionExit->addSuccessor(IfBB); 2386a06bfe05SJan Sjodin } 2387a06bfe05SJan Sjodin } 2388a06bfe05SJan Sjodin CurrentRegion->addMBBs(InnerRegion); 2389d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert BB Select PHI (region)\n"); 2390a06bfe05SJan Sjodin insertMergePHI(IfBB, CodeExitBB, MergeBB, BBSelectRegOut, BBSelectRegIn, 2391a06bfe05SJan Sjodin CodeBBSelectReg); 2392a06bfe05SJan Sjodin 2393a06bfe05SJan Sjodin rewriteLiveOutRegs(IfBB, /* CodeEntryBB */ CodeExitBB, MergeBB, InnerRegion, 2394a06bfe05SJan Sjodin CurrentRegion); 2395a06bfe05SJan Sjodin 2396a06bfe05SJan Sjodin rewriteRegionEntryPHIs(InnerRegion, IfBB); 2397a06bfe05SJan Sjodin 2398a06bfe05SJan Sjodin if (isEntry) { 2399a06bfe05SJan Sjodin CurrentRegion->setEntry(IfBB); 2400a06bfe05SJan Sjodin } 2401a06bfe05SJan Sjodin 2402a06bfe05SJan Sjodin if (isEntry) { 2403a06bfe05SJan Sjodin createEntryPHIs(CurrentRegion); 2404a06bfe05SJan Sjodin } 2405a06bfe05SJan Sjodin 2406a06bfe05SJan Sjodin return IfBB; 2407a06bfe05SJan Sjodin } 2408a06bfe05SJan Sjodin 2409a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::splitLoopPHI(MachineInstr &PHI, 2410a06bfe05SJan Sjodin MachineBasicBlock *Entry, 2411a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc, 2412a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 2413a06bfe05SJan Sjodin SmallVector<unsigned, 2> PHIRegionIndices; 2414a06bfe05SJan Sjodin getPHIRegionIndices(LRegion, PHI, PHIRegionIndices); 2415a06bfe05SJan Sjodin 2416a06bfe05SJan Sjodin assert(PHIRegionIndices.size() == 1); 2417a06bfe05SJan Sjodin 2418a06bfe05SJan Sjodin unsigned RegionIndex = PHIRegionIndices[0]; 2419a06bfe05SJan Sjodin unsigned RegionSourceReg = getPHISourceReg(PHI, RegionIndex); 2420a06bfe05SJan Sjodin MachineBasicBlock *RegionSourceMBB = getPHIPred(PHI, RegionIndex); 2421a06bfe05SJan Sjodin unsigned PHIDest = getPHIDestReg(PHI); 2422a06bfe05SJan Sjodin unsigned PHISource = PHIDest; 2423a06bfe05SJan Sjodin unsigned ReplaceReg; 2424a06bfe05SJan Sjodin 2425a06bfe05SJan Sjodin if (shrinkPHI(PHI, PHIRegionIndices, &ReplaceReg)) { 2426a06bfe05SJan Sjodin PHISource = ReplaceReg; 2427a06bfe05SJan Sjodin } 2428a06bfe05SJan Sjodin 2429a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(PHIDest); 24300c476111SDaniel Sanders Register NewDestReg = MRI->createVirtualRegister(RegClass); 2431a06bfe05SJan Sjodin LRegion->replaceRegisterInsideRegion(PHIDest, NewDestReg, false, MRI); 2432a06bfe05SJan Sjodin MachineInstrBuilder MIB = 2433a06bfe05SJan Sjodin BuildMI(*EntrySucc, EntrySucc->instr_begin(), PHI.getDebugLoc(), 2434a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), NewDestReg); 2435d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Split Entry PHI " << printReg(NewDestReg, TRI) 2436d34e60caSNicola Zaghen << " = PHI("); 2437a06bfe05SJan Sjodin MIB.addReg(PHISource); 2438a06bfe05SJan Sjodin MIB.addMBB(Entry); 2439d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(PHISource, TRI) << ", " 244025528d6dSFrancis Visoiu Mistrih << printMBBReference(*Entry)); 2441a06bfe05SJan Sjodin MIB.addReg(RegionSourceReg); 2442a06bfe05SJan Sjodin MIB.addMBB(RegionSourceMBB); 2443d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " ," << printReg(RegionSourceReg, TRI) << ", " 244425528d6dSFrancis Visoiu Mistrih << printMBBReference(*RegionSourceMBB) << ")\n"); 2445a06bfe05SJan Sjodin } 2446a06bfe05SJan Sjodin 2447a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::splitLoopPHIs(MachineBasicBlock *Entry, 2448a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc, 2449a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 2450a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> PHIs; 2451a06bfe05SJan Sjodin collectPHIs(Entry, PHIs); 2452a06bfe05SJan Sjodin 2453a06bfe05SJan Sjodin for (auto PHII : PHIs) { 2454a06bfe05SJan Sjodin splitLoopPHI(*PHII, Entry, EntrySucc, LRegion); 2455a06bfe05SJan Sjodin } 2456a06bfe05SJan Sjodin } 2457a06bfe05SJan Sjodin 2458a06bfe05SJan Sjodin // Split the exit block so that we can insert a end control flow 2459a06bfe05SJan Sjodin MachineBasicBlock * 2460a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::splitExit(LinearizedRegion *LRegion) { 2461a06bfe05SJan Sjodin auto MRTRegion = LRegion->getRegionMRT(); 2462a06bfe05SJan Sjodin auto Exit = LRegion->getExit(); 2463a06bfe05SJan Sjodin auto MF = Exit->getParent(); 2464a06bfe05SJan Sjodin auto Succ = MRTRegion->getSucc(); 2465a06bfe05SJan Sjodin 2466a06bfe05SJan Sjodin auto NewExit = MF->CreateMachineBasicBlock(); 2467a06bfe05SJan Sjodin auto AfterExitIter = Exit->getIterator(); 2468a06bfe05SJan Sjodin AfterExitIter++; 2469a06bfe05SJan Sjodin MF->insert(AfterExitIter, NewExit); 2470a06bfe05SJan Sjodin Exit->removeSuccessor(Succ); 2471a06bfe05SJan Sjodin Exit->addSuccessor(NewExit); 2472a06bfe05SJan Sjodin NewExit->addSuccessor(Succ); 2473a06bfe05SJan Sjodin insertUnconditionalBranch(NewExit, Succ); 2474a06bfe05SJan Sjodin LRegion->addMBB(NewExit); 2475a06bfe05SJan Sjodin LRegion->setExit(NewExit); 2476a06bfe05SJan Sjodin 2477d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Created new exit block: " << NewExit->getNumber() 2478d34e60caSNicola Zaghen << "\n"); 2479a06bfe05SJan Sjodin 2480a06bfe05SJan Sjodin // Replace any PHI Predecessors in the successor with NewExit 2481a06bfe05SJan Sjodin for (auto &II : *Succ) { 2482a06bfe05SJan Sjodin MachineInstr &Instr = II; 2483a06bfe05SJan Sjodin 2484a06bfe05SJan Sjodin // If we are past the PHI instructions we are done 2485a06bfe05SJan Sjodin if (!Instr.isPHI()) 2486a06bfe05SJan Sjodin break; 2487a06bfe05SJan Sjodin 2488a06bfe05SJan Sjodin int numPreds = getPHINumInputs(Instr); 2489a06bfe05SJan Sjodin for (int i = 0; i < numPreds; ++i) { 2490a06bfe05SJan Sjodin auto Pred = getPHIPred(Instr, i); 2491a06bfe05SJan Sjodin if (Pred == Exit) { 2492a06bfe05SJan Sjodin setPhiPred(Instr, i, NewExit); 2493a06bfe05SJan Sjodin } 2494a06bfe05SJan Sjodin } 2495a06bfe05SJan Sjodin } 2496a06bfe05SJan Sjodin 2497a06bfe05SJan Sjodin return NewExit; 2498a06bfe05SJan Sjodin } 2499a06bfe05SJan Sjodin 2500a06bfe05SJan Sjodin static MachineBasicBlock *split(MachineBasicBlock::iterator I) { 2501a06bfe05SJan Sjodin // Create the fall-through block. 2502a06bfe05SJan Sjodin MachineBasicBlock *MBB = (*I).getParent(); 2503a06bfe05SJan Sjodin MachineFunction *MF = MBB->getParent(); 2504a06bfe05SJan Sjodin MachineBasicBlock *SuccMBB = MF->CreateMachineBasicBlock(); 2505a06bfe05SJan Sjodin auto MBBIter = ++(MBB->getIterator()); 2506a06bfe05SJan Sjodin MF->insert(MBBIter, SuccMBB); 2507a06bfe05SJan Sjodin SuccMBB->transferSuccessorsAndUpdatePHIs(MBB); 2508a06bfe05SJan Sjodin MBB->addSuccessor(SuccMBB); 2509a06bfe05SJan Sjodin 2510a06bfe05SJan Sjodin // Splice the code over. 2511a06bfe05SJan Sjodin SuccMBB->splice(SuccMBB->end(), MBB, I, MBB->end()); 2512a06bfe05SJan Sjodin 2513a06bfe05SJan Sjodin return SuccMBB; 2514a06bfe05SJan Sjodin } 2515a06bfe05SJan Sjodin 2516a06bfe05SJan Sjodin // Split the entry block separating PHI-nodes and the rest of the code 2517a06bfe05SJan Sjodin // This is needed to insert an initializer for the bb select register 2518a06bfe05SJan Sjodin // inloop regions. 2519a06bfe05SJan Sjodin 2520a06bfe05SJan Sjodin MachineBasicBlock * 2521a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::splitEntry(LinearizedRegion *LRegion) { 2522a06bfe05SJan Sjodin MachineBasicBlock *Entry = LRegion->getEntry(); 2523a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc = split(Entry->getFirstNonPHI()); 2524a06bfe05SJan Sjodin MachineBasicBlock *Exit = LRegion->getExit(); 2525a06bfe05SJan Sjodin 2526d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Split " << printMBBReference(*Entry) << " to " 252725528d6dSFrancis Visoiu Mistrih << printMBBReference(*Entry) << " -> " 252825528d6dSFrancis Visoiu Mistrih << printMBBReference(*EntrySucc) << "\n"); 2529a06bfe05SJan Sjodin LRegion->addMBB(EntrySucc); 2530a06bfe05SJan Sjodin 2531a06bfe05SJan Sjodin // Make the backedge go to Entry Succ 2532a06bfe05SJan Sjodin if (Exit->isSuccessor(Entry)) { 2533a06bfe05SJan Sjodin Exit->removeSuccessor(Entry); 2534a06bfe05SJan Sjodin } 2535a06bfe05SJan Sjodin Exit->addSuccessor(EntrySucc); 2536a06bfe05SJan Sjodin MachineInstr &Branch = *(Exit->instr_rbegin()); 2537a06bfe05SJan Sjodin for (auto &UI : Branch.uses()) { 2538a06bfe05SJan Sjodin if (UI.isMBB() && UI.getMBB() == Entry) { 2539a06bfe05SJan Sjodin UI.setMBB(EntrySucc); 2540a06bfe05SJan Sjodin } 2541a06bfe05SJan Sjodin } 2542a06bfe05SJan Sjodin 2543a06bfe05SJan Sjodin splitLoopPHIs(Entry, EntrySucc, LRegion); 2544a06bfe05SJan Sjodin 2545a06bfe05SJan Sjodin return EntrySucc; 2546a06bfe05SJan Sjodin } 2547a06bfe05SJan Sjodin 2548a06bfe05SJan Sjodin LinearizedRegion * 2549a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::initLinearizedRegion(RegionMRT *Region) { 2550a06bfe05SJan Sjodin LinearizedRegion *LRegion = Region->getLinearizedRegion(); 2551a06bfe05SJan Sjodin LRegion->initLiveOut(Region, MRI, TRI, PHIInfo); 2552a06bfe05SJan Sjodin LRegion->setEntry(Region->getEntry()); 2553a06bfe05SJan Sjodin return LRegion; 2554a06bfe05SJan Sjodin } 2555a06bfe05SJan Sjodin 2556a06bfe05SJan Sjodin static void removeOldExitPreds(RegionMRT *Region) { 2557a06bfe05SJan Sjodin MachineBasicBlock *Exit = Region->getSucc(); 2558a06bfe05SJan Sjodin if (Exit == nullptr) { 2559a06bfe05SJan Sjodin return; 2560a06bfe05SJan Sjodin } 2561a06bfe05SJan Sjodin for (MachineBasicBlock::pred_iterator PI = Exit->pred_begin(), 2562a06bfe05SJan Sjodin E = Exit->pred_end(); 2563a06bfe05SJan Sjodin PI != E; ++PI) { 2564a06bfe05SJan Sjodin if (Region->contains(*PI)) { 2565a06bfe05SJan Sjodin (*PI)->removeSuccessor(Exit); 2566a06bfe05SJan Sjodin } 2567a06bfe05SJan Sjodin } 2568a06bfe05SJan Sjodin } 2569a06bfe05SJan Sjodin 2570a06bfe05SJan Sjodin static bool mbbHasBackEdge(MachineBasicBlock *MBB, 2571a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 8> &MBBs) { 2572a06bfe05SJan Sjodin for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI) { 25738590a3e3SKazu Hirata if (MBBs.contains(*SI)) { 2574a06bfe05SJan Sjodin return true; 2575a06bfe05SJan Sjodin } 2576a06bfe05SJan Sjodin } 2577a06bfe05SJan Sjodin return false; 2578a06bfe05SJan Sjodin } 2579a06bfe05SJan Sjodin 2580a06bfe05SJan Sjodin static bool containsNewBackedge(MRT *Tree, 2581a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 8> &MBBs) { 2582a06bfe05SJan Sjodin // Need to traverse this in reverse since it is in post order. 2583a06bfe05SJan Sjodin if (Tree == nullptr) 2584a06bfe05SJan Sjodin return false; 2585a06bfe05SJan Sjodin 2586a06bfe05SJan Sjodin if (Tree->isMBB()) { 2587a06bfe05SJan Sjodin MachineBasicBlock *MBB = Tree->getMBBMRT()->getMBB(); 2588a06bfe05SJan Sjodin MBBs.insert(MBB); 2589a06bfe05SJan Sjodin if (mbbHasBackEdge(MBB, MBBs)) { 2590a06bfe05SJan Sjodin return true; 2591a06bfe05SJan Sjodin } 2592a06bfe05SJan Sjodin } else { 2593a06bfe05SJan Sjodin RegionMRT *Region = Tree->getRegionMRT(); 2594a06bfe05SJan Sjodin SetVector<MRT *> *Children = Region->getChildren(); 2595a06bfe05SJan Sjodin for (auto CI = Children->rbegin(), CE = Children->rend(); CI != CE; ++CI) { 2596a06bfe05SJan Sjodin if (containsNewBackedge(*CI, MBBs)) 2597a06bfe05SJan Sjodin return true; 2598a06bfe05SJan Sjodin } 2599a06bfe05SJan Sjodin } 2600a06bfe05SJan Sjodin return false; 2601a06bfe05SJan Sjodin } 2602a06bfe05SJan Sjodin 2603a06bfe05SJan Sjodin static bool containsNewBackedge(RegionMRT *Region) { 2604a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 8> MBBs; 2605a06bfe05SJan Sjodin return containsNewBackedge(Region, MBBs); 2606a06bfe05SJan Sjodin } 2607a06bfe05SJan Sjodin 2608a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::structurizeComplexRegion(RegionMRT *Region) { 2609a06bfe05SJan Sjodin auto *LRegion = initLinearizedRegion(Region); 2610a06bfe05SJan Sjodin LRegion->setHasLoop(containsNewBackedge(Region)); 2611a06bfe05SJan Sjodin MachineBasicBlock *LastMerge = createLinearizedExitBlock(Region); 2612a06bfe05SJan Sjodin MachineBasicBlock *CurrentMerge = LastMerge; 2613a06bfe05SJan Sjodin LRegion->addMBB(LastMerge); 2614a06bfe05SJan Sjodin LRegion->setExit(LastMerge); 2615a06bfe05SJan Sjodin 2616a06bfe05SJan Sjodin rewriteRegionExitPHIs(Region, LastMerge, LRegion); 2617a06bfe05SJan Sjodin removeOldExitPreds(Region); 2618a06bfe05SJan Sjodin 2619d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2620a06bfe05SJan Sjodin 2621a06bfe05SJan Sjodin SetVector<MRT *> *Children = Region->getChildren(); 2622d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "===========If Region Start===============\n"); 2623a06bfe05SJan Sjodin if (LRegion->getHasLoop()) { 2624d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Has Backedge: Yes\n"); 2625a06bfe05SJan Sjodin } else { 2626d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Has Backedge: No\n"); 2627a06bfe05SJan Sjodin } 2628a06bfe05SJan Sjodin 2629a06bfe05SJan Sjodin unsigned BBSelectRegIn; 2630a06bfe05SJan Sjodin unsigned BBSelectRegOut; 2631a06bfe05SJan Sjodin for (auto CI = Children->begin(), CE = Children->end(); CI != CE; ++CI) { 2632d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "CurrentRegion: \n"); 2633d34e60caSNicola Zaghen LLVM_DEBUG(LRegion->print(dbgs(), TRI)); 2634a06bfe05SJan Sjodin 2635a06bfe05SJan Sjodin auto CNI = CI; 2636a06bfe05SJan Sjodin ++CNI; 2637a06bfe05SJan Sjodin 2638a06bfe05SJan Sjodin MRT *Child = (*CI); 2639a06bfe05SJan Sjodin 2640a06bfe05SJan Sjodin if (Child->isRegion()) { 2641a06bfe05SJan Sjodin 2642a06bfe05SJan Sjodin LinearizedRegion *InnerLRegion = 2643a06bfe05SJan Sjodin Child->getRegionMRT()->getLinearizedRegion(); 2644a06bfe05SJan Sjodin // We found the block is the exit of an inner region, we need 2645a06bfe05SJan Sjodin // to put it in the current linearized region. 2646a06bfe05SJan Sjodin 2647d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Linearizing region: "); 2648d34e60caSNicola Zaghen LLVM_DEBUG(InnerLRegion->print(dbgs(), TRI)); 2649d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2650a06bfe05SJan Sjodin 2651a06bfe05SJan Sjodin MachineBasicBlock *InnerEntry = InnerLRegion->getEntry(); 2652a06bfe05SJan Sjodin if ((&(*(InnerEntry->getParent()->begin()))) == InnerEntry) { 2653a06bfe05SJan Sjodin // Entry has already been linearized, no need to do this region. 2654a06bfe05SJan Sjodin unsigned OuterSelect = InnerLRegion->getBBSelectRegOut(); 2655a06bfe05SJan Sjodin unsigned InnerSelectReg = 2656a06bfe05SJan Sjodin InnerLRegion->getRegionMRT()->getInnerOutputRegister(); 2657a06bfe05SJan Sjodin replaceRegisterWith(InnerSelectReg, OuterSelect), 2658a06bfe05SJan Sjodin resolvePHIInfos(InnerEntry); 2659a06bfe05SJan Sjodin if (!InnerLRegion->getExit()->isSuccessor(CurrentMerge)) 2660a06bfe05SJan Sjodin InnerLRegion->getExit()->addSuccessor(CurrentMerge); 2661a06bfe05SJan Sjodin continue; 2662a06bfe05SJan Sjodin } 2663a06bfe05SJan Sjodin 2664a06bfe05SJan Sjodin BBSelectRegOut = Child->getBBSelectRegOut(); 2665a06bfe05SJan Sjodin BBSelectRegIn = Child->getBBSelectRegIn(); 2666a06bfe05SJan Sjodin 2667d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegIn: " << printReg(BBSelectRegIn, TRI) 2668a06bfe05SJan Sjodin << "\n"); 2669d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegOut: " << printReg(BBSelectRegOut, TRI) 2670a06bfe05SJan Sjodin << "\n"); 2671a06bfe05SJan Sjodin 2672a06bfe05SJan Sjodin MachineBasicBlock *IfEnd = CurrentMerge; 2673a06bfe05SJan Sjodin CurrentMerge = createIfRegion(CurrentMerge, InnerLRegion, LRegion, 2674a06bfe05SJan Sjodin Child->getRegionMRT()->getEntry(), 2675a06bfe05SJan Sjodin BBSelectRegIn, BBSelectRegOut); 2676a06bfe05SJan Sjodin TII->convertNonUniformIfRegion(CurrentMerge, IfEnd); 2677a06bfe05SJan Sjodin } else { 2678a06bfe05SJan Sjodin MachineBasicBlock *MBB = Child->getMBBMRT()->getMBB(); 2679d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Linearizing block: " << MBB->getNumber() << "\n"); 2680a06bfe05SJan Sjodin 2681a06bfe05SJan Sjodin if (MBB == getSingleExitNode(*(MBB->getParent()))) { 2682a06bfe05SJan Sjodin // If this is the exit block then we need to skip to the next. 2683a06bfe05SJan Sjodin // The "in" register will be transferred to "out" in the next 2684a06bfe05SJan Sjodin // iteration. 2685a06bfe05SJan Sjodin continue; 2686a06bfe05SJan Sjodin } 2687a06bfe05SJan Sjodin 2688a06bfe05SJan Sjodin BBSelectRegOut = Child->getBBSelectRegOut(); 2689a06bfe05SJan Sjodin BBSelectRegIn = Child->getBBSelectRegIn(); 2690a06bfe05SJan Sjodin 2691d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegIn: " << printReg(BBSelectRegIn, TRI) 2692a06bfe05SJan Sjodin << "\n"); 2693d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegOut: " << printReg(BBSelectRegOut, TRI) 2694a06bfe05SJan Sjodin << "\n"); 2695a06bfe05SJan Sjodin 2696a06bfe05SJan Sjodin MachineBasicBlock *IfEnd = CurrentMerge; 2697a06bfe05SJan Sjodin // This is a basic block that is not part of an inner region, we 2698a06bfe05SJan Sjodin // need to put it in the current linearized region. 2699a06bfe05SJan Sjodin CurrentMerge = createIfRegion(CurrentMerge, MBB, LRegion, BBSelectRegIn, 2700a06bfe05SJan Sjodin BBSelectRegOut); 2701a06bfe05SJan Sjodin if (CurrentMerge) { 2702a06bfe05SJan Sjodin TII->convertNonUniformIfRegion(CurrentMerge, IfEnd); 2703a06bfe05SJan Sjodin } 2704a06bfe05SJan Sjodin 2705d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2706a06bfe05SJan Sjodin } 2707a06bfe05SJan Sjodin } 2708a06bfe05SJan Sjodin 2709a06bfe05SJan Sjodin LRegion->removeFalseRegisterKills(MRI); 2710a06bfe05SJan Sjodin 2711a06bfe05SJan Sjodin if (LRegion->getHasLoop()) { 2712a06bfe05SJan Sjodin MachineBasicBlock *NewSucc = splitEntry(LRegion); 2713a06bfe05SJan Sjodin if (isFunctionEntryBlock(LRegion->getEntry())) { 2714a06bfe05SJan Sjodin resolvePHIInfos(LRegion->getEntry()); 2715a06bfe05SJan Sjodin } 2716a06bfe05SJan Sjodin const DebugLoc &DL = NewSucc->findDebugLoc(NewSucc->getFirstNonPHI()); 2717a06bfe05SJan Sjodin unsigned InReg = LRegion->getBBSelectRegIn(); 27180c476111SDaniel Sanders Register InnerSelectReg = 2719a06bfe05SJan Sjodin MRI->createVirtualRegister(MRI->getRegClass(InReg)); 27200c476111SDaniel Sanders Register NewInReg = MRI->createVirtualRegister(MRI->getRegClass(InReg)); 2721a06bfe05SJan Sjodin TII->materializeImmediate(*(LRegion->getEntry()), 2722a06bfe05SJan Sjodin LRegion->getEntry()->getFirstTerminator(), DL, 2723a06bfe05SJan Sjodin NewInReg, Region->getEntry()->getNumber()); 2724a06bfe05SJan Sjodin // Need to be careful about updating the registers inside the region. 2725a06bfe05SJan Sjodin LRegion->replaceRegisterInsideRegion(InReg, InnerSelectReg, false, MRI); 2726d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Loop BBSelect Merge PHI:\n"); 2727a06bfe05SJan Sjodin insertMergePHI(LRegion->getEntry(), LRegion->getExit(), NewSucc, 2728a06bfe05SJan Sjodin InnerSelectReg, NewInReg, 2729a06bfe05SJan Sjodin LRegion->getRegionMRT()->getInnerOutputRegister()); 2730a06bfe05SJan Sjodin splitExit(LRegion); 2731a06bfe05SJan Sjodin TII->convertNonUniformLoopRegion(NewSucc, LastMerge); 2732a06bfe05SJan Sjodin } 2733a06bfe05SJan Sjodin 2734a06bfe05SJan Sjodin if (Region->isRoot()) { 2735a06bfe05SJan Sjodin TII->insertReturn(*LastMerge); 2736a06bfe05SJan Sjodin } 2737a06bfe05SJan Sjodin 2738d34e60caSNicola Zaghen LLVM_DEBUG(Region->getEntry()->getParent()->dump()); 2739d34e60caSNicola Zaghen LLVM_DEBUG(LRegion->print(dbgs(), TRI)); 2740d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2741a06bfe05SJan Sjodin 2742d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "===========If Region End===============\n"); 2743a06bfe05SJan Sjodin 2744a06bfe05SJan Sjodin Region->setLinearizedRegion(LRegion); 2745a06bfe05SJan Sjodin return true; 2746a06bfe05SJan Sjodin } 2747a06bfe05SJan Sjodin 2748a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::structurizeRegion(RegionMRT *Region) { 2749a06bfe05SJan Sjodin if (false && regionIsSimpleIf(Region)) { 2750a06bfe05SJan Sjodin transformSimpleIfRegion(Region); 2751a06bfe05SJan Sjodin return true; 2752a06bfe05SJan Sjodin } else if (regionIsSequence(Region)) { 2753a06bfe05SJan Sjodin fixupRegionExits(Region); 2754a06bfe05SJan Sjodin return false; 2755a06bfe05SJan Sjodin } else { 2756a06bfe05SJan Sjodin structurizeComplexRegion(Region); 2757a06bfe05SJan Sjodin } 2758a06bfe05SJan Sjodin return false; 2759a06bfe05SJan Sjodin } 2760a06bfe05SJan Sjodin 2761a06bfe05SJan Sjodin static int structurize_once = 0; 2762a06bfe05SJan Sjodin 2763a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::structurizeRegions(RegionMRT *Region, 2764a06bfe05SJan Sjodin bool isTopRegion) { 2765a06bfe05SJan Sjodin bool Changed = false; 2766a06bfe05SJan Sjodin 2767a06bfe05SJan Sjodin auto Children = Region->getChildren(); 2768a06bfe05SJan Sjodin for (auto CI : *Children) { 2769a06bfe05SJan Sjodin if (CI->isRegion()) { 2770a06bfe05SJan Sjodin Changed |= structurizeRegions(CI->getRegionMRT(), false); 2771a06bfe05SJan Sjodin } 2772a06bfe05SJan Sjodin } 2773a06bfe05SJan Sjodin 2774a06bfe05SJan Sjodin if (structurize_once < 2 || true) { 2775a06bfe05SJan Sjodin Changed |= structurizeRegion(Region); 2776a06bfe05SJan Sjodin structurize_once++; 2777a06bfe05SJan Sjodin } 2778a06bfe05SJan Sjodin return Changed; 2779a06bfe05SJan Sjodin } 2780a06bfe05SJan Sjodin 2781a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::initFallthroughMap(MachineFunction &MF) { 2782d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Fallthrough Map:\n"); 2783a06bfe05SJan Sjodin for (auto &MBBI : MF) { 2784a06bfe05SJan Sjodin MachineBasicBlock *MBB = MBBI.getFallThrough(); 2785a06bfe05SJan Sjodin if (MBB != nullptr) { 2786d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Fallthrough: " << MBBI.getNumber() << " -> " 2787a06bfe05SJan Sjodin << MBB->getNumber() << "\n"); 2788a06bfe05SJan Sjodin } 2789a06bfe05SJan Sjodin FallthroughMap[&MBBI] = MBB; 2790a06bfe05SJan Sjodin } 2791a06bfe05SJan Sjodin } 2792a06bfe05SJan Sjodin 2793a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::createLinearizedRegion(RegionMRT *Region, 2794a06bfe05SJan Sjodin unsigned SelectOut) { 2795a06bfe05SJan Sjodin LinearizedRegion *LRegion = new LinearizedRegion(); 2796a06bfe05SJan Sjodin if (SelectOut) { 2797a06bfe05SJan Sjodin LRegion->addLiveOut(SelectOut); 2798d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add LiveOut (BBSelect): " << printReg(SelectOut, TRI) 2799a06bfe05SJan Sjodin << "\n"); 2800a06bfe05SJan Sjodin } 2801a06bfe05SJan Sjodin LRegion->setRegionMRT(Region); 2802a06bfe05SJan Sjodin Region->setLinearizedRegion(LRegion); 2803a06bfe05SJan Sjodin LRegion->setParent(Region->getParent() 2804a06bfe05SJan Sjodin ? Region->getParent()->getLinearizedRegion() 2805a06bfe05SJan Sjodin : nullptr); 2806a06bfe05SJan Sjodin } 2807a06bfe05SJan Sjodin 2808a06bfe05SJan Sjodin unsigned 2809a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::initializeSelectRegisters(MRT *MRT, unsigned SelectOut, 2810a06bfe05SJan Sjodin MachineRegisterInfo *MRI, 2811a06bfe05SJan Sjodin const SIInstrInfo *TII) { 2812a06bfe05SJan Sjodin if (MRT->isRegion()) { 2813a06bfe05SJan Sjodin RegionMRT *Region = MRT->getRegionMRT(); 2814a06bfe05SJan Sjodin Region->setBBSelectRegOut(SelectOut); 2815a06bfe05SJan Sjodin unsigned InnerSelectOut = createBBSelectReg(TII, MRI); 2816a06bfe05SJan Sjodin 2817a06bfe05SJan Sjodin // Fixme: Move linearization creation to the original spot 2818a06bfe05SJan Sjodin createLinearizedRegion(Region, SelectOut); 2819a06bfe05SJan Sjodin 2820a06bfe05SJan Sjodin for (auto CI = Region->getChildren()->begin(), 2821a06bfe05SJan Sjodin CE = Region->getChildren()->end(); 2822a06bfe05SJan Sjodin CI != CE; ++CI) { 2823a06bfe05SJan Sjodin InnerSelectOut = 2824a06bfe05SJan Sjodin initializeSelectRegisters((*CI), InnerSelectOut, MRI, TII); 2825a06bfe05SJan Sjodin } 2826a06bfe05SJan Sjodin MRT->setBBSelectRegIn(InnerSelectOut); 2827a06bfe05SJan Sjodin return InnerSelectOut; 2828a06bfe05SJan Sjodin } else { 2829a06bfe05SJan Sjodin MRT->setBBSelectRegOut(SelectOut); 2830a06bfe05SJan Sjodin unsigned NewSelectIn = createBBSelectReg(TII, MRI); 2831a06bfe05SJan Sjodin MRT->setBBSelectRegIn(NewSelectIn); 2832a06bfe05SJan Sjodin return NewSelectIn; 2833a06bfe05SJan Sjodin } 2834a06bfe05SJan Sjodin } 2835a06bfe05SJan Sjodin 2836a06bfe05SJan Sjodin static void checkRegOnlyPHIInputs(MachineFunction &MF) { 2837a06bfe05SJan Sjodin for (auto &MBBI : MF) { 2838a06bfe05SJan Sjodin for (MachineBasicBlock::instr_iterator I = MBBI.instr_begin(), 2839a06bfe05SJan Sjodin E = MBBI.instr_end(); 2840a06bfe05SJan Sjodin I != E; ++I) { 2841a06bfe05SJan Sjodin MachineInstr &Instr = *I; 2842a06bfe05SJan Sjodin if (Instr.isPHI()) { 2843a06bfe05SJan Sjodin int numPreds = getPHINumInputs(Instr); 2844a06bfe05SJan Sjodin for (int i = 0; i < numPreds; ++i) { 2845a06bfe05SJan Sjodin assert(Instr.getOperand(i * 2 + 1).isReg() && 2846a06bfe05SJan Sjodin "PHI Operand not a register"); 2847a06bfe05SJan Sjodin } 2848a06bfe05SJan Sjodin } 2849a06bfe05SJan Sjodin } 2850a06bfe05SJan Sjodin } 2851a06bfe05SJan Sjodin } 2852a06bfe05SJan Sjodin 2853a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::runOnMachineFunction(MachineFunction &MF) { 28545bfbae5cSTom Stellard const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 2855a06bfe05SJan Sjodin const SIInstrInfo *TII = ST.getInstrInfo(); 2856a06bfe05SJan Sjodin TRI = ST.getRegisterInfo(); 2857a06bfe05SJan Sjodin MRI = &(MF.getRegInfo()); 2858a06bfe05SJan Sjodin initFallthroughMap(MF); 2859a06bfe05SJan Sjodin 2860a06bfe05SJan Sjodin checkRegOnlyPHIInputs(MF); 2861d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "----STRUCTURIZER START----\n"); 2862d34e60caSNicola Zaghen LLVM_DEBUG(MF.dump()); 2863a06bfe05SJan Sjodin 2864a06bfe05SJan Sjodin Regions = &(getAnalysis<MachineRegionInfoPass>().getRegionInfo()); 2865d34e60caSNicola Zaghen LLVM_DEBUG(Regions->dump()); 2866a06bfe05SJan Sjodin 2867a06bfe05SJan Sjodin RegionMRT *RTree = MRT::buildMRT(MF, Regions, TII, MRI); 2868a06bfe05SJan Sjodin setRegionMRT(RTree); 2869a06bfe05SJan Sjodin initializeSelectRegisters(RTree, 0, MRI, TII); 2870d34e60caSNicola Zaghen LLVM_DEBUG(RTree->dump(TRI)); 2871a06bfe05SJan Sjodin bool result = structurizeRegions(RTree, true); 2872a06bfe05SJan Sjodin delete RTree; 2873d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "----STRUCTURIZER END----\n"); 2874a06bfe05SJan Sjodin initFallthroughMap(MF); 2875a06bfe05SJan Sjodin return result; 2876a06bfe05SJan Sjodin } 2877a06bfe05SJan Sjodin 2878d16eff81SEugene Zelenko char AMDGPUMachineCFGStructurizerID = AMDGPUMachineCFGStructurizer::ID; 2879d16eff81SEugene Zelenko 2880d16eff81SEugene Zelenko INITIALIZE_PASS_BEGIN(AMDGPUMachineCFGStructurizer, "amdgpu-machine-cfg-structurizer", 2881d16eff81SEugene Zelenko "AMDGPU Machine CFG Structurizer", false, false) 2882d16eff81SEugene Zelenko INITIALIZE_PASS_DEPENDENCY(MachineRegionInfoPass) 2883d16eff81SEugene Zelenko INITIALIZE_PASS_END(AMDGPUMachineCFGStructurizer, "amdgpu-machine-cfg-structurizer", 2884d16eff81SEugene Zelenko "AMDGPU Machine CFG Structurizer", false, false) 2885d16eff81SEugene Zelenko 2886a06bfe05SJan Sjodin FunctionPass *llvm::createAMDGPUMachineCFGStructurizerPass() { 2887a06bfe05SJan Sjodin return new AMDGPUMachineCFGStructurizer(); 2888a06bfe05SJan Sjodin } 2889