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