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) { 197dc6e8dfdSJacob 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 816dc6e8dfdSJacob 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( 899dc6e8dfdSJacob 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 PHILinearize PHIInfo; 1077a06bfe05SJan Sjodin DenseMap<MachineBasicBlock *, MachineBasicBlock *> FallthroughMap; 1078d16eff81SEugene Zelenko RegionMRT *RMRT; 1079a06bfe05SJan Sjodin 1080a06bfe05SJan Sjodin void getPHIRegionIndices(RegionMRT *Region, MachineInstr &PHI, 1081a06bfe05SJan Sjodin SmallVector<unsigned, 2> &RegionIndices); 1082a06bfe05SJan Sjodin void getPHIRegionIndices(LinearizedRegion *Region, MachineInstr &PHI, 1083a06bfe05SJan Sjodin SmallVector<unsigned, 2> &RegionIndices); 1084a06bfe05SJan Sjodin void getPHINonRegionIndices(LinearizedRegion *Region, MachineInstr &PHI, 1085a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHINonRegionIndices); 1086a06bfe05SJan Sjodin 1087a06bfe05SJan Sjodin void storePHILinearizationInfoDest( 1088a06bfe05SJan Sjodin unsigned LDestReg, MachineInstr &PHI, 1089a06bfe05SJan Sjodin SmallVector<unsigned, 2> *RegionIndices = nullptr); 1090a06bfe05SJan Sjodin 1091a06bfe05SJan Sjodin unsigned storePHILinearizationInfo(MachineInstr &PHI, 1092a06bfe05SJan Sjodin SmallVector<unsigned, 2> *RegionIndices); 1093a06bfe05SJan Sjodin 1094a06bfe05SJan Sjodin void extractKilledPHIs(MachineBasicBlock *MBB); 1095a06bfe05SJan Sjodin 1096a06bfe05SJan Sjodin bool shrinkPHI(MachineInstr &PHI, SmallVector<unsigned, 2> &PHIIndices, 1097a06bfe05SJan Sjodin unsigned *ReplaceReg); 1098a06bfe05SJan Sjodin 1099a06bfe05SJan Sjodin bool shrinkPHI(MachineInstr &PHI, unsigned CombinedSourceReg, 1100a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB, 1101a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIIndices, unsigned *ReplaceReg); 1102a06bfe05SJan Sjodin 1103a06bfe05SJan Sjodin void replacePHI(MachineInstr &PHI, unsigned CombinedSourceReg, 1104a06bfe05SJan Sjodin MachineBasicBlock *LastMerge, 1105a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices); 1106a06bfe05SJan Sjodin void replaceEntryPHI(MachineInstr &PHI, unsigned CombinedSourceReg, 1107a06bfe05SJan Sjodin MachineBasicBlock *IfMBB, 1108a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices); 1109a06bfe05SJan Sjodin void replaceLiveOutRegs(MachineInstr &PHI, 1110a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices, 1111a06bfe05SJan Sjodin unsigned CombinedSourceReg, 1112a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1113a06bfe05SJan Sjodin void rewriteRegionExitPHI(RegionMRT *Region, MachineBasicBlock *LastMerge, 1114a06bfe05SJan Sjodin MachineInstr &PHI, LinearizedRegion *LRegion); 1115a06bfe05SJan Sjodin 1116a06bfe05SJan Sjodin void rewriteRegionExitPHIs(RegionMRT *Region, MachineBasicBlock *LastMerge, 1117a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1118a06bfe05SJan Sjodin void rewriteRegionEntryPHI(LinearizedRegion *Region, MachineBasicBlock *IfMBB, 1119a06bfe05SJan Sjodin MachineInstr &PHI); 1120a06bfe05SJan Sjodin void rewriteRegionEntryPHIs(LinearizedRegion *Region, 1121a06bfe05SJan Sjodin MachineBasicBlock *IfMBB); 1122a06bfe05SJan Sjodin 1123a06bfe05SJan Sjodin bool regionIsSimpleIf(RegionMRT *Region); 1124a06bfe05SJan Sjodin 1125a06bfe05SJan Sjodin void transformSimpleIfRegion(RegionMRT *Region); 1126a06bfe05SJan Sjodin 1127a06bfe05SJan Sjodin void insertUnconditionalBranch(MachineBasicBlock *MBB, 1128a06bfe05SJan Sjodin MachineBasicBlock *Dest, 1129a06bfe05SJan Sjodin const DebugLoc &DL = DebugLoc()); 1130a06bfe05SJan Sjodin 1131a06bfe05SJan Sjodin MachineBasicBlock *createLinearizedExitBlock(RegionMRT *Region); 1132a06bfe05SJan Sjodin 1133a06bfe05SJan Sjodin void insertMergePHI(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, 1134a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, unsigned DestRegister, 1135a06bfe05SJan Sjodin unsigned IfSourceRegister, unsigned CodeSourceRegister, 1136a06bfe05SJan Sjodin bool IsUndefIfSource = false); 1137a06bfe05SJan Sjodin 1138a06bfe05SJan Sjodin MachineBasicBlock *createIfBlock(MachineBasicBlock *MergeBB, 1139a06bfe05SJan Sjodin MachineBasicBlock *CodeBBStart, 1140a06bfe05SJan Sjodin MachineBasicBlock *CodeBBEnd, 1141a06bfe05SJan Sjodin MachineBasicBlock *SelectBB, unsigned IfReg, 1142a06bfe05SJan Sjodin bool InheritPreds); 1143a06bfe05SJan Sjodin 1144a06bfe05SJan Sjodin void prunePHIInfo(MachineBasicBlock *MBB); 1145a06bfe05SJan Sjodin void createEntryPHI(LinearizedRegion *CurrentRegion, unsigned DestReg); 1146a06bfe05SJan Sjodin 1147a06bfe05SJan Sjodin void createEntryPHIs(LinearizedRegion *CurrentRegion); 1148a06bfe05SJan Sjodin void resolvePHIInfos(MachineBasicBlock *FunctionEntry); 1149a06bfe05SJan Sjodin 115034978602SJay Foad void replaceRegisterWith(unsigned Register, class Register NewRegister); 1151a06bfe05SJan Sjodin 1152a06bfe05SJan Sjodin MachineBasicBlock *createIfRegion(MachineBasicBlock *MergeBB, 1153a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 1154a06bfe05SJan Sjodin LinearizedRegion *LRegion, 1155a06bfe05SJan Sjodin unsigned BBSelectRegIn, 1156a06bfe05SJan Sjodin unsigned BBSelectRegOut); 1157a06bfe05SJan Sjodin 1158a06bfe05SJan Sjodin MachineBasicBlock * 1159a06bfe05SJan Sjodin createIfRegion(MachineBasicBlock *MergeMBB, LinearizedRegion *InnerRegion, 1160a06bfe05SJan Sjodin LinearizedRegion *CurrentRegion, MachineBasicBlock *SelectBB, 1161a06bfe05SJan Sjodin unsigned BBSelectRegIn, unsigned BBSelectRegOut); 1162a06bfe05SJan Sjodin void ensureCondIsNotKilled(SmallVector<MachineOperand, 1> Cond); 1163a06bfe05SJan Sjodin 1164a06bfe05SJan Sjodin void rewriteCodeBBTerminator(MachineBasicBlock *CodeBB, 1165a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1166a06bfe05SJan Sjodin unsigned BBSelectReg); 1167a06bfe05SJan Sjodin 1168a06bfe05SJan Sjodin MachineInstr *getDefInstr(unsigned Reg); 1169a06bfe05SJan Sjodin void insertChainedPHI(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, 1170a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1171a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, unsigned DestReg, 1172a06bfe05SJan Sjodin unsigned SourceReg); 1173a06bfe05SJan Sjodin bool containsDef(MachineBasicBlock *MBB, LinearizedRegion *InnerRegion, 1174a06bfe05SJan Sjodin unsigned Register); 1175a06bfe05SJan Sjodin void rewriteLiveOutRegs(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, 1176a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1177a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 1178a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1179a06bfe05SJan Sjodin 1180a06bfe05SJan Sjodin void splitLoopPHI(MachineInstr &PHI, MachineBasicBlock *Entry, 1181a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc, LinearizedRegion *LRegion); 1182a06bfe05SJan Sjodin void splitLoopPHIs(MachineBasicBlock *Entry, MachineBasicBlock *EntrySucc, 1183a06bfe05SJan Sjodin LinearizedRegion *LRegion); 1184a06bfe05SJan Sjodin 1185a06bfe05SJan Sjodin MachineBasicBlock *splitExit(LinearizedRegion *LRegion); 1186a06bfe05SJan Sjodin 1187a06bfe05SJan Sjodin MachineBasicBlock *splitEntry(LinearizedRegion *LRegion); 1188a06bfe05SJan Sjodin 1189a06bfe05SJan Sjodin LinearizedRegion *initLinearizedRegion(RegionMRT *Region); 1190a06bfe05SJan Sjodin 1191a06bfe05SJan Sjodin bool structurizeComplexRegion(RegionMRT *Region); 1192a06bfe05SJan Sjodin 1193a06bfe05SJan Sjodin bool structurizeRegion(RegionMRT *Region); 1194a06bfe05SJan Sjodin 1195a06bfe05SJan Sjodin bool structurizeRegions(RegionMRT *Region, bool isTopRegion); 1196a06bfe05SJan Sjodin 1197a06bfe05SJan Sjodin public: 1198a06bfe05SJan Sjodin static char ID; 1199a06bfe05SJan Sjodin 1200d16eff81SEugene Zelenko AMDGPUMachineCFGStructurizer() : MachineFunctionPass(ID) { 1201d16eff81SEugene Zelenko initializeAMDGPUMachineCFGStructurizerPass(*PassRegistry::getPassRegistry()); 1202d16eff81SEugene Zelenko } 1203d16eff81SEugene Zelenko 1204a06bfe05SJan Sjodin void getAnalysisUsage(AnalysisUsage &AU) const override { 1205a06bfe05SJan Sjodin AU.addRequired<MachineRegionInfoPass>(); 1206a06bfe05SJan Sjodin MachineFunctionPass::getAnalysisUsage(AU); 1207a06bfe05SJan Sjodin } 1208a06bfe05SJan Sjodin 1209a06bfe05SJan Sjodin void initFallthroughMap(MachineFunction &MF); 1210a06bfe05SJan Sjodin 1211a06bfe05SJan Sjodin void createLinearizedRegion(RegionMRT *Region, unsigned SelectOut); 1212a06bfe05SJan Sjodin 1213a06bfe05SJan Sjodin unsigned initializeSelectRegisters(MRT *MRT, unsigned ExistingExitReg, 1214a06bfe05SJan Sjodin MachineRegisterInfo *MRI, 1215a06bfe05SJan Sjodin const SIInstrInfo *TII); 1216a06bfe05SJan Sjodin 1217a06bfe05SJan Sjodin void setRegionMRT(RegionMRT *RegionTree) { RMRT = RegionTree; } 1218a06bfe05SJan Sjodin 1219a06bfe05SJan Sjodin RegionMRT *getRegionMRT() { return RMRT; } 1220a06bfe05SJan Sjodin 1221a06bfe05SJan Sjodin bool runOnMachineFunction(MachineFunction &MF) override; 1222a06bfe05SJan Sjodin }; 1223d16eff81SEugene Zelenko 1224d16eff81SEugene Zelenko } // end anonymous namespace 1225a06bfe05SJan Sjodin 1226a06bfe05SJan Sjodin char AMDGPUMachineCFGStructurizer::ID = 0; 1227a06bfe05SJan Sjodin 1228a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::regionIsSimpleIf(RegionMRT *Region) { 1229a06bfe05SJan Sjodin MachineBasicBlock *Entry = Region->getEntry(); 1230a06bfe05SJan Sjodin MachineBasicBlock *Succ = Region->getSucc(); 1231a06bfe05SJan Sjodin bool FoundBypass = false; 1232a06bfe05SJan Sjodin bool FoundIf = false; 1233a06bfe05SJan Sjodin 1234a06bfe05SJan Sjodin if (Entry->succ_size() != 2) { 1235a06bfe05SJan Sjodin return false; 1236a06bfe05SJan Sjodin } 1237a06bfe05SJan Sjodin 1238a06bfe05SJan Sjodin for (MachineBasicBlock::const_succ_iterator SI = Entry->succ_begin(), 1239a06bfe05SJan Sjodin E = Entry->succ_end(); 1240a06bfe05SJan Sjodin SI != E; ++SI) { 1241a06bfe05SJan Sjodin MachineBasicBlock *Current = *SI; 1242a06bfe05SJan Sjodin 1243a06bfe05SJan Sjodin if (Current == Succ) { 1244a06bfe05SJan Sjodin FoundBypass = true; 1245a06bfe05SJan Sjodin } else if ((Current->succ_size() == 1) && 1246a06bfe05SJan Sjodin *(Current->succ_begin()) == Succ) { 1247a06bfe05SJan Sjodin FoundIf = true; 1248a06bfe05SJan Sjodin } 1249a06bfe05SJan Sjodin } 1250a06bfe05SJan Sjodin 1251a06bfe05SJan Sjodin return FoundIf && FoundBypass; 1252a06bfe05SJan Sjodin } 1253a06bfe05SJan Sjodin 1254a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::transformSimpleIfRegion(RegionMRT *Region) { 1255a06bfe05SJan Sjodin MachineBasicBlock *Entry = Region->getEntry(); 1256a06bfe05SJan Sjodin MachineBasicBlock *Exit = Region->getExit(); 1257a06bfe05SJan Sjodin TII->convertNonUniformIfRegion(Entry, Exit); 1258a06bfe05SJan Sjodin } 1259a06bfe05SJan Sjodin 1260a06bfe05SJan Sjodin static void fixMBBTerminator(MachineBasicBlock *MBB) { 1261a06bfe05SJan Sjodin if (MBB->succ_size() == 1) { 1262a06bfe05SJan Sjodin auto *Succ = *(MBB->succ_begin()); 1263a06bfe05SJan Sjodin for (auto &TI : MBB->terminators()) { 1264a06bfe05SJan Sjodin for (auto &UI : TI.uses()) { 1265a06bfe05SJan Sjodin if (UI.isMBB() && UI.getMBB() != Succ) { 1266a06bfe05SJan Sjodin UI.setMBB(Succ); 1267a06bfe05SJan Sjodin } 1268a06bfe05SJan Sjodin } 1269a06bfe05SJan Sjodin } 1270a06bfe05SJan Sjodin } 1271a06bfe05SJan Sjodin } 1272a06bfe05SJan Sjodin 1273a06bfe05SJan Sjodin static void fixRegionTerminator(RegionMRT *Region) { 1274a06bfe05SJan Sjodin MachineBasicBlock *InternalSucc = nullptr; 1275a06bfe05SJan Sjodin MachineBasicBlock *ExternalSucc = nullptr; 1276a06bfe05SJan Sjodin LinearizedRegion *LRegion = Region->getLinearizedRegion(); 1277a06bfe05SJan Sjodin auto Exit = LRegion->getExit(); 1278a06bfe05SJan Sjodin 1279a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 2> Successors; 1280a06bfe05SJan Sjodin for (MachineBasicBlock::const_succ_iterator SI = Exit->succ_begin(), 1281a06bfe05SJan Sjodin SE = Exit->succ_end(); 1282a06bfe05SJan Sjodin SI != SE; ++SI) { 1283a06bfe05SJan Sjodin MachineBasicBlock *Succ = *SI; 1284a06bfe05SJan Sjodin if (LRegion->contains(Succ)) { 1285a06bfe05SJan Sjodin // Do not allow re-assign 1286a06bfe05SJan Sjodin assert(InternalSucc == nullptr); 1287a06bfe05SJan Sjodin InternalSucc = Succ; 1288a06bfe05SJan Sjodin } else { 1289a06bfe05SJan Sjodin // Do not allow re-assign 1290a06bfe05SJan Sjodin assert(ExternalSucc == nullptr); 1291a06bfe05SJan Sjodin ExternalSucc = Succ; 1292a06bfe05SJan Sjodin } 1293a06bfe05SJan Sjodin } 1294a06bfe05SJan Sjodin 1295a06bfe05SJan Sjodin for (auto &TI : Exit->terminators()) { 1296a06bfe05SJan Sjodin for (auto &UI : TI.uses()) { 1297a06bfe05SJan Sjodin if (UI.isMBB()) { 1298a06bfe05SJan Sjodin auto Target = UI.getMBB(); 1299a06bfe05SJan Sjodin if (Target != InternalSucc && Target != ExternalSucc) { 1300a06bfe05SJan Sjodin UI.setMBB(ExternalSucc); 1301a06bfe05SJan Sjodin } 1302a06bfe05SJan Sjodin } 1303a06bfe05SJan Sjodin } 1304a06bfe05SJan Sjodin } 1305a06bfe05SJan Sjodin } 1306a06bfe05SJan Sjodin 1307a06bfe05SJan Sjodin // If a region region is just a sequence of regions (and the exit 1308a06bfe05SJan Sjodin // block in the case of the top level region), we can simply skip 1309a06bfe05SJan Sjodin // linearizing it, because it is already linear 1310a06bfe05SJan Sjodin bool regionIsSequence(RegionMRT *Region) { 1311a06bfe05SJan Sjodin auto Children = Region->getChildren(); 1312a06bfe05SJan Sjodin for (auto CI : *Children) { 1313a06bfe05SJan Sjodin if (!CI->isRegion()) { 1314a06bfe05SJan Sjodin if (CI->getMBBMRT()->getMBB()->succ_size() > 1) { 1315a06bfe05SJan Sjodin return false; 1316a06bfe05SJan Sjodin } 1317a06bfe05SJan Sjodin } 1318a06bfe05SJan Sjodin } 1319a06bfe05SJan Sjodin return true; 1320a06bfe05SJan Sjodin } 1321a06bfe05SJan Sjodin 1322a06bfe05SJan Sjodin void fixupRegionExits(RegionMRT *Region) { 1323a06bfe05SJan Sjodin auto Children = Region->getChildren(); 1324a06bfe05SJan Sjodin for (auto CI : *Children) { 1325a06bfe05SJan Sjodin if (!CI->isRegion()) { 1326a06bfe05SJan Sjodin fixMBBTerminator(CI->getMBBMRT()->getMBB()); 1327a06bfe05SJan Sjodin } else { 1328a06bfe05SJan Sjodin fixRegionTerminator(CI->getRegionMRT()); 1329a06bfe05SJan Sjodin } 1330a06bfe05SJan Sjodin } 1331a06bfe05SJan Sjodin } 1332a06bfe05SJan Sjodin 1333a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::getPHIRegionIndices( 1334a06bfe05SJan Sjodin RegionMRT *Region, MachineInstr &PHI, 1335a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1336a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1337a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1338a06bfe05SJan Sjodin MachineBasicBlock *Pred = getPHIPred(PHI, i); 1339a06bfe05SJan Sjodin if (Region->contains(Pred)) { 1340a06bfe05SJan Sjodin PHIRegionIndices.push_back(i); 1341a06bfe05SJan Sjodin } 1342a06bfe05SJan Sjodin } 1343a06bfe05SJan Sjodin } 1344a06bfe05SJan Sjodin 1345a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::getPHIRegionIndices( 1346a06bfe05SJan Sjodin LinearizedRegion *Region, MachineInstr &PHI, 1347a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1348a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1349a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1350a06bfe05SJan Sjodin MachineBasicBlock *Pred = getPHIPred(PHI, i); 1351a06bfe05SJan Sjodin if (Region->contains(Pred)) { 1352a06bfe05SJan Sjodin PHIRegionIndices.push_back(i); 1353a06bfe05SJan Sjodin } 1354a06bfe05SJan Sjodin } 1355a06bfe05SJan Sjodin } 1356a06bfe05SJan Sjodin 1357a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::getPHINonRegionIndices( 1358a06bfe05SJan Sjodin LinearizedRegion *Region, MachineInstr &PHI, 1359a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHINonRegionIndices) { 1360a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1361a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1362a06bfe05SJan Sjodin MachineBasicBlock *Pred = getPHIPred(PHI, i); 1363a06bfe05SJan Sjodin if (!Region->contains(Pred)) { 1364a06bfe05SJan Sjodin PHINonRegionIndices.push_back(i); 1365a06bfe05SJan Sjodin } 1366a06bfe05SJan Sjodin } 1367a06bfe05SJan Sjodin } 1368a06bfe05SJan Sjodin 1369a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::storePHILinearizationInfoDest( 1370a06bfe05SJan Sjodin unsigned LDestReg, MachineInstr &PHI, 1371a06bfe05SJan Sjodin SmallVector<unsigned, 2> *RegionIndices) { 1372a06bfe05SJan Sjodin if (RegionIndices) { 1373a06bfe05SJan Sjodin for (auto i : *RegionIndices) { 1374a06bfe05SJan Sjodin PHIInfo.addSource(LDestReg, getPHISourceReg(PHI, i), getPHIPred(PHI, i)); 1375a06bfe05SJan Sjodin } 1376a06bfe05SJan Sjodin } else { 1377a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1378a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1379a06bfe05SJan Sjodin PHIInfo.addSource(LDestReg, getPHISourceReg(PHI, i), getPHIPred(PHI, i)); 1380a06bfe05SJan Sjodin } 1381a06bfe05SJan Sjodin } 1382a06bfe05SJan Sjodin } 1383a06bfe05SJan Sjodin 1384a06bfe05SJan Sjodin unsigned AMDGPUMachineCFGStructurizer::storePHILinearizationInfo( 1385a06bfe05SJan Sjodin MachineInstr &PHI, SmallVector<unsigned, 2> *RegionIndices) { 1386a06bfe05SJan Sjodin unsigned DestReg = getPHIDestReg(PHI); 13870c476111SDaniel Sanders Register LinearizeDestReg = 1388a06bfe05SJan Sjodin MRI->createVirtualRegister(MRI->getRegClass(DestReg)); 1389a06bfe05SJan Sjodin PHIInfo.addDest(LinearizeDestReg, PHI.getDebugLoc()); 1390a06bfe05SJan Sjodin storePHILinearizationInfoDest(LinearizeDestReg, PHI, RegionIndices); 1391a06bfe05SJan Sjodin return LinearizeDestReg; 1392a06bfe05SJan Sjodin } 1393a06bfe05SJan Sjodin 1394a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::extractKilledPHIs(MachineBasicBlock *MBB) { 1395a06bfe05SJan Sjodin // We need to create a new chain for the killed phi, but there is no 1396a06bfe05SJan Sjodin // need to do the renaming outside or inside the block. 1397a06bfe05SJan Sjodin SmallPtrSet<MachineInstr *, 2> PHIs; 1398a06bfe05SJan Sjodin for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(), 1399a06bfe05SJan Sjodin E = MBB->instr_end(); 1400a06bfe05SJan Sjodin I != E; ++I) { 1401a06bfe05SJan Sjodin MachineInstr &Instr = *I; 1402a06bfe05SJan Sjodin if (Instr.isPHI()) { 1403a06bfe05SJan Sjodin unsigned PHIDestReg = getPHIDestReg(Instr); 1404dc6e8dfdSJacob Lambert LLVM_DEBUG(dbgs() << "Extracting killed phi:\n"); 1405d34e60caSNicola Zaghen LLVM_DEBUG(Instr.dump()); 1406a06bfe05SJan Sjodin PHIs.insert(&Instr); 1407a06bfe05SJan Sjodin PHIInfo.addDest(PHIDestReg, Instr.getDebugLoc()); 1408a06bfe05SJan Sjodin storePHILinearizationInfoDest(PHIDestReg, Instr); 1409a06bfe05SJan Sjodin } 1410a06bfe05SJan Sjodin } 1411a06bfe05SJan Sjodin 1412a06bfe05SJan Sjodin for (auto PI : PHIs) { 1413a06bfe05SJan Sjodin PI->eraseFromParent(); 1414a06bfe05SJan Sjodin } 1415a06bfe05SJan Sjodin } 1416a06bfe05SJan Sjodin 1417a06bfe05SJan Sjodin static bool isPHIRegionIndex(SmallVector<unsigned, 2> PHIRegionIndices, 1418a06bfe05SJan Sjodin unsigned Index) { 1419b4c0d610SKazu Hirata return llvm::is_contained(PHIRegionIndices, Index); 1420a06bfe05SJan Sjodin } 1421a06bfe05SJan Sjodin 1422a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::shrinkPHI(MachineInstr &PHI, 1423a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIIndices, 1424a06bfe05SJan Sjodin unsigned *ReplaceReg) { 1425a06bfe05SJan Sjodin return shrinkPHI(PHI, 0, nullptr, PHIIndices, ReplaceReg); 1426a06bfe05SJan Sjodin } 1427a06bfe05SJan Sjodin 1428a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::shrinkPHI(MachineInstr &PHI, 1429a06bfe05SJan Sjodin unsigned CombinedSourceReg, 1430a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB, 1431a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIIndices, 1432a06bfe05SJan Sjodin unsigned *ReplaceReg) { 1433d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Shrink PHI: "); 1434d34e60caSNicola Zaghen LLVM_DEBUG(PHI.dump()); 1435d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " to " << printReg(getPHIDestReg(PHI), TRI) 1436d34e60caSNicola Zaghen << " = PHI("); 1437a06bfe05SJan Sjodin 1438a06bfe05SJan Sjodin bool Replaced = false; 1439a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1440a06bfe05SJan Sjodin int SingleExternalEntryIndex = -1; 1441a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1442a06bfe05SJan Sjodin if (!isPHIRegionIndex(PHIIndices, i)) { 1443a06bfe05SJan Sjodin if (SingleExternalEntryIndex == -1) { 1444a06bfe05SJan Sjodin // Single entry 1445a06bfe05SJan Sjodin SingleExternalEntryIndex = i; 1446a06bfe05SJan Sjodin } else { 1447a06bfe05SJan Sjodin // Multiple entries 1448a06bfe05SJan Sjodin SingleExternalEntryIndex = -2; 1449a06bfe05SJan Sjodin } 1450a06bfe05SJan Sjodin } 1451a06bfe05SJan Sjodin } 1452a06bfe05SJan Sjodin 1453a06bfe05SJan Sjodin if (SingleExternalEntryIndex > -1) { 1454a06bfe05SJan Sjodin *ReplaceReg = getPHISourceReg(PHI, SingleExternalEntryIndex); 1455a06bfe05SJan Sjodin // We should not rewrite the code, we should only pick up the single value 1456a06bfe05SJan Sjodin // that represents the shrunk PHI. 1457a06bfe05SJan Sjodin Replaced = true; 1458a06bfe05SJan Sjodin } else { 1459a06bfe05SJan Sjodin MachineBasicBlock *MBB = PHI.getParent(); 1460a06bfe05SJan Sjodin MachineInstrBuilder MIB = 1461a06bfe05SJan Sjodin BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), 1462a06bfe05SJan Sjodin getPHIDestReg(PHI)); 1463a06bfe05SJan Sjodin if (SourceMBB) { 1464a06bfe05SJan Sjodin MIB.addReg(CombinedSourceReg); 1465a06bfe05SJan Sjodin MIB.addMBB(SourceMBB); 1466d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", " 146725528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourceMBB)); 1468a06bfe05SJan Sjodin } 1469a06bfe05SJan Sjodin 1470a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1471a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIIndices, i)) { 1472a06bfe05SJan Sjodin continue; 1473a06bfe05SJan Sjodin } 1474a06bfe05SJan Sjodin unsigned SourceReg = getPHISourceReg(PHI, i); 1475a06bfe05SJan Sjodin MachineBasicBlock *SourcePred = getPHIPred(PHI, i); 1476a06bfe05SJan Sjodin MIB.addReg(SourceReg); 1477a06bfe05SJan Sjodin MIB.addMBB(SourcePred); 1478d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 147925528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourcePred)); 1480a06bfe05SJan Sjodin } 1481d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 1482a06bfe05SJan Sjodin } 1483a06bfe05SJan Sjodin PHI.eraseFromParent(); 1484a06bfe05SJan Sjodin return Replaced; 1485a06bfe05SJan Sjodin } 1486a06bfe05SJan Sjodin 1487a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::replacePHI( 1488a06bfe05SJan Sjodin MachineInstr &PHI, unsigned CombinedSourceReg, MachineBasicBlock *LastMerge, 1489a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1490d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replace PHI: "); 1491d34e60caSNicola Zaghen LLVM_DEBUG(PHI.dump()); 1492d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " with " << printReg(getPHIDestReg(PHI), TRI) 1493d34e60caSNicola Zaghen << " = PHI("); 1494a06bfe05SJan Sjodin 1495a06bfe05SJan Sjodin bool HasExternalEdge = false; 1496a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1497a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1498a06bfe05SJan Sjodin if (!isPHIRegionIndex(PHIRegionIndices, i)) { 1499a06bfe05SJan Sjodin HasExternalEdge = true; 1500a06bfe05SJan Sjodin } 1501a06bfe05SJan Sjodin } 1502a06bfe05SJan Sjodin 1503a06bfe05SJan Sjodin if (HasExternalEdge) { 1504a06bfe05SJan Sjodin MachineBasicBlock *MBB = PHI.getParent(); 1505a06bfe05SJan Sjodin MachineInstrBuilder MIB = 1506a06bfe05SJan Sjodin BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), 1507a06bfe05SJan Sjodin getPHIDestReg(PHI)); 1508a06bfe05SJan Sjodin MIB.addReg(CombinedSourceReg); 1509a06bfe05SJan Sjodin MIB.addMBB(LastMerge); 1510d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", " 151125528d6dSFrancis Visoiu Mistrih << printMBBReference(*LastMerge)); 1512a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1513a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIRegionIndices, i)) { 1514a06bfe05SJan Sjodin continue; 1515a06bfe05SJan Sjodin } 1516a06bfe05SJan Sjodin unsigned SourceReg = getPHISourceReg(PHI, i); 1517a06bfe05SJan Sjodin MachineBasicBlock *SourcePred = getPHIPred(PHI, i); 1518a06bfe05SJan Sjodin MIB.addReg(SourceReg); 1519a06bfe05SJan Sjodin MIB.addMBB(SourcePred); 1520d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 152125528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourcePred)); 1522a06bfe05SJan Sjodin } 1523d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 1524a06bfe05SJan Sjodin } else { 1525a06bfe05SJan Sjodin replaceRegisterWith(getPHIDestReg(PHI), CombinedSourceReg); 1526a06bfe05SJan Sjodin } 1527a06bfe05SJan Sjodin PHI.eraseFromParent(); 1528a06bfe05SJan Sjodin } 1529a06bfe05SJan Sjodin 1530a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::replaceEntryPHI( 1531a06bfe05SJan Sjodin MachineInstr &PHI, unsigned CombinedSourceReg, MachineBasicBlock *IfMBB, 1532a06bfe05SJan Sjodin SmallVector<unsigned, 2> &PHIRegionIndices) { 1533d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replace entry PHI: "); 1534d34e60caSNicola Zaghen LLVM_DEBUG(PHI.dump()); 1535d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " with "); 1536a06bfe05SJan Sjodin 1537a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1538a06bfe05SJan Sjodin unsigned NumNonRegionInputs = NumInputs; 1539a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1540a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIRegionIndices, i)) { 1541a06bfe05SJan Sjodin NumNonRegionInputs--; 1542a06bfe05SJan Sjodin } 1543a06bfe05SJan Sjodin } 1544a06bfe05SJan Sjodin 1545a06bfe05SJan Sjodin if (NumNonRegionInputs == 0) { 1546a06bfe05SJan Sjodin auto DestReg = getPHIDestReg(PHI); 1547a06bfe05SJan Sjodin replaceRegisterWith(DestReg, CombinedSourceReg); 1548d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " register " << printReg(CombinedSourceReg, TRI) 1549d34e60caSNicola Zaghen << "\n"); 1550a06bfe05SJan Sjodin PHI.eraseFromParent(); 1551a06bfe05SJan Sjodin } else { 1552d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(getPHIDestReg(PHI), TRI) << " = PHI("); 1553a06bfe05SJan Sjodin MachineBasicBlock *MBB = PHI.getParent(); 1554a06bfe05SJan Sjodin MachineInstrBuilder MIB = 1555a06bfe05SJan Sjodin BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), 1556a06bfe05SJan Sjodin getPHIDestReg(PHI)); 1557a06bfe05SJan Sjodin MIB.addReg(CombinedSourceReg); 1558a06bfe05SJan Sjodin MIB.addMBB(IfMBB); 1559d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", " 156025528d6dSFrancis Visoiu Mistrih << printMBBReference(*IfMBB)); 1561a06bfe05SJan Sjodin unsigned NumInputs = getPHINumInputs(PHI); 1562a06bfe05SJan Sjodin for (unsigned i = 0; i < NumInputs; ++i) { 1563a06bfe05SJan Sjodin if (isPHIRegionIndex(PHIRegionIndices, i)) { 1564a06bfe05SJan Sjodin continue; 1565a06bfe05SJan Sjodin } 1566a06bfe05SJan Sjodin unsigned SourceReg = getPHISourceReg(PHI, i); 1567a06bfe05SJan Sjodin MachineBasicBlock *SourcePred = getPHIPred(PHI, i); 1568a06bfe05SJan Sjodin MIB.addReg(SourceReg); 1569a06bfe05SJan Sjodin MIB.addMBB(SourcePred); 1570d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 157125528d6dSFrancis Visoiu Mistrih << printMBBReference(*SourcePred)); 1572a06bfe05SJan Sjodin } 1573d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 1574a06bfe05SJan Sjodin PHI.eraseFromParent(); 1575a06bfe05SJan Sjodin } 1576a06bfe05SJan Sjodin } 1577a06bfe05SJan Sjodin 1578a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::replaceLiveOutRegs( 1579a06bfe05SJan Sjodin MachineInstr &PHI, SmallVector<unsigned, 2> &PHIRegionIndices, 1580a06bfe05SJan Sjodin unsigned CombinedSourceReg, LinearizedRegion *LRegion) { 1581a06bfe05SJan Sjodin bool WasLiveOut = false; 1582a06bfe05SJan Sjodin for (auto PII : PHIRegionIndices) { 1583a06bfe05SJan Sjodin unsigned Reg = getPHISourceReg(PHI, PII); 1584a06bfe05SJan Sjodin if (LRegion->isLiveOut(Reg)) { 1585a06bfe05SJan Sjodin bool IsDead = true; 1586a06bfe05SJan Sjodin 1587a06bfe05SJan Sjodin // Check if register is live out of the basic block 1588a06bfe05SJan Sjodin MachineBasicBlock *DefMBB = getDefInstr(Reg)->getParent(); 1589a06bfe05SJan Sjodin for (auto UI = MRI->use_begin(Reg), E = MRI->use_end(); UI != E; ++UI) { 1590a06bfe05SJan Sjodin if ((*UI).getParent()->getParent() != DefMBB) { 1591a06bfe05SJan Sjodin IsDead = false; 1592a06bfe05SJan Sjodin } 1593a06bfe05SJan Sjodin } 1594a06bfe05SJan Sjodin 1595d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " << printReg(Reg, TRI) << " is " 1596d34e60caSNicola Zaghen << (IsDead ? "dead" : "alive") 1597d34e60caSNicola Zaghen << " after PHI replace\n"); 1598a06bfe05SJan Sjodin if (IsDead) { 1599a06bfe05SJan Sjodin LRegion->removeLiveOut(Reg); 1600a06bfe05SJan Sjodin } 1601a06bfe05SJan Sjodin WasLiveOut = true; 1602a06bfe05SJan Sjodin } 1603a06bfe05SJan Sjodin } 1604a06bfe05SJan Sjodin 1605a06bfe05SJan Sjodin if (WasLiveOut) 1606a06bfe05SJan Sjodin LRegion->addLiveOut(CombinedSourceReg); 1607a06bfe05SJan Sjodin } 1608a06bfe05SJan Sjodin 1609a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionExitPHI(RegionMRT *Region, 1610a06bfe05SJan Sjodin MachineBasicBlock *LastMerge, 1611a06bfe05SJan Sjodin MachineInstr &PHI, 1612a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 1613a06bfe05SJan Sjodin SmallVector<unsigned, 2> PHIRegionIndices; 1614a06bfe05SJan Sjodin getPHIRegionIndices(Region, PHI, PHIRegionIndices); 1615a06bfe05SJan Sjodin unsigned LinearizedSourceReg = 1616a06bfe05SJan Sjodin storePHILinearizationInfo(PHI, &PHIRegionIndices); 1617a06bfe05SJan Sjodin 1618a06bfe05SJan Sjodin replacePHI(PHI, LinearizedSourceReg, LastMerge, PHIRegionIndices); 1619a06bfe05SJan Sjodin replaceLiveOutRegs(PHI, PHIRegionIndices, LinearizedSourceReg, LRegion); 1620a06bfe05SJan Sjodin } 1621a06bfe05SJan Sjodin 1622a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionEntryPHI(LinearizedRegion *Region, 1623a06bfe05SJan Sjodin MachineBasicBlock *IfMBB, 1624a06bfe05SJan Sjodin MachineInstr &PHI) { 1625a06bfe05SJan Sjodin SmallVector<unsigned, 2> PHINonRegionIndices; 1626a06bfe05SJan Sjodin getPHINonRegionIndices(Region, PHI, PHINonRegionIndices); 1627a06bfe05SJan Sjodin unsigned LinearizedSourceReg = 1628a06bfe05SJan Sjodin storePHILinearizationInfo(PHI, &PHINonRegionIndices); 1629a06bfe05SJan Sjodin replaceEntryPHI(PHI, LinearizedSourceReg, IfMBB, PHINonRegionIndices); 1630a06bfe05SJan Sjodin } 1631a06bfe05SJan Sjodin 1632a06bfe05SJan Sjodin static void collectPHIs(MachineBasicBlock *MBB, 1633a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> &PHIs) { 1634a06bfe05SJan Sjodin for (auto &BBI : *MBB) { 1635a06bfe05SJan Sjodin if (BBI.isPHI()) { 1636a06bfe05SJan Sjodin PHIs.push_back(&BBI); 1637a06bfe05SJan Sjodin } 1638a06bfe05SJan Sjodin } 1639a06bfe05SJan Sjodin } 1640a06bfe05SJan Sjodin 1641a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionExitPHIs(RegionMRT *Region, 1642a06bfe05SJan Sjodin MachineBasicBlock *LastMerge, 1643a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 1644a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> PHIs; 1645a06bfe05SJan Sjodin auto Exit = Region->getSucc(); 1646a06bfe05SJan Sjodin if (Exit == nullptr) 1647a06bfe05SJan Sjodin return; 1648a06bfe05SJan Sjodin 1649a06bfe05SJan Sjodin collectPHIs(Exit, PHIs); 1650a06bfe05SJan Sjodin 1651a06bfe05SJan Sjodin for (auto PHII : PHIs) { 1652a06bfe05SJan Sjodin rewriteRegionExitPHI(Region, LastMerge, *PHII, LRegion); 1653a06bfe05SJan Sjodin } 1654a06bfe05SJan Sjodin } 1655a06bfe05SJan Sjodin 1656a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteRegionEntryPHIs(LinearizedRegion *Region, 1657a06bfe05SJan Sjodin MachineBasicBlock *IfMBB) { 1658a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> PHIs; 1659a06bfe05SJan Sjodin auto Entry = Region->getEntry(); 1660a06bfe05SJan Sjodin 1661a06bfe05SJan Sjodin collectPHIs(Entry, PHIs); 1662a06bfe05SJan Sjodin 1663a06bfe05SJan Sjodin for (auto PHII : PHIs) { 1664a06bfe05SJan Sjodin rewriteRegionEntryPHI(Region, IfMBB, *PHII); 1665a06bfe05SJan Sjodin } 1666a06bfe05SJan Sjodin } 1667a06bfe05SJan Sjodin 1668a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::insertUnconditionalBranch(MachineBasicBlock *MBB, 1669a06bfe05SJan Sjodin MachineBasicBlock *Dest, 1670a06bfe05SJan Sjodin const DebugLoc &DL) { 1671d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Inserting unconditional branch: " << MBB->getNumber() 1672a06bfe05SJan Sjodin << " -> " << Dest->getNumber() << "\n"); 1673a06bfe05SJan Sjodin MachineBasicBlock::instr_iterator Terminator = MBB->getFirstInstrTerminator(); 1674a06bfe05SJan Sjodin bool HasTerminator = Terminator != MBB->instr_end(); 1675a06bfe05SJan Sjodin if (HasTerminator) { 1676a06bfe05SJan Sjodin TII->ReplaceTailWithBranchTo(Terminator, Dest); 1677a06bfe05SJan Sjodin } 1678a06bfe05SJan Sjodin if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(Dest)) { 1679a06bfe05SJan Sjodin TII->insertUnconditionalBranch(*MBB, Dest, DL); 1680a06bfe05SJan Sjodin } 1681a06bfe05SJan Sjodin } 1682a06bfe05SJan Sjodin 1683a06bfe05SJan Sjodin static MachineBasicBlock *getSingleExitNode(MachineFunction &MF) { 1684a06bfe05SJan Sjodin MachineBasicBlock *result = nullptr; 1685a06bfe05SJan Sjodin for (auto &MFI : MF) { 1686c9fca53aSKazu Hirata if (MFI.succ_empty()) { 1687a06bfe05SJan Sjodin if (result == nullptr) { 1688a06bfe05SJan Sjodin result = &MFI; 1689a06bfe05SJan Sjodin } else { 1690a06bfe05SJan Sjodin return nullptr; 1691a06bfe05SJan Sjodin } 1692a06bfe05SJan Sjodin } 1693a06bfe05SJan Sjodin } 1694a06bfe05SJan Sjodin 1695a06bfe05SJan Sjodin return result; 1696a06bfe05SJan Sjodin } 1697a06bfe05SJan Sjodin 1698a06bfe05SJan Sjodin static bool hasOneExitNode(MachineFunction &MF) { 1699a06bfe05SJan Sjodin return getSingleExitNode(MF) != nullptr; 1700a06bfe05SJan Sjodin } 1701a06bfe05SJan Sjodin 1702a06bfe05SJan Sjodin MachineBasicBlock * 1703a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::createLinearizedExitBlock(RegionMRT *Region) { 1704a06bfe05SJan Sjodin auto Exit = Region->getSucc(); 1705a06bfe05SJan Sjodin 1706a06bfe05SJan Sjodin // If the exit is the end of the function, we just use the existing 1707a06bfe05SJan Sjodin MachineFunction *MF = Region->getEntry()->getParent(); 1708a06bfe05SJan Sjodin if (Exit == nullptr && hasOneExitNode(*MF)) { 1709a06bfe05SJan Sjodin return &(*(--(Region->getEntry()->getParent()->end()))); 1710a06bfe05SJan Sjodin } 1711a06bfe05SJan Sjodin 1712a06bfe05SJan Sjodin MachineBasicBlock *LastMerge = MF->CreateMachineBasicBlock(); 1713a06bfe05SJan Sjodin if (Exit == nullptr) { 1714a06bfe05SJan Sjodin MachineFunction::iterator ExitIter = MF->end(); 1715a06bfe05SJan Sjodin MF->insert(ExitIter, LastMerge); 1716a06bfe05SJan Sjodin } else { 1717a06bfe05SJan Sjodin MachineFunction::iterator ExitIter = Exit->getIterator(); 1718a06bfe05SJan Sjodin MF->insert(ExitIter, LastMerge); 1719a06bfe05SJan Sjodin LastMerge->addSuccessor(Exit); 1720a06bfe05SJan Sjodin insertUnconditionalBranch(LastMerge, Exit); 1721d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Created exit block: " << LastMerge->getNumber() 1722d34e60caSNicola Zaghen << "\n"); 1723a06bfe05SJan Sjodin } 1724a06bfe05SJan Sjodin return LastMerge; 1725a06bfe05SJan Sjodin } 1726a06bfe05SJan Sjodin 1727a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::insertMergePHI(MachineBasicBlock *IfBB, 1728a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 1729a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1730a06bfe05SJan Sjodin unsigned DestRegister, 1731a06bfe05SJan Sjodin unsigned IfSourceRegister, 1732a06bfe05SJan Sjodin unsigned CodeSourceRegister, 1733a06bfe05SJan Sjodin bool IsUndefIfSource) { 1734a06bfe05SJan Sjodin // If this is the function exit block, we don't need a phi. 1735a06bfe05SJan Sjodin if (MergeBB->succ_begin() == MergeBB->succ_end()) { 1736a06bfe05SJan Sjodin return; 1737a06bfe05SJan Sjodin } 1738d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Merge PHI (" << printMBBReference(*MergeBB) 1739a8a83d15SFrancis Visoiu Mistrih << "): " << printReg(DestRegister, TRI) << " = PHI(" 174025528d6dSFrancis Visoiu Mistrih << printReg(IfSourceRegister, TRI) << ", " 1741d34e60caSNicola Zaghen << printMBBReference(*IfBB) 1742d34e60caSNicola Zaghen << printReg(CodeSourceRegister, TRI) << ", " 1743d34e60caSNicola Zaghen << printMBBReference(*CodeBB) << ")\n"); 1744a06bfe05SJan Sjodin const DebugLoc &DL = MergeBB->findDebugLoc(MergeBB->begin()); 1745a06bfe05SJan Sjodin MachineInstrBuilder MIB = BuildMI(*MergeBB, MergeBB->instr_begin(), DL, 1746a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), DestRegister); 1747a06bfe05SJan Sjodin if (IsUndefIfSource && false) { 1748a06bfe05SJan Sjodin MIB.addReg(IfSourceRegister, RegState::Undef); 1749a06bfe05SJan Sjodin } else { 1750a06bfe05SJan Sjodin MIB.addReg(IfSourceRegister); 1751a06bfe05SJan Sjodin } 1752a06bfe05SJan Sjodin MIB.addMBB(IfBB); 1753a06bfe05SJan Sjodin MIB.addReg(CodeSourceRegister); 1754a06bfe05SJan Sjodin MIB.addMBB(CodeBB); 1755a06bfe05SJan Sjodin } 1756a06bfe05SJan Sjodin 1757a06bfe05SJan Sjodin static void removeExternalCFGSuccessors(MachineBasicBlock *MBB) { 1758a06bfe05SJan Sjodin for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(), 1759a06bfe05SJan Sjodin E = MBB->succ_end(); 1760a06bfe05SJan Sjodin PI != E; ++PI) { 1761a06bfe05SJan Sjodin if ((*PI) != MBB) { 1762a06bfe05SJan Sjodin (MBB)->removeSuccessor(*PI); 1763a06bfe05SJan Sjodin } 1764a06bfe05SJan Sjodin } 1765a06bfe05SJan Sjodin } 1766a06bfe05SJan Sjodin 1767a06bfe05SJan Sjodin static void removeExternalCFGEdges(MachineBasicBlock *StartMBB, 1768a06bfe05SJan Sjodin MachineBasicBlock *EndMBB) { 1769a06bfe05SJan Sjodin 1770dc6e8dfdSJacob Lambert // We have to check against the StartMBB successor because a 1771a06bfe05SJan Sjodin // structurized region with a loop will have the entry block split, 1772a06bfe05SJan Sjodin // and the backedge will go to the entry successor. 1773a06bfe05SJan Sjodin DenseSet<std::pair<MachineBasicBlock *, MachineBasicBlock *>> Succs; 1774a06bfe05SJan Sjodin unsigned SuccSize = StartMBB->succ_size(); 1775a06bfe05SJan Sjodin if (SuccSize > 0) { 1776a06bfe05SJan Sjodin MachineBasicBlock *StartMBBSucc = *(StartMBB->succ_begin()); 1777a06bfe05SJan Sjodin for (MachineBasicBlock::succ_iterator PI = EndMBB->succ_begin(), 1778a06bfe05SJan Sjodin E = EndMBB->succ_end(); 1779a06bfe05SJan Sjodin PI != E; ++PI) { 1780a06bfe05SJan Sjodin // Either we have a back-edge to the entry block, or a back-edge to the 17816a391bbfSHiroshi Inoue // successor of the entry block since the block may be split. 1782a06bfe05SJan Sjodin if ((*PI) != StartMBB && 1783a06bfe05SJan Sjodin !((*PI) == StartMBBSucc && StartMBB != EndMBB && SuccSize == 1)) { 1784a06bfe05SJan Sjodin Succs.insert( 1785a06bfe05SJan Sjodin std::pair<MachineBasicBlock *, MachineBasicBlock *>(EndMBB, *PI)); 1786a06bfe05SJan Sjodin } 1787a06bfe05SJan Sjodin } 1788a06bfe05SJan Sjodin } 1789a06bfe05SJan Sjodin 1790a06bfe05SJan Sjodin for (MachineBasicBlock::pred_iterator PI = StartMBB->pred_begin(), 1791a06bfe05SJan Sjodin E = StartMBB->pred_end(); 1792a06bfe05SJan Sjodin PI != E; ++PI) { 1793a06bfe05SJan Sjodin if ((*PI) != EndMBB) { 1794a06bfe05SJan Sjodin Succs.insert( 1795a06bfe05SJan Sjodin std::pair<MachineBasicBlock *, MachineBasicBlock *>(*PI, StartMBB)); 1796a06bfe05SJan Sjodin } 1797a06bfe05SJan Sjodin } 1798a06bfe05SJan Sjodin 1799a06bfe05SJan Sjodin for (auto SI : Succs) { 1800a06bfe05SJan Sjodin std::pair<MachineBasicBlock *, MachineBasicBlock *> Edge = SI; 1801d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Removing edge: " << printMBBReference(*Edge.first) 180225528d6dSFrancis Visoiu Mistrih << " -> " << printMBBReference(*Edge.second) << "\n"); 1803a06bfe05SJan Sjodin Edge.first->removeSuccessor(Edge.second); 1804a06bfe05SJan Sjodin } 1805a06bfe05SJan Sjodin } 1806a06bfe05SJan Sjodin 1807a06bfe05SJan Sjodin MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfBlock( 1808a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, MachineBasicBlock *CodeBBStart, 1809a06bfe05SJan Sjodin MachineBasicBlock *CodeBBEnd, MachineBasicBlock *SelectBB, unsigned IfReg, 1810a06bfe05SJan Sjodin bool InheritPreds) { 1811a06bfe05SJan Sjodin MachineFunction *MF = MergeBB->getParent(); 1812a06bfe05SJan Sjodin MachineBasicBlock *IfBB = MF->CreateMachineBasicBlock(); 1813a06bfe05SJan Sjodin 1814a06bfe05SJan Sjodin if (InheritPreds) { 1815a06bfe05SJan Sjodin for (MachineBasicBlock::pred_iterator PI = CodeBBStart->pred_begin(), 1816a06bfe05SJan Sjodin E = CodeBBStart->pred_end(); 1817a06bfe05SJan Sjodin PI != E; ++PI) { 1818a06bfe05SJan Sjodin if ((*PI) != CodeBBEnd) { 1819a06bfe05SJan Sjodin MachineBasicBlock *Pred = (*PI); 1820a06bfe05SJan Sjodin Pred->addSuccessor(IfBB); 1821a06bfe05SJan Sjodin } 1822a06bfe05SJan Sjodin } 1823a06bfe05SJan Sjodin } 1824a06bfe05SJan Sjodin 1825a06bfe05SJan Sjodin removeExternalCFGEdges(CodeBBStart, CodeBBEnd); 1826a06bfe05SJan Sjodin 1827a06bfe05SJan Sjodin auto CodeBBStartI = CodeBBStart->getIterator(); 1828a06bfe05SJan Sjodin auto CodeBBEndI = CodeBBEnd->getIterator(); 1829a06bfe05SJan Sjodin auto MergeIter = MergeBB->getIterator(); 1830a06bfe05SJan Sjodin MF->insert(MergeIter, IfBB); 1831a06bfe05SJan Sjodin MF->splice(MergeIter, CodeBBStartI, ++CodeBBEndI); 1832a06bfe05SJan Sjodin IfBB->addSuccessor(MergeBB); 1833a06bfe05SJan Sjodin IfBB->addSuccessor(CodeBBStart); 1834a06bfe05SJan Sjodin 1835d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Created If block: " << IfBB->getNumber() << "\n"); 18366a391bbfSHiroshi Inoue // Ensure that the MergeBB is a successor of the CodeEndBB. 1837a06bfe05SJan Sjodin if (!CodeBBEnd->isSuccessor(MergeBB)) 1838a06bfe05SJan Sjodin CodeBBEnd->addSuccessor(MergeBB); 1839a06bfe05SJan Sjodin 1840d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Moved " << printMBBReference(*CodeBBStart) 1841d34e60caSNicola Zaghen << " through " << printMBBReference(*CodeBBEnd) << "\n"); 1842a06bfe05SJan Sjodin 1843a06bfe05SJan Sjodin // If we have a single predecessor we can find a reasonable debug location 1844a06bfe05SJan Sjodin MachineBasicBlock *SinglePred = 1845a06bfe05SJan Sjodin CodeBBStart->pred_size() == 1 ? *(CodeBBStart->pred_begin()) : nullptr; 1846a06bfe05SJan Sjodin const DebugLoc &DL = SinglePred 1847a06bfe05SJan Sjodin ? SinglePred->findDebugLoc(SinglePred->getFirstTerminator()) 1848a06bfe05SJan Sjodin : DebugLoc(); 1849a06bfe05SJan Sjodin 185098de0d22SJay Foad Register Reg = 1851a06bfe05SJan Sjodin TII->insertEQ(IfBB, IfBB->begin(), DL, IfReg, 1852a06bfe05SJan Sjodin SelectBB->getNumber() /* CodeBBStart->getNumber() */); 1853a06bfe05SJan Sjodin if (&(*(IfBB->getParent()->begin())) == IfBB) { 1854a06bfe05SJan Sjodin TII->materializeImmediate(*IfBB, IfBB->begin(), DL, IfReg, 1855a06bfe05SJan Sjodin CodeBBStart->getNumber()); 1856a06bfe05SJan Sjodin } 1857a06bfe05SJan Sjodin MachineOperand RegOp = MachineOperand::CreateReg(Reg, false, false, true); 1858a06bfe05SJan Sjodin ArrayRef<MachineOperand> Cond(RegOp); 1859a06bfe05SJan Sjodin TII->insertBranch(*IfBB, MergeBB, CodeBBStart, Cond, DL); 1860a06bfe05SJan Sjodin 1861a06bfe05SJan Sjodin return IfBB; 1862a06bfe05SJan Sjodin } 1863a06bfe05SJan Sjodin 1864a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::ensureCondIsNotKilled( 1865a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> Cond) { 1866a06bfe05SJan Sjodin if (Cond.size() != 1) 1867a06bfe05SJan Sjodin return; 1868a06bfe05SJan Sjodin if (!Cond[0].isReg()) 1869a06bfe05SJan Sjodin return; 1870a06bfe05SJan Sjodin 18710c476111SDaniel Sanders Register CondReg = Cond[0].getReg(); 1872a06bfe05SJan Sjodin for (auto UI = MRI->use_begin(CondReg), E = MRI->use_end(); UI != E; ++UI) { 1873a06bfe05SJan Sjodin (*UI).setIsKill(false); 1874a06bfe05SJan Sjodin } 1875a06bfe05SJan Sjodin } 1876a06bfe05SJan Sjodin 1877a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteCodeBBTerminator(MachineBasicBlock *CodeBB, 1878a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1879a06bfe05SJan Sjodin unsigned BBSelectReg) { 1880a06bfe05SJan Sjodin MachineBasicBlock *TrueBB = nullptr; 1881a06bfe05SJan Sjodin MachineBasicBlock *FalseBB = nullptr; 1882a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> Cond; 1883a06bfe05SJan Sjodin MachineBasicBlock *FallthroughBB = FallthroughMap[CodeBB]; 1884a06bfe05SJan Sjodin TII->analyzeBranch(*CodeBB, TrueBB, FalseBB, Cond); 1885a06bfe05SJan Sjodin 1886a06bfe05SJan Sjodin const DebugLoc &DL = CodeBB->findDebugLoc(CodeBB->getFirstTerminator()); 1887a06bfe05SJan Sjodin 1888a06bfe05SJan Sjodin if (FalseBB == nullptr && TrueBB == nullptr && FallthroughBB == nullptr) { 1889a06bfe05SJan Sjodin // This is an exit block, hence no successors. We will assign the 1890a06bfe05SJan Sjodin // bb select register to the entry block. 1891a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1892a06bfe05SJan Sjodin BBSelectReg, 1893a06bfe05SJan Sjodin CodeBB->getParent()->begin()->getNumber()); 1894a06bfe05SJan Sjodin insertUnconditionalBranch(CodeBB, MergeBB, DL); 1895a06bfe05SJan Sjodin return; 1896a06bfe05SJan Sjodin } 1897a06bfe05SJan Sjodin 1898a06bfe05SJan Sjodin if (FalseBB == nullptr && TrueBB == nullptr) { 1899a06bfe05SJan Sjodin TrueBB = FallthroughBB; 1900a06bfe05SJan Sjodin } else if (TrueBB != nullptr) { 1901a06bfe05SJan Sjodin FalseBB = 1902a06bfe05SJan Sjodin (FallthroughBB && (FallthroughBB != TrueBB)) ? FallthroughBB : FalseBB; 1903a06bfe05SJan Sjodin } 1904a06bfe05SJan Sjodin 1905a06bfe05SJan Sjodin if ((TrueBB != nullptr && FalseBB == nullptr) || (TrueBB == FalseBB)) { 1906a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1907a06bfe05SJan Sjodin BBSelectReg, TrueBB->getNumber()); 1908a06bfe05SJan Sjodin } else { 1909a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(BBSelectReg); 19100c476111SDaniel Sanders Register TrueBBReg = MRI->createVirtualRegister(RegClass); 19110c476111SDaniel Sanders Register FalseBBReg = MRI->createVirtualRegister(RegClass); 1912a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1913a06bfe05SJan Sjodin TrueBBReg, TrueBB->getNumber()); 1914a06bfe05SJan Sjodin TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, 1915a06bfe05SJan Sjodin FalseBBReg, FalseBB->getNumber()); 1916a06bfe05SJan Sjodin ensureCondIsNotKilled(Cond); 1917a06bfe05SJan Sjodin TII->insertVectorSelect(*CodeBB, CodeBB->getFirstTerminator(), DL, 1918a06bfe05SJan Sjodin BBSelectReg, Cond, TrueBBReg, FalseBBReg); 1919a06bfe05SJan Sjodin } 1920a06bfe05SJan Sjodin 1921a06bfe05SJan Sjodin insertUnconditionalBranch(CodeBB, MergeBB, DL); 1922a06bfe05SJan Sjodin } 1923a06bfe05SJan Sjodin 1924a06bfe05SJan Sjodin MachineInstr *AMDGPUMachineCFGStructurizer::getDefInstr(unsigned Reg) { 1925a06bfe05SJan Sjodin if (MRI->def_begin(Reg) == MRI->def_end()) { 1926d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " 1927d34e60caSNicola Zaghen << printReg(Reg, MRI->getTargetRegisterInfo()) 1928a06bfe05SJan Sjodin << " has NO defs\n"); 1929a06bfe05SJan Sjodin } else if (!MRI->hasOneDef(Reg)) { 1930d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Register " 1931d34e60caSNicola Zaghen << printReg(Reg, MRI->getTargetRegisterInfo()) 1932a06bfe05SJan Sjodin << " has multiple defs\n"); 1933d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DEFS BEGIN:\n"); 1934a06bfe05SJan Sjodin for (auto DI = MRI->def_begin(Reg), DE = MRI->def_end(); DI != DE; ++DI) { 1935d34e60caSNicola Zaghen LLVM_DEBUG(DI->getParent()->dump()); 1936a06bfe05SJan Sjodin } 1937d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DEFS END\n"); 1938a06bfe05SJan Sjodin } 1939a06bfe05SJan Sjodin 1940a06bfe05SJan Sjodin assert(MRI->hasOneDef(Reg) && "Register has multiple definitions"); 1941a06bfe05SJan Sjodin return (*(MRI->def_begin(Reg))).getParent(); 1942a06bfe05SJan Sjodin } 1943a06bfe05SJan Sjodin 1944a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::insertChainedPHI(MachineBasicBlock *IfBB, 1945a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 1946a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 1947a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 1948a06bfe05SJan Sjodin unsigned DestReg, 1949a06bfe05SJan Sjodin unsigned SourceReg) { 1950a06bfe05SJan Sjodin // In this function we know we are part of a chain already, so we need 1951a06bfe05SJan Sjodin // to add the registers to the existing chain, and rename the register 1952a06bfe05SJan Sjodin // inside the region. 1953a06bfe05SJan Sjodin bool IsSingleBB = InnerRegion->getEntry() == InnerRegion->getExit(); 1954a06bfe05SJan Sjodin MachineInstr *DefInstr = getDefInstr(SourceReg); 1955a06bfe05SJan Sjodin if (DefInstr->isPHI() && DefInstr->getParent() == CodeBB && IsSingleBB) { 1956a06bfe05SJan Sjodin // Handle the case where the def is a PHI-def inside a basic 1957a06bfe05SJan Sjodin // block, then we only need to do renaming. Special care needs to 1958a06bfe05SJan Sjodin // be taken if the PHI-def is part of an existing chain, or if a 1959a06bfe05SJan Sjodin // new one needs to be created. 1960a06bfe05SJan Sjodin InnerRegion->replaceRegisterInsideRegion(SourceReg, DestReg, true, MRI); 1961a06bfe05SJan Sjodin 1962a06bfe05SJan Sjodin // We collect all PHI Information, and if we are at the region entry, 1963a06bfe05SJan Sjodin // all PHIs will be removed, and then re-introduced if needed. 1964a06bfe05SJan Sjodin storePHILinearizationInfoDest(DestReg, *DefInstr); 1965a06bfe05SJan Sjodin // We have picked up all the information we need now and can remove 1966a06bfe05SJan Sjodin // the PHI 1967a06bfe05SJan Sjodin PHIInfo.removeSource(DestReg, SourceReg, CodeBB); 1968a06bfe05SJan Sjodin DefInstr->eraseFromParent(); 1969a06bfe05SJan Sjodin } else { 1970a06bfe05SJan Sjodin // If this is not a phi-def, or it is a phi-def but from a linearized region 1971a06bfe05SJan Sjodin if (IsSingleBB && DefInstr->getParent() == InnerRegion->getEntry()) { 1972a06bfe05SJan Sjodin // If this is a single BB and the definition is in this block we 1973a06bfe05SJan Sjodin // need to replace any uses outside the region. 1974a06bfe05SJan Sjodin InnerRegion->replaceRegisterOutsideRegion(SourceReg, DestReg, false, MRI); 1975a06bfe05SJan Sjodin } 1976a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(DestReg); 19770c476111SDaniel Sanders Register NextDestReg = MRI->createVirtualRegister(RegClass); 1978a06bfe05SJan Sjodin bool IsLastDef = PHIInfo.getNumSources(DestReg) == 1; 1979d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert Chained PHI\n"); 1980a06bfe05SJan Sjodin insertMergePHI(IfBB, InnerRegion->getExit(), MergeBB, DestReg, NextDestReg, 1981a06bfe05SJan Sjodin SourceReg, IsLastDef); 1982a06bfe05SJan Sjodin 1983a06bfe05SJan Sjodin PHIInfo.removeSource(DestReg, SourceReg, CodeBB); 1984a06bfe05SJan Sjodin if (IsLastDef) { 1985a06bfe05SJan Sjodin const DebugLoc &DL = IfBB->findDebugLoc(IfBB->getFirstTerminator()); 1986a06bfe05SJan Sjodin TII->materializeImmediate(*IfBB, IfBB->getFirstTerminator(), DL, 1987a06bfe05SJan Sjodin NextDestReg, 0); 1988a06bfe05SJan Sjodin PHIInfo.deleteDef(DestReg); 1989a06bfe05SJan Sjodin } else { 1990a06bfe05SJan Sjodin PHIInfo.replaceDef(DestReg, NextDestReg); 1991a06bfe05SJan Sjodin } 1992a06bfe05SJan Sjodin } 1993a06bfe05SJan Sjodin } 1994a06bfe05SJan Sjodin 1995a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::containsDef(MachineBasicBlock *MBB, 1996a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 1997a06bfe05SJan Sjodin unsigned Register) { 1998a06bfe05SJan Sjodin return getDefInstr(Register)->getParent() == MBB || 1999a06bfe05SJan Sjodin InnerRegion->contains(getDefInstr(Register)->getParent()); 2000a06bfe05SJan Sjodin } 2001a06bfe05SJan Sjodin 2002a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::rewriteLiveOutRegs(MachineBasicBlock *IfBB, 2003a06bfe05SJan Sjodin MachineBasicBlock *CodeBB, 2004a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, 2005a06bfe05SJan Sjodin LinearizedRegion *InnerRegion, 2006a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 2007a06bfe05SJan Sjodin DenseSet<unsigned> *LiveOuts = InnerRegion->getLiveOuts(); 2008a06bfe05SJan Sjodin SmallVector<unsigned, 4> OldLiveOuts; 2009a06bfe05SJan Sjodin bool IsSingleBB = InnerRegion->getEntry() == InnerRegion->getExit(); 2010a06bfe05SJan Sjodin for (auto OLI : *LiveOuts) { 2011a06bfe05SJan Sjodin OldLiveOuts.push_back(OLI); 2012a06bfe05SJan Sjodin } 2013a06bfe05SJan Sjodin 2014a06bfe05SJan Sjodin for (auto LI : OldLiveOuts) { 2015d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LiveOut: " << printReg(LI, TRI)); 2016a06bfe05SJan Sjodin if (!containsDef(CodeBB, InnerRegion, LI) || 2017a06bfe05SJan Sjodin (!IsSingleBB && (getDefInstr(LI)->getParent() == LRegion->getExit()))) { 2018dc6e8dfdSJacob Lambert // If the register simply lives through the CodeBB, we don't have 2019a06bfe05SJan Sjodin // to rewrite anything since the register is not defined in this 2020a06bfe05SJan Sjodin // part of the code. 2021d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "- through"); 2022a06bfe05SJan Sjodin continue; 2023a06bfe05SJan Sjodin } 2024d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2025a06bfe05SJan Sjodin unsigned Reg = LI; 2026a06bfe05SJan Sjodin if (/*!PHIInfo.isSource(Reg) &&*/ Reg != InnerRegion->getBBSelectRegOut()) { 2027a06bfe05SJan Sjodin // If the register is live out, we do want to create a phi, 2028dc6e8dfdSJacob Lambert // unless it is from the Exit block, because in that case there 2029a06bfe05SJan Sjodin // is already a PHI, and no need to create a new one. 2030a06bfe05SJan Sjodin 2031a06bfe05SJan Sjodin // If the register is just a live out def and not part of a phi 2032a06bfe05SJan Sjodin // chain, we need to create a PHI node to handle the if region, 2033a06bfe05SJan Sjodin // and replace all uses outside of the region with the new dest 2034a06bfe05SJan Sjodin // register, unless it is the outgoing BB select register. We have 2035dc6e8dfdSJacob Lambert // already created phi nodes for these. 2036a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(Reg); 20370c476111SDaniel Sanders Register PHIDestReg = MRI->createVirtualRegister(RegClass); 20380c476111SDaniel Sanders Register IfSourceReg = MRI->createVirtualRegister(RegClass); 2039a06bfe05SJan Sjodin // Create initializer, this value is never used, but is needed 2040a06bfe05SJan Sjodin // to satisfy SSA. 2041d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Initializer for reg: " << printReg(Reg) << "\n"); 2042a06bfe05SJan Sjodin TII->materializeImmediate(*IfBB, IfBB->getFirstTerminator(), DebugLoc(), 2043a06bfe05SJan Sjodin IfSourceReg, 0); 2044a06bfe05SJan Sjodin 2045a06bfe05SJan Sjodin InnerRegion->replaceRegisterOutsideRegion(Reg, PHIDestReg, true, MRI); 2046d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert Non-Chained Live out PHI\n"); 2047a06bfe05SJan Sjodin insertMergePHI(IfBB, InnerRegion->getExit(), MergeBB, PHIDestReg, 2048a06bfe05SJan Sjodin IfSourceReg, Reg, true); 2049a06bfe05SJan Sjodin } 2050a06bfe05SJan Sjodin } 2051a06bfe05SJan Sjodin 2052a06bfe05SJan Sjodin // Handle the chained definitions in PHIInfo, checking if this basic block 2053a06bfe05SJan Sjodin // is a source block for a definition. 2054a06bfe05SJan Sjodin SmallVector<unsigned, 4> Sources; 2055a06bfe05SJan Sjodin if (PHIInfo.findSourcesFromMBB(CodeBB, Sources)) { 2056d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Inserting PHI Live Out from " 2057d34e60caSNicola Zaghen << printMBBReference(*CodeBB) << "\n"); 2058a06bfe05SJan Sjodin for (auto SI : Sources) { 2059a06bfe05SJan Sjodin unsigned DestReg; 2060a06bfe05SJan Sjodin PHIInfo.findDest(SI, CodeBB, DestReg); 2061a06bfe05SJan Sjodin insertChainedPHI(IfBB, CodeBB, MergeBB, InnerRegion, DestReg, SI); 2062a06bfe05SJan Sjodin } 2063d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insertion done.\n"); 2064a06bfe05SJan Sjodin } 2065a06bfe05SJan Sjodin 2066d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2067a06bfe05SJan Sjodin } 2068a06bfe05SJan Sjodin 2069a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::prunePHIInfo(MachineBasicBlock *MBB) { 2070d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Before PHI Prune\n"); 2071d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2072a06bfe05SJan Sjodin SmallVector<std::tuple<unsigned, unsigned, MachineBasicBlock *>, 4> 2073a06bfe05SJan Sjodin ElimiatedSources; 2074a06bfe05SJan Sjodin for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; 2075a06bfe05SJan Sjodin ++DRI) { 2076a06bfe05SJan Sjodin 2077a06bfe05SJan Sjodin unsigned DestReg = *DRI; 2078a06bfe05SJan Sjodin auto SE = PHIInfo.sources_end(DestReg); 2079a06bfe05SJan Sjodin 2080a06bfe05SJan Sjodin bool MBBContainsPHISource = false; 2081a06bfe05SJan Sjodin // Check if there is a PHI source in this MBB 2082a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2083a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2084a06bfe05SJan Sjodin MachineOperand *Def = &(*(MRI->def_begin(SourceReg))); 2085a06bfe05SJan Sjodin if (Def->getParent()->getParent() == MBB) { 2086a06bfe05SJan Sjodin MBBContainsPHISource = true; 2087a06bfe05SJan Sjodin } 2088a06bfe05SJan Sjodin } 2089a06bfe05SJan Sjodin 2090a06bfe05SJan Sjodin // If so, all other sources are useless since we know this block 2091a06bfe05SJan Sjodin // is always executed when the region is executed. 2092a06bfe05SJan Sjodin if (MBBContainsPHISource) { 2093a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2094a06bfe05SJan Sjodin PHILinearize::PHISourceT Source = *SRI; 2095a06bfe05SJan Sjodin unsigned SourceReg = Source.first; 2096a06bfe05SJan Sjodin MachineBasicBlock *SourceMBB = Source.second; 2097a06bfe05SJan Sjodin MachineOperand *Def = &(*(MRI->def_begin(SourceReg))); 2098a06bfe05SJan Sjodin if (Def->getParent()->getParent() != MBB) { 2099a06bfe05SJan Sjodin ElimiatedSources.push_back( 2100a06bfe05SJan Sjodin std::make_tuple(DestReg, SourceReg, SourceMBB)); 2101a06bfe05SJan Sjodin } 2102a06bfe05SJan Sjodin } 2103a06bfe05SJan Sjodin } 2104a06bfe05SJan Sjodin } 2105a06bfe05SJan Sjodin 2106a06bfe05SJan Sjodin // Remove the PHI sources that are in the given MBB 2107a06bfe05SJan Sjodin for (auto &SourceInfo : ElimiatedSources) { 2108a06bfe05SJan Sjodin PHIInfo.removeSource(std::get<0>(SourceInfo), std::get<1>(SourceInfo), 2109a06bfe05SJan Sjodin std::get<2>(SourceInfo)); 2110a06bfe05SJan Sjodin } 2111d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "After PHI Prune\n"); 2112d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2113a06bfe05SJan Sjodin } 2114a06bfe05SJan Sjodin 2115a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::createEntryPHI(LinearizedRegion *CurrentRegion, 2116a06bfe05SJan Sjodin unsigned DestReg) { 2117a06bfe05SJan Sjodin MachineBasicBlock *Entry = CurrentRegion->getEntry(); 2118a06bfe05SJan Sjodin MachineBasicBlock *Exit = CurrentRegion->getExit(); 2119a06bfe05SJan Sjodin 2120d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "RegionExit: " << Exit->getNumber() << " Pred: " 2121d34e60caSNicola Zaghen << (*(Entry->pred_begin()))->getNumber() << "\n"); 2122a06bfe05SJan Sjodin 2123a06bfe05SJan Sjodin int NumSources = 0; 2124a06bfe05SJan Sjodin auto SE = PHIInfo.sources_end(DestReg); 2125a06bfe05SJan Sjodin 2126a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2127a06bfe05SJan Sjodin NumSources++; 2128a06bfe05SJan Sjodin } 2129a06bfe05SJan Sjodin 2130a06bfe05SJan Sjodin if (NumSources == 1) { 2131a06bfe05SJan Sjodin auto SRI = PHIInfo.sources_begin(DestReg); 2132a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2133a06bfe05SJan Sjodin replaceRegisterWith(DestReg, SourceReg); 2134a06bfe05SJan Sjodin } else { 2135a06bfe05SJan Sjodin const DebugLoc &DL = Entry->findDebugLoc(Entry->begin()); 2136a06bfe05SJan Sjodin MachineInstrBuilder MIB = BuildMI(*Entry, Entry->instr_begin(), DL, 2137a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), DestReg); 2138d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Entry PHI " << printReg(DestReg, TRI) << " = PHI("); 2139a06bfe05SJan Sjodin 2140a06bfe05SJan Sjodin unsigned CurrentBackedgeReg = 0; 2141a06bfe05SJan Sjodin 2142a06bfe05SJan Sjodin for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { 2143a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2144a06bfe05SJan Sjodin 2145a06bfe05SJan Sjodin if (CurrentRegion->contains((*SRI).second)) { 2146a06bfe05SJan Sjodin if (CurrentBackedgeReg == 0) { 2147a06bfe05SJan Sjodin CurrentBackedgeReg = SourceReg; 2148a06bfe05SJan Sjodin } else { 2149a06bfe05SJan Sjodin MachineInstr *PHIDefInstr = getDefInstr(SourceReg); 2150a06bfe05SJan Sjodin MachineBasicBlock *PHIDefMBB = PHIDefInstr->getParent(); 2151a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = 2152a06bfe05SJan Sjodin MRI->getRegClass(CurrentBackedgeReg); 21530c476111SDaniel Sanders Register NewBackedgeReg = MRI->createVirtualRegister(RegClass); 2154a06bfe05SJan Sjodin MachineInstrBuilder BackedgePHI = 2155a06bfe05SJan Sjodin BuildMI(*PHIDefMBB, PHIDefMBB->instr_begin(), DL, 2156a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), NewBackedgeReg); 2157a06bfe05SJan Sjodin BackedgePHI.addReg(CurrentBackedgeReg); 2158a06bfe05SJan Sjodin BackedgePHI.addMBB(getPHIPred(*PHIDefInstr, 0)); 2159a06bfe05SJan Sjodin BackedgePHI.addReg(getPHISourceReg(*PHIDefInstr, 1)); 2160a06bfe05SJan Sjodin BackedgePHI.addMBB((*SRI).second); 2161a06bfe05SJan Sjodin CurrentBackedgeReg = NewBackedgeReg; 2162d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() 2163d34e60caSNicola Zaghen << "Inserting backedge PHI: " 2164a8a83d15SFrancis Visoiu Mistrih << printReg(NewBackedgeReg, TRI) << " = PHI(" 216525528d6dSFrancis Visoiu Mistrih << printReg(CurrentBackedgeReg, TRI) << ", " 2166d34e60caSNicola Zaghen << printMBBReference(*getPHIPred(*PHIDefInstr, 0)) << ", " 2167d34e60caSNicola Zaghen << printReg(getPHISourceReg(*PHIDefInstr, 1), TRI) << ", " 2168d34e60caSNicola Zaghen << printMBBReference(*(*SRI).second)); 2169a06bfe05SJan Sjodin } 2170a06bfe05SJan Sjodin } else { 2171a06bfe05SJan Sjodin MIB.addReg(SourceReg); 2172a06bfe05SJan Sjodin MIB.addMBB((*SRI).second); 2173d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", " 217425528d6dSFrancis Visoiu Mistrih << printMBBReference(*(*SRI).second) << ", "); 2175a06bfe05SJan Sjodin } 2176a06bfe05SJan Sjodin } 2177a06bfe05SJan Sjodin 2178a06bfe05SJan Sjodin // Add the final backedge register source to the entry phi 2179a06bfe05SJan Sjodin if (CurrentBackedgeReg != 0) { 2180a06bfe05SJan Sjodin MIB.addReg(CurrentBackedgeReg); 2181a06bfe05SJan Sjodin MIB.addMBB(Exit); 2182d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(CurrentBackedgeReg, TRI) << ", " 218325528d6dSFrancis Visoiu Mistrih << printMBBReference(*Exit) << ")\n"); 2184a06bfe05SJan Sjodin } else { 2185d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ")\n"); 2186a06bfe05SJan Sjodin } 2187a06bfe05SJan Sjodin } 2188a06bfe05SJan Sjodin } 2189a06bfe05SJan Sjodin 2190a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::createEntryPHIs(LinearizedRegion *CurrentRegion) { 2191d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2192a06bfe05SJan Sjodin 2193a06bfe05SJan Sjodin for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; 2194a06bfe05SJan Sjodin ++DRI) { 2195a06bfe05SJan Sjodin 2196a06bfe05SJan Sjodin unsigned DestReg = *DRI; 2197a06bfe05SJan Sjodin createEntryPHI(CurrentRegion, DestReg); 2198a06bfe05SJan Sjodin } 2199a06bfe05SJan Sjodin PHIInfo.clear(); 2200a06bfe05SJan Sjodin } 2201a06bfe05SJan Sjodin 220234978602SJay Foad void AMDGPUMachineCFGStructurizer::replaceRegisterWith( 220334978602SJay Foad unsigned Register, class Register NewRegister) { 2204a06bfe05SJan Sjodin assert(Register != NewRegister && "Cannot replace a reg with itself"); 2205a06bfe05SJan Sjodin 2206a06bfe05SJan Sjodin for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Register), 2207a06bfe05SJan Sjodin E = MRI->reg_end(); 2208a06bfe05SJan Sjodin I != E;) { 2209a06bfe05SJan Sjodin MachineOperand &O = *I; 2210a06bfe05SJan Sjodin ++I; 221134978602SJay Foad if (NewRegister.isPhysical()) { 2212d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Trying to substitute physical register: " 22139d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) 2214a06bfe05SJan Sjodin << "\n"); 2215a06bfe05SJan Sjodin llvm_unreachable("Cannot substitute physical registers"); 2216a06bfe05SJan Sjodin // We don't handle physical registers, but if we need to 2217a06bfe05SJan Sjodin // in the future This is how we do it: 2218a06bfe05SJan Sjodin // O.substPhysReg(NewRegister, *TRI); 2219a06bfe05SJan Sjodin } else { 2220d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Replacing register: " 22219d419d3bSFrancis Visoiu Mistrih << printReg(Register, MRI->getTargetRegisterInfo()) 2222a06bfe05SJan Sjodin << " with " 22239d419d3bSFrancis Visoiu Mistrih << printReg(NewRegister, MRI->getTargetRegisterInfo()) 2224a06bfe05SJan Sjodin << "\n"); 2225a06bfe05SJan Sjodin O.setReg(NewRegister); 2226a06bfe05SJan Sjodin } 2227a06bfe05SJan Sjodin } 2228a06bfe05SJan Sjodin PHIInfo.deleteDef(Register); 2229a06bfe05SJan Sjodin 2230a06bfe05SJan Sjodin getRegionMRT()->replaceLiveOutReg(Register, NewRegister); 2231a06bfe05SJan Sjodin 2232d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2233a06bfe05SJan Sjodin } 2234a06bfe05SJan Sjodin 2235a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::resolvePHIInfos(MachineBasicBlock *FunctionEntry) { 2236d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Resolve PHI Infos\n"); 2237d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2238a06bfe05SJan Sjodin for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; 2239a06bfe05SJan Sjodin ++DRI) { 2240a06bfe05SJan Sjodin unsigned DestReg = *DRI; 2241d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DestReg: " << printReg(DestReg, TRI) << "\n"); 2242a06bfe05SJan Sjodin auto SRI = PHIInfo.sources_begin(DestReg); 2243a06bfe05SJan Sjodin unsigned SourceReg = (*SRI).first; 2244d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "DestReg: " << printReg(DestReg, TRI) 22459d419d3bSFrancis Visoiu Mistrih << " SourceReg: " << printReg(SourceReg, TRI) << "\n"); 2246a06bfe05SJan Sjodin 2247a06bfe05SJan Sjodin assert(PHIInfo.sources_end(DestReg) == ++SRI && 2248a06bfe05SJan Sjodin "More than one phi source in entry node"); 2249a06bfe05SJan Sjodin replaceRegisterWith(DestReg, SourceReg); 2250a06bfe05SJan Sjodin } 2251a06bfe05SJan Sjodin } 2252a06bfe05SJan Sjodin 2253a06bfe05SJan Sjodin static bool isFunctionEntryBlock(MachineBasicBlock *MBB) { 2254a06bfe05SJan Sjodin return ((&(*(MBB->getParent()->begin()))) == MBB); 2255a06bfe05SJan Sjodin } 2256a06bfe05SJan Sjodin 2257a06bfe05SJan Sjodin MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfRegion( 2258a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, MachineBasicBlock *CodeBB, 2259a06bfe05SJan Sjodin LinearizedRegion *CurrentRegion, unsigned BBSelectRegIn, 2260a06bfe05SJan Sjodin unsigned BBSelectRegOut) { 2261a06bfe05SJan Sjodin if (isFunctionEntryBlock(CodeBB) && !CurrentRegion->getHasLoop()) { 2262a06bfe05SJan Sjodin // Handle non-loop function entry block. 2263a06bfe05SJan Sjodin // We need to allow loops to the entry block and then 2264a06bfe05SJan Sjodin rewriteCodeBBTerminator(CodeBB, MergeBB, BBSelectRegOut); 2265a06bfe05SJan Sjodin resolvePHIInfos(CodeBB); 2266a06bfe05SJan Sjodin removeExternalCFGSuccessors(CodeBB); 2267a06bfe05SJan Sjodin CodeBB->addSuccessor(MergeBB); 2268a06bfe05SJan Sjodin CurrentRegion->addMBB(CodeBB); 2269a06bfe05SJan Sjodin return nullptr; 2270a06bfe05SJan Sjodin } 2271a06bfe05SJan Sjodin if (CurrentRegion->getEntry() == CodeBB && !CurrentRegion->getHasLoop()) { 2272a06bfe05SJan Sjodin // Handle non-loop region entry block. 2273a06bfe05SJan Sjodin MachineFunction *MF = MergeBB->getParent(); 2274a06bfe05SJan Sjodin auto MergeIter = MergeBB->getIterator(); 2275a06bfe05SJan Sjodin auto CodeBBStartIter = CodeBB->getIterator(); 2276a06bfe05SJan Sjodin auto CodeBBEndIter = ++(CodeBB->getIterator()); 2277a06bfe05SJan Sjodin if (CodeBBEndIter != MergeIter) { 2278a06bfe05SJan Sjodin MF->splice(MergeIter, CodeBBStartIter, CodeBBEndIter); 2279a06bfe05SJan Sjodin } 2280a06bfe05SJan Sjodin rewriteCodeBBTerminator(CodeBB, MergeBB, BBSelectRegOut); 2281a06bfe05SJan Sjodin prunePHIInfo(CodeBB); 2282a06bfe05SJan Sjodin createEntryPHIs(CurrentRegion); 2283a06bfe05SJan Sjodin removeExternalCFGSuccessors(CodeBB); 2284a06bfe05SJan Sjodin CodeBB->addSuccessor(MergeBB); 2285a06bfe05SJan Sjodin CurrentRegion->addMBB(CodeBB); 2286a06bfe05SJan Sjodin return nullptr; 2287a06bfe05SJan Sjodin } else { 2288a06bfe05SJan Sjodin // Handle internal block. 2289a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(BBSelectRegIn); 22900c476111SDaniel Sanders Register CodeBBSelectReg = MRI->createVirtualRegister(RegClass); 2291a06bfe05SJan Sjodin rewriteCodeBBTerminator(CodeBB, MergeBB, CodeBBSelectReg); 2292a06bfe05SJan Sjodin bool IsRegionEntryBB = CurrentRegion->getEntry() == CodeBB; 2293a06bfe05SJan Sjodin MachineBasicBlock *IfBB = createIfBlock(MergeBB, CodeBB, CodeBB, CodeBB, 2294a06bfe05SJan Sjodin BBSelectRegIn, IsRegionEntryBB); 2295a06bfe05SJan Sjodin CurrentRegion->addMBB(IfBB); 2296a06bfe05SJan Sjodin // If this is the entry block we need to make the If block the new 2297a06bfe05SJan Sjodin // linearized region entry. 2298a06bfe05SJan Sjodin if (IsRegionEntryBB) { 2299a06bfe05SJan Sjodin CurrentRegion->setEntry(IfBB); 2300a06bfe05SJan Sjodin 2301a06bfe05SJan Sjodin if (CurrentRegion->getHasLoop()) { 2302a06bfe05SJan Sjodin MachineBasicBlock *RegionExit = CurrentRegion->getExit(); 2303a06bfe05SJan Sjodin MachineBasicBlock *ETrueBB = nullptr; 2304a06bfe05SJan Sjodin MachineBasicBlock *EFalseBB = nullptr; 2305a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> ECond; 2306a06bfe05SJan Sjodin 2307a06bfe05SJan Sjodin const DebugLoc &DL = DebugLoc(); 2308a06bfe05SJan Sjodin TII->analyzeBranch(*RegionExit, ETrueBB, EFalseBB, ECond); 2309a06bfe05SJan Sjodin TII->removeBranch(*RegionExit); 2310a06bfe05SJan Sjodin 2311a06bfe05SJan Sjodin // We need to create a backedge if there is a loop 231298de0d22SJay Foad Register Reg = TII->insertNE( 2313a06bfe05SJan Sjodin RegionExit, RegionExit->instr_end(), DL, 2314a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getInnerOutputRegister(), 2315a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getEntry()->getNumber()); 2316a06bfe05SJan Sjodin MachineOperand RegOp = 2317a06bfe05SJan Sjodin MachineOperand::CreateReg(Reg, false, false, true); 2318a06bfe05SJan Sjodin ArrayRef<MachineOperand> Cond(RegOp); 2319d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "RegionExitReg: "); 2320d34e60caSNicola Zaghen LLVM_DEBUG(Cond[0].print(dbgs(), TRI)); 2321d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2322a06bfe05SJan Sjodin TII->insertBranch(*RegionExit, CurrentRegion->getEntry(), RegionExit, 2323a06bfe05SJan Sjodin Cond, DebugLoc()); 2324a06bfe05SJan Sjodin RegionExit->addSuccessor(CurrentRegion->getEntry()); 2325a06bfe05SJan Sjodin } 2326a06bfe05SJan Sjodin } 2327a06bfe05SJan Sjodin CurrentRegion->addMBB(CodeBB); 2328a06bfe05SJan Sjodin LinearizedRegion InnerRegion(CodeBB, MRI, TRI, PHIInfo); 2329a06bfe05SJan Sjodin 2330a06bfe05SJan Sjodin InnerRegion.setParent(CurrentRegion); 2331d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert BB Select PHI (BB)\n"); 2332a06bfe05SJan Sjodin insertMergePHI(IfBB, CodeBB, MergeBB, BBSelectRegOut, BBSelectRegIn, 2333a06bfe05SJan Sjodin CodeBBSelectReg); 2334a06bfe05SJan Sjodin InnerRegion.addMBB(MergeBB); 2335a06bfe05SJan Sjodin 2336d34e60caSNicola Zaghen LLVM_DEBUG(InnerRegion.print(dbgs(), TRI)); 2337a06bfe05SJan Sjodin rewriteLiveOutRegs(IfBB, CodeBB, MergeBB, &InnerRegion, CurrentRegion); 2338a06bfe05SJan Sjodin extractKilledPHIs(CodeBB); 2339a06bfe05SJan Sjodin if (IsRegionEntryBB) { 2340a06bfe05SJan Sjodin createEntryPHIs(CurrentRegion); 2341a06bfe05SJan Sjodin } 2342a06bfe05SJan Sjodin return IfBB; 2343a06bfe05SJan Sjodin } 2344a06bfe05SJan Sjodin } 2345a06bfe05SJan Sjodin 2346a06bfe05SJan Sjodin MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfRegion( 2347a06bfe05SJan Sjodin MachineBasicBlock *MergeBB, LinearizedRegion *InnerRegion, 2348a06bfe05SJan Sjodin LinearizedRegion *CurrentRegion, MachineBasicBlock *SelectBB, 2349a06bfe05SJan Sjodin unsigned BBSelectRegIn, unsigned BBSelectRegOut) { 2350a06bfe05SJan Sjodin unsigned CodeBBSelectReg = 2351a06bfe05SJan Sjodin InnerRegion->getRegionMRT()->getInnerOutputRegister(); 2352a06bfe05SJan Sjodin MachineBasicBlock *CodeEntryBB = InnerRegion->getEntry(); 2353a06bfe05SJan Sjodin MachineBasicBlock *CodeExitBB = InnerRegion->getExit(); 2354a06bfe05SJan Sjodin MachineBasicBlock *IfBB = createIfBlock(MergeBB, CodeEntryBB, CodeExitBB, 2355a06bfe05SJan Sjodin SelectBB, BBSelectRegIn, true); 2356a06bfe05SJan Sjodin CurrentRegion->addMBB(IfBB); 2357a06bfe05SJan Sjodin bool isEntry = CurrentRegion->getEntry() == InnerRegion->getEntry(); 2358a06bfe05SJan Sjodin if (isEntry) { 2359a06bfe05SJan Sjodin 2360a06bfe05SJan Sjodin if (CurrentRegion->getHasLoop()) { 2361a06bfe05SJan Sjodin MachineBasicBlock *RegionExit = CurrentRegion->getExit(); 2362a06bfe05SJan Sjodin MachineBasicBlock *ETrueBB = nullptr; 2363a06bfe05SJan Sjodin MachineBasicBlock *EFalseBB = nullptr; 2364a06bfe05SJan Sjodin SmallVector<MachineOperand, 1> ECond; 2365a06bfe05SJan Sjodin 2366a06bfe05SJan Sjodin const DebugLoc &DL = DebugLoc(); 2367a06bfe05SJan Sjodin TII->analyzeBranch(*RegionExit, ETrueBB, EFalseBB, ECond); 2368a06bfe05SJan Sjodin TII->removeBranch(*RegionExit); 2369a06bfe05SJan Sjodin 2370a06bfe05SJan Sjodin // We need to create a backedge if there is a loop 237198de0d22SJay Foad Register Reg = 2372a06bfe05SJan Sjodin TII->insertNE(RegionExit, RegionExit->instr_end(), DL, 2373a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getInnerOutputRegister(), 2374a06bfe05SJan Sjodin CurrentRegion->getRegionMRT()->getEntry()->getNumber()); 2375a06bfe05SJan Sjodin MachineOperand RegOp = MachineOperand::CreateReg(Reg, false, false, true); 2376a06bfe05SJan Sjodin ArrayRef<MachineOperand> Cond(RegOp); 2377d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "RegionExitReg: "); 2378d34e60caSNicola Zaghen LLVM_DEBUG(Cond[0].print(dbgs(), TRI)); 2379d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2380a06bfe05SJan Sjodin TII->insertBranch(*RegionExit, CurrentRegion->getEntry(), RegionExit, 2381a06bfe05SJan Sjodin Cond, DebugLoc()); 2382a06bfe05SJan Sjodin RegionExit->addSuccessor(IfBB); 2383a06bfe05SJan Sjodin } 2384a06bfe05SJan Sjodin } 2385a06bfe05SJan Sjodin CurrentRegion->addMBBs(InnerRegion); 2386d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Insert BB Select PHI (region)\n"); 2387a06bfe05SJan Sjodin insertMergePHI(IfBB, CodeExitBB, MergeBB, BBSelectRegOut, BBSelectRegIn, 2388a06bfe05SJan Sjodin CodeBBSelectReg); 2389a06bfe05SJan Sjodin 2390a06bfe05SJan Sjodin rewriteLiveOutRegs(IfBB, /* CodeEntryBB */ CodeExitBB, MergeBB, InnerRegion, 2391a06bfe05SJan Sjodin CurrentRegion); 2392a06bfe05SJan Sjodin 2393a06bfe05SJan Sjodin rewriteRegionEntryPHIs(InnerRegion, IfBB); 2394a06bfe05SJan Sjodin 2395a06bfe05SJan Sjodin if (isEntry) { 2396a06bfe05SJan Sjodin CurrentRegion->setEntry(IfBB); 2397a06bfe05SJan Sjodin } 2398a06bfe05SJan Sjodin 2399a06bfe05SJan Sjodin if (isEntry) { 2400a06bfe05SJan Sjodin createEntryPHIs(CurrentRegion); 2401a06bfe05SJan Sjodin } 2402a06bfe05SJan Sjodin 2403a06bfe05SJan Sjodin return IfBB; 2404a06bfe05SJan Sjodin } 2405a06bfe05SJan Sjodin 2406a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::splitLoopPHI(MachineInstr &PHI, 2407a06bfe05SJan Sjodin MachineBasicBlock *Entry, 2408a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc, 2409a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 2410a06bfe05SJan Sjodin SmallVector<unsigned, 2> PHIRegionIndices; 2411a06bfe05SJan Sjodin getPHIRegionIndices(LRegion, PHI, PHIRegionIndices); 2412a06bfe05SJan Sjodin 2413a06bfe05SJan Sjodin assert(PHIRegionIndices.size() == 1); 2414a06bfe05SJan Sjodin 2415a06bfe05SJan Sjodin unsigned RegionIndex = PHIRegionIndices[0]; 2416a06bfe05SJan Sjodin unsigned RegionSourceReg = getPHISourceReg(PHI, RegionIndex); 2417a06bfe05SJan Sjodin MachineBasicBlock *RegionSourceMBB = getPHIPred(PHI, RegionIndex); 2418a06bfe05SJan Sjodin unsigned PHIDest = getPHIDestReg(PHI); 2419a06bfe05SJan Sjodin unsigned PHISource = PHIDest; 2420a06bfe05SJan Sjodin unsigned ReplaceReg; 2421a06bfe05SJan Sjodin 2422a06bfe05SJan Sjodin if (shrinkPHI(PHI, PHIRegionIndices, &ReplaceReg)) { 2423a06bfe05SJan Sjodin PHISource = ReplaceReg; 2424a06bfe05SJan Sjodin } 2425a06bfe05SJan Sjodin 2426a06bfe05SJan Sjodin const TargetRegisterClass *RegClass = MRI->getRegClass(PHIDest); 24270c476111SDaniel Sanders Register NewDestReg = MRI->createVirtualRegister(RegClass); 2428a06bfe05SJan Sjodin LRegion->replaceRegisterInsideRegion(PHIDest, NewDestReg, false, MRI); 2429a06bfe05SJan Sjodin MachineInstrBuilder MIB = 2430a06bfe05SJan Sjodin BuildMI(*EntrySucc, EntrySucc->instr_begin(), PHI.getDebugLoc(), 2431a06bfe05SJan Sjodin TII->get(TargetOpcode::PHI), NewDestReg); 2432d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Split Entry PHI " << printReg(NewDestReg, TRI) 2433d34e60caSNicola Zaghen << " = PHI("); 2434a06bfe05SJan Sjodin MIB.addReg(PHISource); 2435a06bfe05SJan Sjodin MIB.addMBB(Entry); 2436d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << printReg(PHISource, TRI) << ", " 243725528d6dSFrancis Visoiu Mistrih << printMBBReference(*Entry)); 2438a06bfe05SJan Sjodin MIB.addReg(RegionSourceReg); 2439a06bfe05SJan Sjodin MIB.addMBB(RegionSourceMBB); 2440d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " ," << printReg(RegionSourceReg, TRI) << ", " 244125528d6dSFrancis Visoiu Mistrih << printMBBReference(*RegionSourceMBB) << ")\n"); 2442a06bfe05SJan Sjodin } 2443a06bfe05SJan Sjodin 2444a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::splitLoopPHIs(MachineBasicBlock *Entry, 2445a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc, 2446a06bfe05SJan Sjodin LinearizedRegion *LRegion) { 2447a06bfe05SJan Sjodin SmallVector<MachineInstr *, 2> PHIs; 2448a06bfe05SJan Sjodin collectPHIs(Entry, PHIs); 2449a06bfe05SJan Sjodin 2450a06bfe05SJan Sjodin for (auto PHII : PHIs) { 2451a06bfe05SJan Sjodin splitLoopPHI(*PHII, Entry, EntrySucc, LRegion); 2452a06bfe05SJan Sjodin } 2453a06bfe05SJan Sjodin } 2454a06bfe05SJan Sjodin 2455a06bfe05SJan Sjodin // Split the exit block so that we can insert a end control flow 2456a06bfe05SJan Sjodin MachineBasicBlock * 2457a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::splitExit(LinearizedRegion *LRegion) { 2458a06bfe05SJan Sjodin auto MRTRegion = LRegion->getRegionMRT(); 2459a06bfe05SJan Sjodin auto Exit = LRegion->getExit(); 2460a06bfe05SJan Sjodin auto MF = Exit->getParent(); 2461a06bfe05SJan Sjodin auto Succ = MRTRegion->getSucc(); 2462a06bfe05SJan Sjodin 2463a06bfe05SJan Sjodin auto NewExit = MF->CreateMachineBasicBlock(); 2464a06bfe05SJan Sjodin auto AfterExitIter = Exit->getIterator(); 2465a06bfe05SJan Sjodin AfterExitIter++; 2466a06bfe05SJan Sjodin MF->insert(AfterExitIter, NewExit); 2467a06bfe05SJan Sjodin Exit->removeSuccessor(Succ); 2468a06bfe05SJan Sjodin Exit->addSuccessor(NewExit); 2469a06bfe05SJan Sjodin NewExit->addSuccessor(Succ); 2470a06bfe05SJan Sjodin insertUnconditionalBranch(NewExit, Succ); 2471a06bfe05SJan Sjodin LRegion->addMBB(NewExit); 2472a06bfe05SJan Sjodin LRegion->setExit(NewExit); 2473a06bfe05SJan Sjodin 2474d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Created new exit block: " << NewExit->getNumber() 2475d34e60caSNicola Zaghen << "\n"); 2476a06bfe05SJan Sjodin 2477a06bfe05SJan Sjodin // Replace any PHI Predecessors in the successor with NewExit 2478a06bfe05SJan Sjodin for (auto &II : *Succ) { 2479a06bfe05SJan Sjodin MachineInstr &Instr = II; 2480a06bfe05SJan Sjodin 2481a06bfe05SJan Sjodin // If we are past the PHI instructions we are done 2482a06bfe05SJan Sjodin if (!Instr.isPHI()) 2483a06bfe05SJan Sjodin break; 2484a06bfe05SJan Sjodin 2485a06bfe05SJan Sjodin int numPreds = getPHINumInputs(Instr); 2486a06bfe05SJan Sjodin for (int i = 0; i < numPreds; ++i) { 2487a06bfe05SJan Sjodin auto Pred = getPHIPred(Instr, i); 2488a06bfe05SJan Sjodin if (Pred == Exit) { 2489a06bfe05SJan Sjodin setPhiPred(Instr, i, NewExit); 2490a06bfe05SJan Sjodin } 2491a06bfe05SJan Sjodin } 2492a06bfe05SJan Sjodin } 2493a06bfe05SJan Sjodin 2494a06bfe05SJan Sjodin return NewExit; 2495a06bfe05SJan Sjodin } 2496a06bfe05SJan Sjodin 2497a06bfe05SJan Sjodin static MachineBasicBlock *split(MachineBasicBlock::iterator I) { 2498a06bfe05SJan Sjodin // Create the fall-through block. 2499a06bfe05SJan Sjodin MachineBasicBlock *MBB = (*I).getParent(); 2500a06bfe05SJan Sjodin MachineFunction *MF = MBB->getParent(); 2501a06bfe05SJan Sjodin MachineBasicBlock *SuccMBB = MF->CreateMachineBasicBlock(); 2502a06bfe05SJan Sjodin auto MBBIter = ++(MBB->getIterator()); 2503a06bfe05SJan Sjodin MF->insert(MBBIter, SuccMBB); 2504a06bfe05SJan Sjodin SuccMBB->transferSuccessorsAndUpdatePHIs(MBB); 2505a06bfe05SJan Sjodin MBB->addSuccessor(SuccMBB); 2506a06bfe05SJan Sjodin 2507a06bfe05SJan Sjodin // Splice the code over. 2508a06bfe05SJan Sjodin SuccMBB->splice(SuccMBB->end(), MBB, I, MBB->end()); 2509a06bfe05SJan Sjodin 2510a06bfe05SJan Sjodin return SuccMBB; 2511a06bfe05SJan Sjodin } 2512a06bfe05SJan Sjodin 2513a06bfe05SJan Sjodin // Split the entry block separating PHI-nodes and the rest of the code 2514a06bfe05SJan Sjodin // This is needed to insert an initializer for the bb select register 2515a06bfe05SJan Sjodin // inloop regions. 2516a06bfe05SJan Sjodin 2517a06bfe05SJan Sjodin MachineBasicBlock * 2518a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::splitEntry(LinearizedRegion *LRegion) { 2519a06bfe05SJan Sjodin MachineBasicBlock *Entry = LRegion->getEntry(); 2520a06bfe05SJan Sjodin MachineBasicBlock *EntrySucc = split(Entry->getFirstNonPHI()); 2521a06bfe05SJan Sjodin MachineBasicBlock *Exit = LRegion->getExit(); 2522a06bfe05SJan Sjodin 2523d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Split " << printMBBReference(*Entry) << " to " 252425528d6dSFrancis Visoiu Mistrih << printMBBReference(*Entry) << " -> " 252525528d6dSFrancis Visoiu Mistrih << printMBBReference(*EntrySucc) << "\n"); 2526a06bfe05SJan Sjodin LRegion->addMBB(EntrySucc); 2527a06bfe05SJan Sjodin 2528a06bfe05SJan Sjodin // Make the backedge go to Entry Succ 2529a06bfe05SJan Sjodin if (Exit->isSuccessor(Entry)) { 2530a06bfe05SJan Sjodin Exit->removeSuccessor(Entry); 2531a06bfe05SJan Sjodin } 2532a06bfe05SJan Sjodin Exit->addSuccessor(EntrySucc); 2533a06bfe05SJan Sjodin MachineInstr &Branch = *(Exit->instr_rbegin()); 2534a06bfe05SJan Sjodin for (auto &UI : Branch.uses()) { 2535a06bfe05SJan Sjodin if (UI.isMBB() && UI.getMBB() == Entry) { 2536a06bfe05SJan Sjodin UI.setMBB(EntrySucc); 2537a06bfe05SJan Sjodin } 2538a06bfe05SJan Sjodin } 2539a06bfe05SJan Sjodin 2540a06bfe05SJan Sjodin splitLoopPHIs(Entry, EntrySucc, LRegion); 2541a06bfe05SJan Sjodin 2542a06bfe05SJan Sjodin return EntrySucc; 2543a06bfe05SJan Sjodin } 2544a06bfe05SJan Sjodin 2545a06bfe05SJan Sjodin LinearizedRegion * 2546a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::initLinearizedRegion(RegionMRT *Region) { 2547a06bfe05SJan Sjodin LinearizedRegion *LRegion = Region->getLinearizedRegion(); 2548a06bfe05SJan Sjodin LRegion->initLiveOut(Region, MRI, TRI, PHIInfo); 2549a06bfe05SJan Sjodin LRegion->setEntry(Region->getEntry()); 2550a06bfe05SJan Sjodin return LRegion; 2551a06bfe05SJan Sjodin } 2552a06bfe05SJan Sjodin 2553a06bfe05SJan Sjodin static void removeOldExitPreds(RegionMRT *Region) { 2554a06bfe05SJan Sjodin MachineBasicBlock *Exit = Region->getSucc(); 2555a06bfe05SJan Sjodin if (Exit == nullptr) { 2556a06bfe05SJan Sjodin return; 2557a06bfe05SJan Sjodin } 2558a06bfe05SJan Sjodin for (MachineBasicBlock::pred_iterator PI = Exit->pred_begin(), 2559a06bfe05SJan Sjodin E = Exit->pred_end(); 2560a06bfe05SJan Sjodin PI != E; ++PI) { 2561a06bfe05SJan Sjodin if (Region->contains(*PI)) { 2562a06bfe05SJan Sjodin (*PI)->removeSuccessor(Exit); 2563a06bfe05SJan Sjodin } 2564a06bfe05SJan Sjodin } 2565a06bfe05SJan Sjodin } 2566a06bfe05SJan Sjodin 2567a06bfe05SJan Sjodin static bool mbbHasBackEdge(MachineBasicBlock *MBB, 2568a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 8> &MBBs) { 2569a06bfe05SJan Sjodin for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI) { 25708590a3e3SKazu Hirata if (MBBs.contains(*SI)) { 2571a06bfe05SJan Sjodin return true; 2572a06bfe05SJan Sjodin } 2573a06bfe05SJan Sjodin } 2574a06bfe05SJan Sjodin return false; 2575a06bfe05SJan Sjodin } 2576a06bfe05SJan Sjodin 2577a06bfe05SJan Sjodin static bool containsNewBackedge(MRT *Tree, 2578a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 8> &MBBs) { 2579a06bfe05SJan Sjodin // Need to traverse this in reverse since it is in post order. 2580a06bfe05SJan Sjodin if (Tree == nullptr) 2581a06bfe05SJan Sjodin return false; 2582a06bfe05SJan Sjodin 2583a06bfe05SJan Sjodin if (Tree->isMBB()) { 2584a06bfe05SJan Sjodin MachineBasicBlock *MBB = Tree->getMBBMRT()->getMBB(); 2585a06bfe05SJan Sjodin MBBs.insert(MBB); 2586a06bfe05SJan Sjodin if (mbbHasBackEdge(MBB, MBBs)) { 2587a06bfe05SJan Sjodin return true; 2588a06bfe05SJan Sjodin } 2589a06bfe05SJan Sjodin } else { 2590a06bfe05SJan Sjodin RegionMRT *Region = Tree->getRegionMRT(); 2591*14d656b3SKazu Hirata for (MRT *C : llvm::reverse(*Region->getChildren())) 2592*14d656b3SKazu Hirata if (containsNewBackedge(C, MBBs)) 2593a06bfe05SJan Sjodin return true; 2594a06bfe05SJan Sjodin } 2595a06bfe05SJan Sjodin return false; 2596a06bfe05SJan Sjodin } 2597a06bfe05SJan Sjodin 2598a06bfe05SJan Sjodin static bool containsNewBackedge(RegionMRT *Region) { 2599a06bfe05SJan Sjodin SmallPtrSet<MachineBasicBlock *, 8> MBBs; 2600a06bfe05SJan Sjodin return containsNewBackedge(Region, MBBs); 2601a06bfe05SJan Sjodin } 2602a06bfe05SJan Sjodin 2603a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::structurizeComplexRegion(RegionMRT *Region) { 2604a06bfe05SJan Sjodin auto *LRegion = initLinearizedRegion(Region); 2605a06bfe05SJan Sjodin LRegion->setHasLoop(containsNewBackedge(Region)); 2606a06bfe05SJan Sjodin MachineBasicBlock *LastMerge = createLinearizedExitBlock(Region); 2607a06bfe05SJan Sjodin MachineBasicBlock *CurrentMerge = LastMerge; 2608a06bfe05SJan Sjodin LRegion->addMBB(LastMerge); 2609a06bfe05SJan Sjodin LRegion->setExit(LastMerge); 2610a06bfe05SJan Sjodin 2611a06bfe05SJan Sjodin rewriteRegionExitPHIs(Region, LastMerge, LRegion); 2612a06bfe05SJan Sjodin removeOldExitPreds(Region); 2613a06bfe05SJan Sjodin 2614d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2615a06bfe05SJan Sjodin 2616a06bfe05SJan Sjodin SetVector<MRT *> *Children = Region->getChildren(); 2617d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "===========If Region Start===============\n"); 2618a06bfe05SJan Sjodin if (LRegion->getHasLoop()) { 2619d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Has Backedge: Yes\n"); 2620a06bfe05SJan Sjodin } else { 2621d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Has Backedge: No\n"); 2622a06bfe05SJan Sjodin } 2623a06bfe05SJan Sjodin 2624a06bfe05SJan Sjodin unsigned BBSelectRegIn; 2625a06bfe05SJan Sjodin unsigned BBSelectRegOut; 2626a06bfe05SJan Sjodin for (auto CI = Children->begin(), CE = Children->end(); CI != CE; ++CI) { 2627d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "CurrentRegion: \n"); 2628d34e60caSNicola Zaghen LLVM_DEBUG(LRegion->print(dbgs(), TRI)); 2629a06bfe05SJan Sjodin 2630a06bfe05SJan Sjodin auto CNI = CI; 2631a06bfe05SJan Sjodin ++CNI; 2632a06bfe05SJan Sjodin 2633a06bfe05SJan Sjodin MRT *Child = (*CI); 2634a06bfe05SJan Sjodin 2635a06bfe05SJan Sjodin if (Child->isRegion()) { 2636a06bfe05SJan Sjodin 2637a06bfe05SJan Sjodin LinearizedRegion *InnerLRegion = 2638a06bfe05SJan Sjodin Child->getRegionMRT()->getLinearizedRegion(); 2639a06bfe05SJan Sjodin // We found the block is the exit of an inner region, we need 2640a06bfe05SJan Sjodin // to put it in the current linearized region. 2641a06bfe05SJan Sjodin 2642d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Linearizing region: "); 2643d34e60caSNicola Zaghen LLVM_DEBUG(InnerLRegion->print(dbgs(), TRI)); 2644d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n"); 2645a06bfe05SJan Sjodin 2646a06bfe05SJan Sjodin MachineBasicBlock *InnerEntry = InnerLRegion->getEntry(); 2647a06bfe05SJan Sjodin if ((&(*(InnerEntry->getParent()->begin()))) == InnerEntry) { 2648a06bfe05SJan Sjodin // Entry has already been linearized, no need to do this region. 2649a06bfe05SJan Sjodin unsigned OuterSelect = InnerLRegion->getBBSelectRegOut(); 2650a06bfe05SJan Sjodin unsigned InnerSelectReg = 2651a06bfe05SJan Sjodin InnerLRegion->getRegionMRT()->getInnerOutputRegister(); 2652a06bfe05SJan Sjodin replaceRegisterWith(InnerSelectReg, OuterSelect), 2653a06bfe05SJan Sjodin resolvePHIInfos(InnerEntry); 2654a06bfe05SJan Sjodin if (!InnerLRegion->getExit()->isSuccessor(CurrentMerge)) 2655a06bfe05SJan Sjodin InnerLRegion->getExit()->addSuccessor(CurrentMerge); 2656a06bfe05SJan Sjodin continue; 2657a06bfe05SJan Sjodin } 2658a06bfe05SJan Sjodin 2659a06bfe05SJan Sjodin BBSelectRegOut = Child->getBBSelectRegOut(); 2660a06bfe05SJan Sjodin BBSelectRegIn = Child->getBBSelectRegIn(); 2661a06bfe05SJan Sjodin 2662d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegIn: " << printReg(BBSelectRegIn, TRI) 2663a06bfe05SJan Sjodin << "\n"); 2664d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegOut: " << printReg(BBSelectRegOut, TRI) 2665a06bfe05SJan Sjodin << "\n"); 2666a06bfe05SJan Sjodin 2667a06bfe05SJan Sjodin MachineBasicBlock *IfEnd = CurrentMerge; 2668a06bfe05SJan Sjodin CurrentMerge = createIfRegion(CurrentMerge, InnerLRegion, LRegion, 2669a06bfe05SJan Sjodin Child->getRegionMRT()->getEntry(), 2670a06bfe05SJan Sjodin BBSelectRegIn, BBSelectRegOut); 2671a06bfe05SJan Sjodin TII->convertNonUniformIfRegion(CurrentMerge, IfEnd); 2672a06bfe05SJan Sjodin } else { 2673a06bfe05SJan Sjodin MachineBasicBlock *MBB = Child->getMBBMRT()->getMBB(); 2674d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Linearizing block: " << MBB->getNumber() << "\n"); 2675a06bfe05SJan Sjodin 2676a06bfe05SJan Sjodin if (MBB == getSingleExitNode(*(MBB->getParent()))) { 2677a06bfe05SJan Sjodin // If this is the exit block then we need to skip to the next. 2678a06bfe05SJan Sjodin // The "in" register will be transferred to "out" in the next 2679a06bfe05SJan Sjodin // iteration. 2680a06bfe05SJan Sjodin continue; 2681a06bfe05SJan Sjodin } 2682a06bfe05SJan Sjodin 2683a06bfe05SJan Sjodin BBSelectRegOut = Child->getBBSelectRegOut(); 2684a06bfe05SJan Sjodin BBSelectRegIn = Child->getBBSelectRegIn(); 2685a06bfe05SJan Sjodin 2686d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegIn: " << printReg(BBSelectRegIn, TRI) 2687a06bfe05SJan Sjodin << "\n"); 2688d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "BBSelectRegOut: " << printReg(BBSelectRegOut, TRI) 2689a06bfe05SJan Sjodin << "\n"); 2690a06bfe05SJan Sjodin 2691a06bfe05SJan Sjodin MachineBasicBlock *IfEnd = CurrentMerge; 2692a06bfe05SJan Sjodin // This is a basic block that is not part of an inner region, we 2693a06bfe05SJan Sjodin // need to put it in the current linearized region. 2694a06bfe05SJan Sjodin CurrentMerge = createIfRegion(CurrentMerge, MBB, LRegion, BBSelectRegIn, 2695a06bfe05SJan Sjodin BBSelectRegOut); 2696a06bfe05SJan Sjodin if (CurrentMerge) { 2697a06bfe05SJan Sjodin TII->convertNonUniformIfRegion(CurrentMerge, IfEnd); 2698a06bfe05SJan Sjodin } 2699a06bfe05SJan Sjodin 2700d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2701a06bfe05SJan Sjodin } 2702a06bfe05SJan Sjodin } 2703a06bfe05SJan Sjodin 2704a06bfe05SJan Sjodin LRegion->removeFalseRegisterKills(MRI); 2705a06bfe05SJan Sjodin 2706a06bfe05SJan Sjodin if (LRegion->getHasLoop()) { 2707a06bfe05SJan Sjodin MachineBasicBlock *NewSucc = splitEntry(LRegion); 2708a06bfe05SJan Sjodin if (isFunctionEntryBlock(LRegion->getEntry())) { 2709a06bfe05SJan Sjodin resolvePHIInfos(LRegion->getEntry()); 2710a06bfe05SJan Sjodin } 2711a06bfe05SJan Sjodin const DebugLoc &DL = NewSucc->findDebugLoc(NewSucc->getFirstNonPHI()); 2712a06bfe05SJan Sjodin unsigned InReg = LRegion->getBBSelectRegIn(); 27130c476111SDaniel Sanders Register InnerSelectReg = 2714a06bfe05SJan Sjodin MRI->createVirtualRegister(MRI->getRegClass(InReg)); 27150c476111SDaniel Sanders Register NewInReg = MRI->createVirtualRegister(MRI->getRegClass(InReg)); 2716a06bfe05SJan Sjodin TII->materializeImmediate(*(LRegion->getEntry()), 2717a06bfe05SJan Sjodin LRegion->getEntry()->getFirstTerminator(), DL, 2718a06bfe05SJan Sjodin NewInReg, Region->getEntry()->getNumber()); 2719a06bfe05SJan Sjodin // Need to be careful about updating the registers inside the region. 2720a06bfe05SJan Sjodin LRegion->replaceRegisterInsideRegion(InReg, InnerSelectReg, false, MRI); 2721d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Loop BBSelect Merge PHI:\n"); 2722a06bfe05SJan Sjodin insertMergePHI(LRegion->getEntry(), LRegion->getExit(), NewSucc, 2723a06bfe05SJan Sjodin InnerSelectReg, NewInReg, 2724a06bfe05SJan Sjodin LRegion->getRegionMRT()->getInnerOutputRegister()); 2725a06bfe05SJan Sjodin splitExit(LRegion); 2726a06bfe05SJan Sjodin TII->convertNonUniformLoopRegion(NewSucc, LastMerge); 2727a06bfe05SJan Sjodin } 2728a06bfe05SJan Sjodin 2729a06bfe05SJan Sjodin if (Region->isRoot()) { 2730a06bfe05SJan Sjodin TII->insertReturn(*LastMerge); 2731a06bfe05SJan Sjodin } 2732a06bfe05SJan Sjodin 2733d34e60caSNicola Zaghen LLVM_DEBUG(Region->getEntry()->getParent()->dump()); 2734d34e60caSNicola Zaghen LLVM_DEBUG(LRegion->print(dbgs(), TRI)); 2735d34e60caSNicola Zaghen LLVM_DEBUG(PHIInfo.dump(MRI)); 2736a06bfe05SJan Sjodin 2737d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "===========If Region End===============\n"); 2738a06bfe05SJan Sjodin 2739a06bfe05SJan Sjodin Region->setLinearizedRegion(LRegion); 2740a06bfe05SJan Sjodin return true; 2741a06bfe05SJan Sjodin } 2742a06bfe05SJan Sjodin 2743a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::structurizeRegion(RegionMRT *Region) { 2744a06bfe05SJan Sjodin if (false && regionIsSimpleIf(Region)) { 2745a06bfe05SJan Sjodin transformSimpleIfRegion(Region); 2746a06bfe05SJan Sjodin return true; 2747a06bfe05SJan Sjodin } else if (regionIsSequence(Region)) { 2748a06bfe05SJan Sjodin fixupRegionExits(Region); 2749a06bfe05SJan Sjodin return false; 2750a06bfe05SJan Sjodin } else { 2751a06bfe05SJan Sjodin structurizeComplexRegion(Region); 2752a06bfe05SJan Sjodin } 2753a06bfe05SJan Sjodin return false; 2754a06bfe05SJan Sjodin } 2755a06bfe05SJan Sjodin 2756a06bfe05SJan Sjodin static int structurize_once = 0; 2757a06bfe05SJan Sjodin 2758a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::structurizeRegions(RegionMRT *Region, 2759a06bfe05SJan Sjodin bool isTopRegion) { 2760a06bfe05SJan Sjodin bool Changed = false; 2761a06bfe05SJan Sjodin 2762a06bfe05SJan Sjodin auto Children = Region->getChildren(); 2763a06bfe05SJan Sjodin for (auto CI : *Children) { 2764a06bfe05SJan Sjodin if (CI->isRegion()) { 2765a06bfe05SJan Sjodin Changed |= structurizeRegions(CI->getRegionMRT(), false); 2766a06bfe05SJan Sjodin } 2767a06bfe05SJan Sjodin } 2768a06bfe05SJan Sjodin 2769a06bfe05SJan Sjodin if (structurize_once < 2 || true) { 2770a06bfe05SJan Sjodin Changed |= structurizeRegion(Region); 2771a06bfe05SJan Sjodin structurize_once++; 2772a06bfe05SJan Sjodin } 2773a06bfe05SJan Sjodin return Changed; 2774a06bfe05SJan Sjodin } 2775a06bfe05SJan Sjodin 2776a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::initFallthroughMap(MachineFunction &MF) { 2777d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Fallthrough Map:\n"); 2778a06bfe05SJan Sjodin for (auto &MBBI : MF) { 2779a06bfe05SJan Sjodin MachineBasicBlock *MBB = MBBI.getFallThrough(); 2780a06bfe05SJan Sjodin if (MBB != nullptr) { 2781d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Fallthrough: " << MBBI.getNumber() << " -> " 2782a06bfe05SJan Sjodin << MBB->getNumber() << "\n"); 2783a06bfe05SJan Sjodin } 2784a06bfe05SJan Sjodin FallthroughMap[&MBBI] = MBB; 2785a06bfe05SJan Sjodin } 2786a06bfe05SJan Sjodin } 2787a06bfe05SJan Sjodin 2788a06bfe05SJan Sjodin void AMDGPUMachineCFGStructurizer::createLinearizedRegion(RegionMRT *Region, 2789a06bfe05SJan Sjodin unsigned SelectOut) { 2790a06bfe05SJan Sjodin LinearizedRegion *LRegion = new LinearizedRegion(); 2791a06bfe05SJan Sjodin if (SelectOut) { 2792a06bfe05SJan Sjodin LRegion->addLiveOut(SelectOut); 2793d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Add LiveOut (BBSelect): " << printReg(SelectOut, TRI) 2794a06bfe05SJan Sjodin << "\n"); 2795a06bfe05SJan Sjodin } 2796a06bfe05SJan Sjodin LRegion->setRegionMRT(Region); 2797a06bfe05SJan Sjodin Region->setLinearizedRegion(LRegion); 2798a06bfe05SJan Sjodin LRegion->setParent(Region->getParent() 2799a06bfe05SJan Sjodin ? Region->getParent()->getLinearizedRegion() 2800a06bfe05SJan Sjodin : nullptr); 2801a06bfe05SJan Sjodin } 2802a06bfe05SJan Sjodin 2803a06bfe05SJan Sjodin unsigned 2804a06bfe05SJan Sjodin AMDGPUMachineCFGStructurizer::initializeSelectRegisters(MRT *MRT, unsigned SelectOut, 2805a06bfe05SJan Sjodin MachineRegisterInfo *MRI, 2806a06bfe05SJan Sjodin const SIInstrInfo *TII) { 2807a06bfe05SJan Sjodin if (MRT->isRegion()) { 2808a06bfe05SJan Sjodin RegionMRT *Region = MRT->getRegionMRT(); 2809a06bfe05SJan Sjodin Region->setBBSelectRegOut(SelectOut); 2810a06bfe05SJan Sjodin unsigned InnerSelectOut = createBBSelectReg(TII, MRI); 2811a06bfe05SJan Sjodin 2812a06bfe05SJan Sjodin // Fixme: Move linearization creation to the original spot 2813a06bfe05SJan Sjodin createLinearizedRegion(Region, SelectOut); 2814a06bfe05SJan Sjodin 2815a06bfe05SJan Sjodin for (auto CI = Region->getChildren()->begin(), 2816a06bfe05SJan Sjodin CE = Region->getChildren()->end(); 2817a06bfe05SJan Sjodin CI != CE; ++CI) { 2818a06bfe05SJan Sjodin InnerSelectOut = 2819a06bfe05SJan Sjodin initializeSelectRegisters((*CI), InnerSelectOut, MRI, TII); 2820a06bfe05SJan Sjodin } 2821a06bfe05SJan Sjodin MRT->setBBSelectRegIn(InnerSelectOut); 2822a06bfe05SJan Sjodin return InnerSelectOut; 2823a06bfe05SJan Sjodin } else { 2824a06bfe05SJan Sjodin MRT->setBBSelectRegOut(SelectOut); 2825a06bfe05SJan Sjodin unsigned NewSelectIn = createBBSelectReg(TII, MRI); 2826a06bfe05SJan Sjodin MRT->setBBSelectRegIn(NewSelectIn); 2827a06bfe05SJan Sjodin return NewSelectIn; 2828a06bfe05SJan Sjodin } 2829a06bfe05SJan Sjodin } 2830a06bfe05SJan Sjodin 2831a06bfe05SJan Sjodin static void checkRegOnlyPHIInputs(MachineFunction &MF) { 2832a06bfe05SJan Sjodin for (auto &MBBI : MF) { 2833a06bfe05SJan Sjodin for (MachineBasicBlock::instr_iterator I = MBBI.instr_begin(), 2834a06bfe05SJan Sjodin E = MBBI.instr_end(); 2835a06bfe05SJan Sjodin I != E; ++I) { 2836a06bfe05SJan Sjodin MachineInstr &Instr = *I; 2837a06bfe05SJan Sjodin if (Instr.isPHI()) { 2838a06bfe05SJan Sjodin int numPreds = getPHINumInputs(Instr); 2839a06bfe05SJan Sjodin for (int i = 0; i < numPreds; ++i) { 2840a06bfe05SJan Sjodin assert(Instr.getOperand(i * 2 + 1).isReg() && 2841a06bfe05SJan Sjodin "PHI Operand not a register"); 2842a06bfe05SJan Sjodin } 2843a06bfe05SJan Sjodin } 2844a06bfe05SJan Sjodin } 2845a06bfe05SJan Sjodin } 2846a06bfe05SJan Sjodin } 2847a06bfe05SJan Sjodin 2848a06bfe05SJan Sjodin bool AMDGPUMachineCFGStructurizer::runOnMachineFunction(MachineFunction &MF) { 28495bfbae5cSTom Stellard const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 2850a06bfe05SJan Sjodin const SIInstrInfo *TII = ST.getInstrInfo(); 2851a06bfe05SJan Sjodin TRI = ST.getRegisterInfo(); 2852a06bfe05SJan Sjodin MRI = &(MF.getRegInfo()); 2853a06bfe05SJan Sjodin initFallthroughMap(MF); 2854a06bfe05SJan Sjodin 2855a06bfe05SJan Sjodin checkRegOnlyPHIInputs(MF); 2856d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "----STRUCTURIZER START----\n"); 2857d34e60caSNicola Zaghen LLVM_DEBUG(MF.dump()); 2858a06bfe05SJan Sjodin 2859a06bfe05SJan Sjodin Regions = &(getAnalysis<MachineRegionInfoPass>().getRegionInfo()); 2860d34e60caSNicola Zaghen LLVM_DEBUG(Regions->dump()); 2861a06bfe05SJan Sjodin 2862a06bfe05SJan Sjodin RegionMRT *RTree = MRT::buildMRT(MF, Regions, TII, MRI); 2863a06bfe05SJan Sjodin setRegionMRT(RTree); 2864a06bfe05SJan Sjodin initializeSelectRegisters(RTree, 0, MRI, TII); 2865d34e60caSNicola Zaghen LLVM_DEBUG(RTree->dump(TRI)); 2866a06bfe05SJan Sjodin bool result = structurizeRegions(RTree, true); 2867a06bfe05SJan Sjodin delete RTree; 2868d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "----STRUCTURIZER END----\n"); 2869a06bfe05SJan Sjodin initFallthroughMap(MF); 2870a06bfe05SJan Sjodin return result; 2871a06bfe05SJan Sjodin } 2872a06bfe05SJan Sjodin 2873d16eff81SEugene Zelenko char AMDGPUMachineCFGStructurizerID = AMDGPUMachineCFGStructurizer::ID; 2874d16eff81SEugene Zelenko 2875d16eff81SEugene Zelenko INITIALIZE_PASS_BEGIN(AMDGPUMachineCFGStructurizer, "amdgpu-machine-cfg-structurizer", 2876d16eff81SEugene Zelenko "AMDGPU Machine CFG Structurizer", false, false) 2877d16eff81SEugene Zelenko INITIALIZE_PASS_DEPENDENCY(MachineRegionInfoPass) 2878d16eff81SEugene Zelenko INITIALIZE_PASS_END(AMDGPUMachineCFGStructurizer, "amdgpu-machine-cfg-structurizer", 2879d16eff81SEugene Zelenko "AMDGPU Machine CFG Structurizer", false, false) 2880d16eff81SEugene Zelenko 2881a06bfe05SJan Sjodin FunctionPass *llvm::createAMDGPUMachineCFGStructurizerPass() { 2882a06bfe05SJan Sjodin return new AMDGPUMachineCFGStructurizer(); 2883a06bfe05SJan Sjodin } 2884