[X86][SSE] Vector integer/float conversion memory folding
[oota-llvm.git] / lib / Target / R600 / AMDILCFGStructurizer.cpp
1 //===-- AMDILCFGStructurizer.cpp - CFG Structurizer -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 /// \file
9 //==-----------------------------------------------------------------------===//
10
11 #include "AMDGPU.h"
12 #include "AMDGPUInstrInfo.h"
13 #include "R600InstrInfo.h"
14 #include "AMDGPUSubtarget.h"
15 #include "llvm/ADT/DepthFirstIterator.h"
16 #include "llvm/ADT/SCCIterator.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/MachineDominators.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineJumpTableInfo.h"
25 #include "llvm/CodeGen/MachineLoopInfo.h"
26 #include "llvm/CodeGen/MachinePostDominators.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/Dominators.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/Target/TargetMachine.h"
33
34 using namespace llvm;
35
36 #define DEBUG_TYPE "structcfg"
37
38 #define DEFAULT_VEC_SLOTS 8
39
40 // TODO: move-begin.
41
42 //===----------------------------------------------------------------------===//
43 //
44 // Statistics for CFGStructurizer.
45 //
46 //===----------------------------------------------------------------------===//
47
48 STATISTIC(numSerialPatternMatch,    "CFGStructurizer number of serial pattern "
49     "matched");
50 STATISTIC(numIfPatternMatch,        "CFGStructurizer number of if pattern "
51     "matched");
52 STATISTIC(numLoopcontPatternMatch,  "CFGStructurizer number of loop-continue "
53     "pattern matched");
54 STATISTIC(numClonedBlock,           "CFGStructurizer cloned blocks");
55 STATISTIC(numClonedInstr,           "CFGStructurizer cloned instructions");
56
57 namespace llvm {
58   void initializeAMDGPUCFGStructurizerPass(PassRegistry&);
59 }
60
61 //===----------------------------------------------------------------------===//
62 //
63 // Miscellaneous utility for CFGStructurizer.
64 //
65 //===----------------------------------------------------------------------===//
66 namespace {
67 #define SHOWNEWINSTR(i) \
68   DEBUG(dbgs() << "New instr: " << *i << "\n");
69
70 #define SHOWNEWBLK(b, msg) \
71 DEBUG( \
72   dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
73   dbgs() << "\n"; \
74 );
75
76 #define SHOWBLK_DETAIL(b, msg) \
77 DEBUG( \
78   if (b) { \
79   dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
80   b->print(dbgs()); \
81   dbgs() << "\n"; \
82   } \
83 );
84
85 #define INVALIDSCCNUM -1
86
87 template<class NodeT>
88 void ReverseVector(SmallVectorImpl<NodeT *> &Src) {
89   size_t sz = Src.size();
90   for (size_t i = 0; i < sz/2; ++i) {
91     NodeT *t = Src[i];
92     Src[i] = Src[sz - i - 1];
93     Src[sz - i - 1] = t;
94   }
95 }
96
97 } // end anonymous namespace
98
99 //===----------------------------------------------------------------------===//
100 //
101 // supporting data structure for CFGStructurizer
102 //
103 //===----------------------------------------------------------------------===//
104
105
106 namespace {
107
108 class BlockInformation {
109 public:
110   bool IsRetired;
111   int  SccNum;
112   BlockInformation() : IsRetired(false), SccNum(INVALIDSCCNUM) {}
113 };
114
115 } // end anonymous namespace
116
117 //===----------------------------------------------------------------------===//
118 //
119 // CFGStructurizer
120 //
121 //===----------------------------------------------------------------------===//
122
123 namespace {
124 class AMDGPUCFGStructurizer : public MachineFunctionPass {
125 public:
126   typedef SmallVector<MachineBasicBlock *, 32> MBBVector;
127   typedef std::map<MachineBasicBlock *, BlockInformation *> MBBInfoMap;
128   typedef std::map<MachineLoop *, MachineBasicBlock *> LoopLandInfoMap;
129
130   enum PathToKind {
131     Not_SinglePath = 0,
132     SinglePath_InPath = 1,
133     SinglePath_NotInPath = 2
134   };
135
136   static char ID;
137
138   AMDGPUCFGStructurizer() :
139       MachineFunctionPass(ID), TII(nullptr), TRI(nullptr) {
140     initializeAMDGPUCFGStructurizerPass(*PassRegistry::getPassRegistry());
141   }
142
143    const char *getPassName() const override {
144     return "AMDGPU Control Flow Graph structurizer Pass";
145   }
146
147   void getAnalysisUsage(AnalysisUsage &AU) const override {
148     AU.addPreserved<MachineFunctionAnalysis>();
149     AU.addRequired<MachineFunctionAnalysis>();
150     AU.addRequired<MachineDominatorTree>();
151     AU.addRequired<MachinePostDominatorTree>();
152     AU.addRequired<MachineLoopInfo>();
153   }
154
155   /// Perform the CFG structurization
156   bool run();
157
158   /// Perform the CFG preparation
159   /// This step will remove every unconditionnal/dead jump instructions and make
160   /// sure all loops have an exit block
161   bool prepare();
162
163   bool runOnMachineFunction(MachineFunction &MF) override {
164     TII = static_cast<const R600InstrInfo *>(MF.getSubtarget().getInstrInfo());
165     TRI = &TII->getRegisterInfo();
166     DEBUG(MF.dump(););
167     OrderedBlks.clear();
168     FuncRep = &MF;
169     MLI = &getAnalysis<MachineLoopInfo>();
170     DEBUG(dbgs() << "LoopInfo:\n"; PrintLoopinfo(*MLI););
171     MDT = &getAnalysis<MachineDominatorTree>();
172     DEBUG(MDT->print(dbgs(), (const llvm::Module*)nullptr););
173     PDT = &getAnalysis<MachinePostDominatorTree>();
174     DEBUG(PDT->print(dbgs()););
175     prepare();
176     run();
177     DEBUG(MF.dump(););
178     return true;
179   }
180
181 protected:
182   MachineDominatorTree *MDT;
183   MachinePostDominatorTree *PDT;
184   MachineLoopInfo *MLI;
185   const R600InstrInfo *TII;
186   const AMDGPURegisterInfo *TRI;
187
188   // PRINT FUNCTIONS
189   /// Print the ordered Blocks.
190   void printOrderedBlocks() const {
191     size_t i = 0;
192     for (MBBVector::const_iterator iterBlk = OrderedBlks.begin(),
193         iterBlkEnd = OrderedBlks.end(); iterBlk != iterBlkEnd; ++iterBlk, ++i) {
194       dbgs() << "BB" << (*iterBlk)->getNumber();
195       dbgs() << "(" << getSCCNum(*iterBlk) << "," << (*iterBlk)->size() << ")";
196       if (i != 0 && i % 10 == 0) {
197         dbgs() << "\n";
198       } else {
199         dbgs() << " ";
200       }
201     }
202   }
203   static void PrintLoopinfo(const MachineLoopInfo &LoopInfo) {
204     for (MachineLoop::iterator iter = LoopInfo.begin(),
205          iterEnd = LoopInfo.end(); iter != iterEnd; ++iter) {
206       (*iter)->print(dbgs(), 0);
207     }
208   }
209
210   // UTILITY FUNCTIONS
211   int getSCCNum(MachineBasicBlock *MBB) const;
212   MachineBasicBlock *getLoopLandInfo(MachineLoop *LoopRep) const;
213   bool hasBackEdge(MachineBasicBlock *MBB) const;
214   static unsigned getLoopDepth(MachineLoop *LoopRep);
215   bool isRetiredBlock(MachineBasicBlock *MBB) const;
216   bool isActiveLoophead(MachineBasicBlock *MBB) const;
217   PathToKind singlePathTo(MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
218       bool AllowSideEntry = true) const;
219   int countActiveBlock(MBBVector::const_iterator It,
220       MBBVector::const_iterator E) const;
221   bool needMigrateBlock(MachineBasicBlock *MBB) const;
222
223   // Utility Functions
224   void reversePredicateSetter(MachineBasicBlock::iterator I);
225   /// Compute the reversed DFS post order of Blocks
226   void orderBlocks(MachineFunction *MF);
227
228   // Function originally from CFGStructTraits
229   void insertInstrEnd(MachineBasicBlock *MBB, int NewOpcode,
230       DebugLoc DL = DebugLoc());
231   MachineInstr *insertInstrBefore(MachineBasicBlock *MBB, int NewOpcode,
232     DebugLoc DL = DebugLoc());
233   MachineInstr *insertInstrBefore(MachineBasicBlock::iterator I, int NewOpcode);
234   void insertCondBranchBefore(MachineBasicBlock::iterator I, int NewOpcode,
235       DebugLoc DL);
236   void insertCondBranchBefore(MachineBasicBlock *MBB,
237       MachineBasicBlock::iterator I, int NewOpcode, int RegNum,
238       DebugLoc DL);
239   void insertCondBranchEnd(MachineBasicBlock *MBB, int NewOpcode, int RegNum);
240   static int getBranchNzeroOpcode(int OldOpcode);
241   static int getBranchZeroOpcode(int OldOpcode);
242   static int getContinueNzeroOpcode(int OldOpcode);
243   static int getContinueZeroOpcode(int OldOpcode);
244   static MachineBasicBlock *getTrueBranch(MachineInstr *MI);
245   static void setTrueBranch(MachineInstr *MI, MachineBasicBlock *MBB);
246   static MachineBasicBlock *getFalseBranch(MachineBasicBlock *MBB,
247       MachineInstr *MI);
248   static bool isCondBranch(MachineInstr *MI);
249   static bool isUncondBranch(MachineInstr *MI);
250   static DebugLoc getLastDebugLocInBB(MachineBasicBlock *MBB);
251   static MachineInstr *getNormalBlockBranchInstr(MachineBasicBlock *MBB);
252   /// The correct naming for this is getPossibleLoopendBlockBranchInstr.
253   ///
254   /// BB with backward-edge could have move instructions after the branch
255   /// instruction.  Such move instruction "belong to" the loop backward-edge.
256   MachineInstr *getLoopendBlockBranchInstr(MachineBasicBlock *MBB);
257   static MachineInstr *getReturnInstr(MachineBasicBlock *MBB);
258   static MachineInstr *getContinueInstr(MachineBasicBlock *MBB);
259   static bool isReturnBlock(MachineBasicBlock *MBB);
260   static void cloneSuccessorList(MachineBasicBlock *DstMBB,
261       MachineBasicBlock *SrcMBB) ;
262   static MachineBasicBlock *clone(MachineBasicBlock *MBB);
263   /// MachineBasicBlock::ReplaceUsesOfBlockWith doesn't serve the purpose
264   /// because the AMDGPU instruction is not recognized as terminator fix this
265   /// and retire this routine
266   void replaceInstrUseOfBlockWith(MachineBasicBlock *SrcMBB,
267       MachineBasicBlock *OldMBB, MachineBasicBlock *NewBlk);
268   static void wrapup(MachineBasicBlock *MBB);
269
270
271   int patternMatch(MachineBasicBlock *MBB);
272   int patternMatchGroup(MachineBasicBlock *MBB);
273   int serialPatternMatch(MachineBasicBlock *MBB);
274   int ifPatternMatch(MachineBasicBlock *MBB);
275   int loopendPatternMatch();
276   int mergeLoop(MachineLoop *LoopRep);
277   int loopcontPatternMatch(MachineLoop *LoopRep, MachineBasicBlock *LoopHeader);
278
279   void handleLoopcontBlock(MachineBasicBlock *ContingMBB,
280       MachineLoop *ContingLoop, MachineBasicBlock *ContMBB,
281       MachineLoop *ContLoop);
282   /// return true iff src1Blk->succ_size() == 0 && src1Blk and src2Blk are in
283   /// the same loop with LoopLandInfo without explicitly keeping track of
284   /// loopContBlks and loopBreakBlks, this is a method to get the information.
285   bool isSameloopDetachedContbreak(MachineBasicBlock *Src1MBB,
286       MachineBasicBlock *Src2MBB);
287   int handleJumpintoIf(MachineBasicBlock *HeadMBB,
288       MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
289   int handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
290       MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
291   int improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
292       MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
293       MachineBasicBlock **LandMBBPtr);
294   void showImproveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
295       MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
296       MachineBasicBlock *LandMBB, bool Detail = false);
297   int cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
298       MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB);
299   void mergeSerialBlock(MachineBasicBlock *DstMBB,
300       MachineBasicBlock *SrcMBB);
301
302   void mergeIfthenelseBlock(MachineInstr *BranchMI,
303       MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
304       MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB);
305   void mergeLooplandBlock(MachineBasicBlock *DstMBB,
306       MachineBasicBlock *LandMBB);
307   void mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
308       MachineBasicBlock *LandMBB);
309   void settleLoopcontBlock(MachineBasicBlock *ContingMBB,
310       MachineBasicBlock *ContMBB);
311   /// normalizeInfiniteLoopExit change
312   ///   B1:
313   ///        uncond_br LoopHeader
314   ///
315   /// to
316   ///   B1:
317   ///        cond_br 1 LoopHeader dummyExit
318   /// and return the newly added dummy exit block
319   MachineBasicBlock *normalizeInfiniteLoopExit(MachineLoop *LoopRep);
320   void removeUnconditionalBranch(MachineBasicBlock *MBB);
321   /// Remove duplicate branches instructions in a block.
322   /// For instance
323   /// B0:
324   ///    cond_br X B1 B2
325   ///    cond_br X B1 B2
326   /// is transformed to
327   /// B0:
328   ///    cond_br X B1 B2
329   void removeRedundantConditionalBranch(MachineBasicBlock *MBB);
330   void addDummyExitBlock(SmallVectorImpl<MachineBasicBlock *> &RetMBB);
331   void removeSuccessor(MachineBasicBlock *MBB);
332   MachineBasicBlock *cloneBlockForPredecessor(MachineBasicBlock *MBB,
333       MachineBasicBlock *PredMBB);
334   void migrateInstruction(MachineBasicBlock *SrcMBB,
335       MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I);
336   void recordSccnum(MachineBasicBlock *MBB, int SCCNum);
337   void retireBlock(MachineBasicBlock *MBB);
338   void setLoopLandBlock(MachineLoop *LoopRep, MachineBasicBlock *MBB = nullptr);
339
340   MachineBasicBlock *findNearestCommonPostDom(std::set<MachineBasicBlock *>&);
341   /// This is work around solution for findNearestCommonDominator not available
342   /// to post dom a proper fix should go to Dominators.h.
343   MachineBasicBlock *findNearestCommonPostDom(MachineBasicBlock *MBB1,
344       MachineBasicBlock *MBB2);
345
346 private:
347   MBBInfoMap BlockInfoMap;
348   LoopLandInfoMap LLInfoMap;
349   std::map<MachineLoop *, bool> Visited;
350   MachineFunction *FuncRep;
351   SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> OrderedBlks;
352 };
353
354 int AMDGPUCFGStructurizer::getSCCNum(MachineBasicBlock *MBB) const {
355   MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
356   if (It == BlockInfoMap.end())
357     return INVALIDSCCNUM;
358   return (*It).second->SccNum;
359 }
360
361 MachineBasicBlock *AMDGPUCFGStructurizer::getLoopLandInfo(MachineLoop *LoopRep)
362     const {
363   LoopLandInfoMap::const_iterator It = LLInfoMap.find(LoopRep);
364   if (It == LLInfoMap.end())
365     return nullptr;
366   return (*It).second;
367 }
368
369 bool AMDGPUCFGStructurizer::hasBackEdge(MachineBasicBlock *MBB) const {
370   MachineLoop *LoopRep = MLI->getLoopFor(MBB);
371   if (!LoopRep)
372     return false;
373   MachineBasicBlock *LoopHeader = LoopRep->getHeader();
374   return MBB->isSuccessor(LoopHeader);
375 }
376
377 unsigned AMDGPUCFGStructurizer::getLoopDepth(MachineLoop *LoopRep) {
378   return LoopRep ? LoopRep->getLoopDepth() : 0;
379 }
380
381 bool AMDGPUCFGStructurizer::isRetiredBlock(MachineBasicBlock *MBB) const {
382   MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
383   if (It == BlockInfoMap.end())
384     return false;
385   return (*It).second->IsRetired;
386 }
387
388 bool AMDGPUCFGStructurizer::isActiveLoophead(MachineBasicBlock *MBB) const {
389   MachineLoop *LoopRep = MLI->getLoopFor(MBB);
390   while (LoopRep && LoopRep->getHeader() == MBB) {
391     MachineBasicBlock *LoopLand = getLoopLandInfo(LoopRep);
392     if(!LoopLand)
393       return true;
394     if (!isRetiredBlock(LoopLand))
395       return true;
396     LoopRep = LoopRep->getParentLoop();
397   }
398   return false;
399 }
400 AMDGPUCFGStructurizer::PathToKind AMDGPUCFGStructurizer::singlePathTo(
401     MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
402     bool AllowSideEntry) const {
403   assert(DstMBB);
404   if (SrcMBB == DstMBB)
405     return SinglePath_InPath;
406   while (SrcMBB && SrcMBB->succ_size() == 1) {
407     SrcMBB = *SrcMBB->succ_begin();
408     if (SrcMBB == DstMBB)
409       return SinglePath_InPath;
410     if (!AllowSideEntry && SrcMBB->pred_size() > 1)
411       return Not_SinglePath;
412   }
413   if (SrcMBB && SrcMBB->succ_size()==0)
414     return SinglePath_NotInPath;
415   return Not_SinglePath;
416 }
417
418 int AMDGPUCFGStructurizer::countActiveBlock(MBBVector::const_iterator It,
419     MBBVector::const_iterator E) const {
420   int Count = 0;
421   while (It != E) {
422     if (!isRetiredBlock(*It))
423       ++Count;
424     ++It;
425   }
426   return Count;
427 }
428
429 bool AMDGPUCFGStructurizer::needMigrateBlock(MachineBasicBlock *MBB) const {
430   unsigned BlockSizeThreshold = 30;
431   unsigned CloneInstrThreshold = 100;
432   bool MultiplePreds = MBB && (MBB->pred_size() > 1);
433
434   if(!MultiplePreds)
435     return false;
436   unsigned BlkSize = MBB->size();
437   return ((BlkSize > BlockSizeThreshold) &&
438       (BlkSize * (MBB->pred_size() - 1) > CloneInstrThreshold));
439 }
440
441 void AMDGPUCFGStructurizer::reversePredicateSetter(
442     MachineBasicBlock::iterator I) {
443   while (I--) {
444     if (I->getOpcode() == AMDGPU::PRED_X) {
445       switch (static_cast<MachineInstr *>(I)->getOperand(2).getImm()) {
446       case OPCODE_IS_ZERO_INT:
447         static_cast<MachineInstr *>(I)->getOperand(2)
448             .setImm(OPCODE_IS_NOT_ZERO_INT);
449         return;
450       case OPCODE_IS_NOT_ZERO_INT:
451         static_cast<MachineInstr *>(I)->getOperand(2)
452             .setImm(OPCODE_IS_ZERO_INT);
453         return;
454       case OPCODE_IS_ZERO:
455         static_cast<MachineInstr *>(I)->getOperand(2)
456             .setImm(OPCODE_IS_NOT_ZERO);
457         return;
458       case OPCODE_IS_NOT_ZERO:
459         static_cast<MachineInstr *>(I)->getOperand(2)
460             .setImm(OPCODE_IS_ZERO);
461         return;
462       default:
463         llvm_unreachable("PRED_X Opcode invalid!");
464       }
465     }
466   }
467 }
468
469 void AMDGPUCFGStructurizer::insertInstrEnd(MachineBasicBlock *MBB,
470     int NewOpcode, DebugLoc DL) {
471  MachineInstr *MI = MBB->getParent()
472     ->CreateMachineInstr(TII->get(NewOpcode), DL);
473   MBB->push_back(MI);
474   //assume the instruction doesn't take any reg operand ...
475   SHOWNEWINSTR(MI);
476 }
477
478 MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(MachineBasicBlock *MBB,
479     int NewOpcode, DebugLoc DL) {
480   MachineInstr *MI =
481       MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
482   if (MBB->begin() != MBB->end())
483     MBB->insert(MBB->begin(), MI);
484   else
485     MBB->push_back(MI);
486   SHOWNEWINSTR(MI);
487   return MI;
488 }
489
490 MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(
491     MachineBasicBlock::iterator I, int NewOpcode) {
492   MachineInstr *OldMI = &(*I);
493   MachineBasicBlock *MBB = OldMI->getParent();
494   MachineInstr *NewMBB =
495       MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DebugLoc());
496   MBB->insert(I, NewMBB);
497   //assume the instruction doesn't take any reg operand ...
498   SHOWNEWINSTR(NewMBB);
499   return NewMBB;
500 }
501
502 void AMDGPUCFGStructurizer::insertCondBranchBefore(
503     MachineBasicBlock::iterator I, int NewOpcode, DebugLoc DL) {
504   MachineInstr *OldMI = &(*I);
505   MachineBasicBlock *MBB = OldMI->getParent();
506   MachineFunction *MF = MBB->getParent();
507   MachineInstr *NewMI = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
508   MBB->insert(I, NewMI);
509   MachineInstrBuilder MIB(*MF, NewMI);
510   MIB.addReg(OldMI->getOperand(1).getReg(), false);
511   SHOWNEWINSTR(NewMI);
512   //erase later oldInstr->eraseFromParent();
513 }
514
515 void AMDGPUCFGStructurizer::insertCondBranchBefore(MachineBasicBlock *blk,
516     MachineBasicBlock::iterator I, int NewOpcode, int RegNum,
517     DebugLoc DL) {
518   MachineFunction *MF = blk->getParent();
519   MachineInstr *NewInstr = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
520   //insert before
521   blk->insert(I, NewInstr);
522   MachineInstrBuilder(*MF, NewInstr).addReg(RegNum, false);
523   SHOWNEWINSTR(NewInstr);
524 }
525
526 void AMDGPUCFGStructurizer::insertCondBranchEnd(MachineBasicBlock *MBB,
527     int NewOpcode, int RegNum) {
528   MachineFunction *MF = MBB->getParent();
529   MachineInstr *NewInstr =
530     MF->CreateMachineInstr(TII->get(NewOpcode), DebugLoc());
531   MBB->push_back(NewInstr);
532   MachineInstrBuilder(*MF, NewInstr).addReg(RegNum, false);
533   SHOWNEWINSTR(NewInstr);
534 }
535
536 int AMDGPUCFGStructurizer::getBranchNzeroOpcode(int OldOpcode) {
537   switch(OldOpcode) {
538   case AMDGPU::JUMP_COND:
539   case AMDGPU::JUMP: return AMDGPU::IF_PREDICATE_SET;
540   case AMDGPU::BRANCH_COND_i32:
541   case AMDGPU::BRANCH_COND_f32: return AMDGPU::IF_LOGICALNZ_f32;
542   default: llvm_unreachable("internal error");
543   }
544   return -1;
545 }
546
547 int AMDGPUCFGStructurizer::getBranchZeroOpcode(int OldOpcode) {
548   switch(OldOpcode) {
549   case AMDGPU::JUMP_COND:
550   case AMDGPU::JUMP: return AMDGPU::IF_PREDICATE_SET;
551   case AMDGPU::BRANCH_COND_i32:
552   case AMDGPU::BRANCH_COND_f32: return AMDGPU::IF_LOGICALZ_f32;
553   default: llvm_unreachable("internal error");
554   }
555   return -1;
556 }
557
558 int AMDGPUCFGStructurizer::getContinueNzeroOpcode(int OldOpcode) {
559   switch(OldOpcode) {
560   case AMDGPU::JUMP_COND:
561   case AMDGPU::JUMP: return AMDGPU::CONTINUE_LOGICALNZ_i32;
562   default: llvm_unreachable("internal error");
563   };
564   return -1;
565 }
566
567 int AMDGPUCFGStructurizer::getContinueZeroOpcode(int OldOpcode) {
568   switch(OldOpcode) {
569   case AMDGPU::JUMP_COND:
570   case AMDGPU::JUMP: return AMDGPU::CONTINUE_LOGICALZ_i32;
571   default: llvm_unreachable("internal error");
572   }
573   return -1;
574 }
575
576 MachineBasicBlock *AMDGPUCFGStructurizer::getTrueBranch(MachineInstr *MI) {
577   return MI->getOperand(0).getMBB();
578 }
579
580 void AMDGPUCFGStructurizer::setTrueBranch(MachineInstr *MI,
581     MachineBasicBlock *MBB) {
582   MI->getOperand(0).setMBB(MBB);
583 }
584
585 MachineBasicBlock *
586 AMDGPUCFGStructurizer::getFalseBranch(MachineBasicBlock *MBB,
587     MachineInstr *MI) {
588   assert(MBB->succ_size() == 2);
589   MachineBasicBlock *TrueBranch = getTrueBranch(MI);
590   MachineBasicBlock::succ_iterator It = MBB->succ_begin();
591   MachineBasicBlock::succ_iterator Next = It;
592   ++Next;
593   return (*It == TrueBranch) ? *Next : *It;
594 }
595
596 bool AMDGPUCFGStructurizer::isCondBranch(MachineInstr *MI) {
597   switch (MI->getOpcode()) {
598     case AMDGPU::JUMP_COND:
599     case AMDGPU::BRANCH_COND_i32:
600     case AMDGPU::BRANCH_COND_f32: return true;
601   default:
602     return false;
603   }
604   return false;
605 }
606
607 bool AMDGPUCFGStructurizer::isUncondBranch(MachineInstr *MI) {
608   switch (MI->getOpcode()) {
609   case AMDGPU::JUMP:
610   case AMDGPU::BRANCH:
611     return true;
612   default:
613     return false;
614   }
615   return false;
616 }
617
618 DebugLoc AMDGPUCFGStructurizer::getLastDebugLocInBB(MachineBasicBlock *MBB) {
619   //get DebugLoc from the first MachineBasicBlock instruction with debug info
620   DebugLoc DL;
621   for (MachineBasicBlock::iterator It = MBB->begin(); It != MBB->end();
622       ++It) {
623     MachineInstr *instr = &(*It);
624     if (instr->getDebugLoc().isUnknown() == false)
625       DL = instr->getDebugLoc();
626   }
627   return DL;
628 }
629
630 MachineInstr *AMDGPUCFGStructurizer::getNormalBlockBranchInstr(
631     MachineBasicBlock *MBB) {
632   MachineBasicBlock::reverse_iterator It = MBB->rbegin();
633   MachineInstr *MI = &*It;
634   if (MI && (isCondBranch(MI) || isUncondBranch(MI)))
635     return MI;
636   return nullptr;
637 }
638
639 MachineInstr *AMDGPUCFGStructurizer::getLoopendBlockBranchInstr(
640     MachineBasicBlock *MBB) {
641   for (MachineBasicBlock::reverse_iterator It = MBB->rbegin(), E = MBB->rend();
642       It != E; ++It) {
643     // FIXME: Simplify
644     MachineInstr *MI = &*It;
645     if (MI) {
646       if (isCondBranch(MI) || isUncondBranch(MI))
647         return MI;
648       else if (!TII->isMov(MI->getOpcode()))
649         break;
650     }
651   }
652   return nullptr;
653 }
654
655 MachineInstr *AMDGPUCFGStructurizer::getReturnInstr(MachineBasicBlock *MBB) {
656   MachineBasicBlock::reverse_iterator It = MBB->rbegin();
657   if (It != MBB->rend()) {
658     MachineInstr *instr = &(*It);
659     if (instr->getOpcode() == AMDGPU::RETURN)
660       return instr;
661   }
662   return nullptr;
663 }
664
665 MachineInstr *AMDGPUCFGStructurizer::getContinueInstr(MachineBasicBlock *MBB) {
666   MachineBasicBlock::reverse_iterator It = MBB->rbegin();
667   if (It != MBB->rend()) {
668     MachineInstr *MI = &(*It);
669     if (MI->getOpcode() == AMDGPU::CONTINUE)
670       return MI;
671   }
672   return nullptr;
673 }
674
675 bool AMDGPUCFGStructurizer::isReturnBlock(MachineBasicBlock *MBB) {
676   MachineInstr *MI = getReturnInstr(MBB);
677   bool IsReturn = (MBB->succ_size() == 0);
678   if (MI)
679     assert(IsReturn);
680   else if (IsReturn)
681     DEBUG(
682       dbgs() << "BB" << MBB->getNumber()
683              <<" is return block without RETURN instr\n";);
684   return  IsReturn;
685 }
686
687 void AMDGPUCFGStructurizer::cloneSuccessorList(MachineBasicBlock *DstMBB,
688     MachineBasicBlock *SrcMBB) {
689   for (MachineBasicBlock::succ_iterator It = SrcMBB->succ_begin(),
690        iterEnd = SrcMBB->succ_end(); It != iterEnd; ++It)
691     DstMBB->addSuccessor(*It);  // *iter's predecessor is also taken care of
692 }
693
694 MachineBasicBlock *AMDGPUCFGStructurizer::clone(MachineBasicBlock *MBB) {
695   MachineFunction *Func = MBB->getParent();
696   MachineBasicBlock *NewMBB = Func->CreateMachineBasicBlock();
697   Func->push_back(NewMBB);  //insert to function
698   for (MachineBasicBlock::iterator It = MBB->begin(), E = MBB->end();
699       It != E; ++It) {
700     MachineInstr *MI = Func->CloneMachineInstr(It);
701     NewMBB->push_back(MI);
702   }
703   return NewMBB;
704 }
705
706 void AMDGPUCFGStructurizer::replaceInstrUseOfBlockWith(
707     MachineBasicBlock *SrcMBB, MachineBasicBlock *OldMBB,
708     MachineBasicBlock *NewBlk) {
709   MachineInstr *BranchMI = getLoopendBlockBranchInstr(SrcMBB);
710   if (BranchMI && isCondBranch(BranchMI) &&
711       getTrueBranch(BranchMI) == OldMBB)
712     setTrueBranch(BranchMI, NewBlk);
713 }
714
715 void AMDGPUCFGStructurizer::wrapup(MachineBasicBlock *MBB) {
716   assert((!MBB->getParent()->getJumpTableInfo()
717           || MBB->getParent()->getJumpTableInfo()->isEmpty())
718          && "found a jump table");
719
720    //collect continue right before endloop
721    SmallVector<MachineInstr *, DEFAULT_VEC_SLOTS> ContInstr;
722    MachineBasicBlock::iterator Pre = MBB->begin();
723    MachineBasicBlock::iterator E = MBB->end();
724    MachineBasicBlock::iterator It = Pre;
725    while (It != E) {
726      if (Pre->getOpcode() == AMDGPU::CONTINUE
727          && It->getOpcode() == AMDGPU::ENDLOOP)
728        ContInstr.push_back(Pre);
729      Pre = It;
730      ++It;
731    }
732
733    //delete continue right before endloop
734    for (unsigned i = 0; i < ContInstr.size(); ++i)
735       ContInstr[i]->eraseFromParent();
736
737    // TODO to fix up jump table so later phase won't be confused.  if
738    // (jumpTableInfo->isEmpty() == false) { need to clean the jump table, but
739    // there isn't such an interface yet.  alternatively, replace all the other
740    // blocks in the jump table with the entryBlk //}
741
742 }
743
744
745 bool AMDGPUCFGStructurizer::prepare() {
746   bool Changed = false;
747
748   //FIXME: if not reducible flow graph, make it so ???
749
750   DEBUG(dbgs() << "AMDGPUCFGStructurizer::prepare\n";);
751
752   orderBlocks(FuncRep);
753
754   SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> RetBlks;
755
756   // Add an ExitBlk to loop that don't have one
757   for (MachineLoopInfo::iterator It = MLI->begin(),
758        E = MLI->end(); It != E; ++It) {
759     MachineLoop *LoopRep = (*It);
760     MBBVector ExitingMBBs;
761     LoopRep->getExitingBlocks(ExitingMBBs);
762
763     if (ExitingMBBs.size() == 0) {
764       MachineBasicBlock* DummyExitBlk = normalizeInfiniteLoopExit(LoopRep);
765       if (DummyExitBlk)
766         RetBlks.push_back(DummyExitBlk);
767     }
768   }
769
770   // Remove unconditional branch instr.
771   // Add dummy exit block iff there are multiple returns.
772   for (SmallVectorImpl<MachineBasicBlock *>::const_iterator
773        It = OrderedBlks.begin(), E = OrderedBlks.end(); It != E; ++It) {
774     MachineBasicBlock *MBB = *It;
775     removeUnconditionalBranch(MBB);
776     removeRedundantConditionalBranch(MBB);
777     if (isReturnBlock(MBB)) {
778       RetBlks.push_back(MBB);
779     }
780     assert(MBB->succ_size() <= 2);
781   }
782
783   if (RetBlks.size() >= 2) {
784     addDummyExitBlock(RetBlks);
785     Changed = true;
786   }
787
788   return Changed;
789 }
790
791 bool AMDGPUCFGStructurizer::run() {
792
793   //Assume reducible CFG...
794   DEBUG(dbgs() << "AMDGPUCFGStructurizer::run\n");
795
796 #ifdef STRESSTEST
797   //Use the worse block ordering to test the algorithm.
798   ReverseVector(orderedBlks);
799 #endif
800
801   DEBUG(dbgs() << "Ordered blocks:\n"; printOrderedBlocks(););
802   int NumIter = 0;
803   bool Finish = false;
804   MachineBasicBlock *MBB;
805   bool MakeProgress = false;
806   int NumRemainedBlk = countActiveBlock(OrderedBlks.begin(),
807                                         OrderedBlks.end());
808
809   do {
810     ++NumIter;
811     DEBUG(
812       dbgs() << "numIter = " << NumIter
813              << ", numRemaintedBlk = " << NumRemainedBlk << "\n";
814     );
815
816     SmallVectorImpl<MachineBasicBlock *>::const_iterator It =
817         OrderedBlks.begin();
818     SmallVectorImpl<MachineBasicBlock *>::const_iterator E =
819         OrderedBlks.end();
820
821     SmallVectorImpl<MachineBasicBlock *>::const_iterator SccBeginIter =
822         It;
823     MachineBasicBlock *SccBeginMBB = nullptr;
824     int SccNumBlk = 0;  // The number of active blocks, init to a
825                         // maximum possible number.
826     int SccNumIter;     // Number of iteration in this SCC.
827
828     while (It != E) {
829       MBB = *It;
830
831       if (!SccBeginMBB) {
832         SccBeginIter = It;
833         SccBeginMBB = MBB;
834         SccNumIter = 0;
835         SccNumBlk = NumRemainedBlk; // Init to maximum possible number.
836         DEBUG(
837               dbgs() << "start processing SCC" << getSCCNum(SccBeginMBB);
838               dbgs() << "\n";
839         );
840       }
841
842       if (!isRetiredBlock(MBB))
843         patternMatch(MBB);
844
845       ++It;
846
847       bool ContNextScc = true;
848       if (It == E
849           || getSCCNum(SccBeginMBB) != getSCCNum(*It)) {
850         // Just finish one scc.
851         ++SccNumIter;
852         int sccRemainedNumBlk = countActiveBlock(SccBeginIter, It);
853         if (sccRemainedNumBlk != 1 && sccRemainedNumBlk >= SccNumBlk) {
854           DEBUG(
855             dbgs() << "Can't reduce SCC " << getSCCNum(MBB)
856                    << ", sccNumIter = " << SccNumIter;
857             dbgs() << "doesn't make any progress\n";
858           );
859           ContNextScc = true;
860         } else if (sccRemainedNumBlk != 1 && sccRemainedNumBlk < SccNumBlk) {
861           SccNumBlk = sccRemainedNumBlk;
862           It = SccBeginIter;
863           ContNextScc = false;
864           DEBUG(
865             dbgs() << "repeat processing SCC" << getSCCNum(MBB)
866                    << "sccNumIter = " << SccNumIter << '\n';
867           );
868         } else {
869           // Finish the current scc.
870           ContNextScc = true;
871         }
872       } else {
873         // Continue on next component in the current scc.
874         ContNextScc = false;
875       }
876
877       if (ContNextScc)
878         SccBeginMBB = nullptr;
879     } //while, "one iteration" over the function.
880
881     MachineBasicBlock *EntryMBB =
882         GraphTraits<MachineFunction *>::nodes_begin(FuncRep);
883     if (EntryMBB->succ_size() == 0) {
884       Finish = true;
885       DEBUG(
886         dbgs() << "Reduce to one block\n";
887       );
888     } else {
889       int NewnumRemainedBlk
890         = countActiveBlock(OrderedBlks.begin(), OrderedBlks.end());
891       // consider cloned blocks ??
892       if (NewnumRemainedBlk == 1 || NewnumRemainedBlk < NumRemainedBlk) {
893         MakeProgress = true;
894         NumRemainedBlk = NewnumRemainedBlk;
895       } else {
896         MakeProgress = false;
897         DEBUG(
898           dbgs() << "No progress\n";
899         );
900       }
901     }
902   } while (!Finish && MakeProgress);
903
904   // Misc wrap up to maintain the consistency of the Function representation.
905   wrapup(GraphTraits<MachineFunction *>::nodes_begin(FuncRep));
906
907   // Detach retired Block, release memory.
908   for (MBBInfoMap::iterator It = BlockInfoMap.begin(), E = BlockInfoMap.end();
909       It != E; ++It) {
910     if ((*It).second && (*It).second->IsRetired) {
911       assert(((*It).first)->getNumber() != -1);
912       DEBUG(
913         dbgs() << "Erase BB" << ((*It).first)->getNumber() << "\n";
914       );
915       (*It).first->eraseFromParent();  //Remove from the parent Function.
916     }
917     delete (*It).second;
918   }
919   BlockInfoMap.clear();
920   LLInfoMap.clear();
921
922   if (!Finish) {
923     DEBUG(FuncRep->viewCFG());
924     llvm_unreachable("IRREDUCIBLE_CFG");
925   }
926
927   return true;
928 }
929
930
931
932 void AMDGPUCFGStructurizer::orderBlocks(MachineFunction *MF) {
933   int SccNum = 0;
934   MachineBasicBlock *MBB;
935   for (scc_iterator<MachineFunction *> It = scc_begin(MF); !It.isAtEnd();
936        ++It, ++SccNum) {
937     const std::vector<MachineBasicBlock *> &SccNext = *It;
938     for (std::vector<MachineBasicBlock *>::const_iterator
939          blockIter = SccNext.begin(), blockEnd = SccNext.end();
940          blockIter != blockEnd; ++blockIter) {
941       MBB = *blockIter;
942       OrderedBlks.push_back(MBB);
943       recordSccnum(MBB, SccNum);
944     }
945   }
946
947   //walk through all the block in func to check for unreachable
948   typedef GraphTraits<MachineFunction *> GTM;
949   MachineFunction::iterator It = GTM::nodes_begin(MF), E = GTM::nodes_end(MF);
950   for (; It != E; ++It) {
951     MachineBasicBlock *MBB = &(*It);
952     SccNum = getSCCNum(MBB);
953     if (SccNum == INVALIDSCCNUM)
954       dbgs() << "unreachable block BB" << MBB->getNumber() << "\n";
955   }
956 }
957
958 int AMDGPUCFGStructurizer::patternMatch(MachineBasicBlock *MBB) {
959   int NumMatch = 0;
960   int CurMatch;
961
962   DEBUG(
963         dbgs() << "Begin patternMatch BB" << MBB->getNumber() << "\n";
964   );
965
966   while ((CurMatch = patternMatchGroup(MBB)) > 0)
967     NumMatch += CurMatch;
968
969   DEBUG(
970         dbgs() << "End patternMatch BB" << MBB->getNumber()
971       << ", numMatch = " << NumMatch << "\n";
972   );
973
974   return NumMatch;
975 }
976
977 int AMDGPUCFGStructurizer::patternMatchGroup(MachineBasicBlock *MBB) {
978   int NumMatch = 0;
979   NumMatch += loopendPatternMatch();
980   NumMatch += serialPatternMatch(MBB);
981   NumMatch += ifPatternMatch(MBB);
982   return NumMatch;
983 }
984
985
986 int AMDGPUCFGStructurizer::serialPatternMatch(MachineBasicBlock *MBB) {
987   if (MBB->succ_size() != 1)
988     return 0;
989
990   MachineBasicBlock *childBlk = *MBB->succ_begin();
991   if (childBlk->pred_size() != 1 || isActiveLoophead(childBlk))
992     return 0;
993
994   mergeSerialBlock(MBB, childBlk);
995   ++numSerialPatternMatch;
996   return 1;
997 }
998
999 int AMDGPUCFGStructurizer::ifPatternMatch(MachineBasicBlock *MBB) {
1000   //two edges
1001   if (MBB->succ_size() != 2)
1002     return 0;
1003   if (hasBackEdge(MBB))
1004     return 0;
1005   MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
1006   if (!BranchMI)
1007     return 0;
1008
1009   assert(isCondBranch(BranchMI));
1010   int NumMatch = 0;
1011
1012   MachineBasicBlock *TrueMBB = getTrueBranch(BranchMI);
1013   NumMatch += serialPatternMatch(TrueMBB);
1014   NumMatch += ifPatternMatch(TrueMBB);
1015   MachineBasicBlock *FalseMBB = getFalseBranch(MBB, BranchMI);
1016   NumMatch += serialPatternMatch(FalseMBB);
1017   NumMatch += ifPatternMatch(FalseMBB);
1018   MachineBasicBlock *LandBlk;
1019   int Cloned = 0;
1020
1021   assert (!TrueMBB->succ_empty() || !FalseMBB->succ_empty());
1022   // TODO: Simplify
1023   if (TrueMBB->succ_size() == 1 && FalseMBB->succ_size() == 1
1024     && *TrueMBB->succ_begin() == *FalseMBB->succ_begin()) {
1025     // Diamond pattern
1026     LandBlk = *TrueMBB->succ_begin();
1027   } else if (TrueMBB->succ_size() == 1 && *TrueMBB->succ_begin() == FalseMBB) {
1028     // Triangle pattern, false is empty
1029     LandBlk = FalseMBB;
1030     FalseMBB = nullptr;
1031   } else if (FalseMBB->succ_size() == 1
1032              && *FalseMBB->succ_begin() == TrueMBB) {
1033     // Triangle pattern, true is empty
1034     // We reverse the predicate to make a triangle, empty false pattern;
1035     std::swap(TrueMBB, FalseMBB);
1036     reversePredicateSetter(MBB->end());
1037     LandBlk = FalseMBB;
1038     FalseMBB = nullptr;
1039   } else if (FalseMBB->succ_size() == 1
1040              && isSameloopDetachedContbreak(TrueMBB, FalseMBB)) {
1041     LandBlk = *FalseMBB->succ_begin();
1042   } else if (TrueMBB->succ_size() == 1
1043     && isSameloopDetachedContbreak(FalseMBB, TrueMBB)) {
1044     LandBlk = *TrueMBB->succ_begin();
1045   } else {
1046     return NumMatch + handleJumpintoIf(MBB, TrueMBB, FalseMBB);
1047   }
1048
1049   // improveSimpleJumpinfoIf can handle the case where landBlk == NULL but the
1050   // new BB created for landBlk==NULL may introduce new challenge to the
1051   // reduction process.
1052   if (LandBlk &&
1053       ((TrueMBB && TrueMBB->pred_size() > 1)
1054       || (FalseMBB && FalseMBB->pred_size() > 1))) {
1055      Cloned += improveSimpleJumpintoIf(MBB, TrueMBB, FalseMBB, &LandBlk);
1056   }
1057
1058   if (TrueMBB && TrueMBB->pred_size() > 1) {
1059     TrueMBB = cloneBlockForPredecessor(TrueMBB, MBB);
1060     ++Cloned;
1061   }
1062
1063   if (FalseMBB && FalseMBB->pred_size() > 1) {
1064     FalseMBB = cloneBlockForPredecessor(FalseMBB, MBB);
1065     ++Cloned;
1066   }
1067
1068   mergeIfthenelseBlock(BranchMI, MBB, TrueMBB, FalseMBB, LandBlk);
1069
1070   ++numIfPatternMatch;
1071
1072   numClonedBlock += Cloned;
1073
1074   return 1 + Cloned + NumMatch;
1075 }
1076
1077 int AMDGPUCFGStructurizer::loopendPatternMatch() {
1078   std::vector<MachineLoop *> NestedLoops;
1079   for (MachineLoopInfo::iterator It = MLI->begin(), E = MLI->end(); It != E;
1080        ++It)
1081     for (MachineLoop *ML : depth_first(*It))
1082       NestedLoops.push_back(ML);
1083
1084   if (NestedLoops.size() == 0)
1085     return 0;
1086
1087   // Process nested loop outside->inside, so "continue" to a outside loop won't
1088   // be mistaken as "break" of the current loop.
1089   int Num = 0;
1090   for (std::vector<MachineLoop *>::reverse_iterator It = NestedLoops.rbegin(),
1091       E = NestedLoops.rend(); It != E; ++It) {
1092     MachineLoop *ExaminedLoop = *It;
1093     if (ExaminedLoop->getNumBlocks() == 0 || Visited[ExaminedLoop])
1094       continue;
1095     DEBUG(dbgs() << "Processing:\n"; ExaminedLoop->dump(););
1096     int NumBreak = mergeLoop(ExaminedLoop);
1097     if (NumBreak == -1)
1098       break;
1099     Num += NumBreak;
1100   }
1101   return Num;
1102 }
1103
1104 int AMDGPUCFGStructurizer::mergeLoop(MachineLoop *LoopRep) {
1105   MachineBasicBlock *LoopHeader = LoopRep->getHeader();
1106   MBBVector ExitingMBBs;
1107   LoopRep->getExitingBlocks(ExitingMBBs);
1108   assert(!ExitingMBBs.empty() && "Infinite Loop not supported");
1109   DEBUG(dbgs() << "Loop has " << ExitingMBBs.size() << " exiting blocks\n";);
1110   // We assume a single ExitBlk
1111   MBBVector ExitBlks;
1112   LoopRep->getExitBlocks(ExitBlks);
1113   SmallPtrSet<MachineBasicBlock *, 2> ExitBlkSet;
1114   for (unsigned i = 0, e = ExitBlks.size(); i < e; ++i)
1115     ExitBlkSet.insert(ExitBlks[i]);
1116   assert(ExitBlkSet.size() == 1);
1117   MachineBasicBlock *ExitBlk = *ExitBlks.begin();
1118   assert(ExitBlk && "Loop has several exit block");
1119   MBBVector LatchBlks;
1120   typedef GraphTraits<Inverse<MachineBasicBlock*> > InvMBBTraits;
1121   InvMBBTraits::ChildIteratorType PI = InvMBBTraits::child_begin(LoopHeader),
1122       PE = InvMBBTraits::child_end(LoopHeader);
1123   for (; PI != PE; PI++) {
1124     if (LoopRep->contains(*PI))
1125       LatchBlks.push_back(*PI);
1126   }
1127
1128   for (unsigned i = 0, e = ExitingMBBs.size(); i < e; ++i)
1129     mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk);
1130   for (unsigned i = 0, e = LatchBlks.size(); i < e; ++i)
1131     settleLoopcontBlock(LatchBlks[i], LoopHeader);
1132   int Match = 0;
1133   do {
1134     Match = 0;
1135     Match += serialPatternMatch(LoopHeader);
1136     Match += ifPatternMatch(LoopHeader);
1137   } while (Match > 0);
1138   mergeLooplandBlock(LoopHeader, ExitBlk);
1139   MachineLoop *ParentLoop = LoopRep->getParentLoop();
1140   if (ParentLoop)
1141     MLI->changeLoopFor(LoopHeader, ParentLoop);
1142   else
1143     MLI->removeBlock(LoopHeader);
1144   Visited[LoopRep] = true;
1145   return 1;
1146 }
1147
1148 int AMDGPUCFGStructurizer::loopcontPatternMatch(MachineLoop *LoopRep,
1149     MachineBasicBlock *LoopHeader) {
1150   int NumCont = 0;
1151   SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> ContMBB;
1152   typedef GraphTraits<Inverse<MachineBasicBlock *> > GTIM;
1153   GTIM::ChildIteratorType It = GTIM::child_begin(LoopHeader),
1154       E = GTIM::child_end(LoopHeader);
1155   for (; It != E; ++It) {
1156     MachineBasicBlock *MBB = *It;
1157     if (LoopRep->contains(MBB)) {
1158       handleLoopcontBlock(MBB, MLI->getLoopFor(MBB),
1159                           LoopHeader, LoopRep);
1160       ContMBB.push_back(MBB);
1161       ++NumCont;
1162     }
1163   }
1164
1165   for (SmallVectorImpl<MachineBasicBlock *>::iterator It = ContMBB.begin(),
1166       E = ContMBB.end(); It != E; ++It) {
1167     (*It)->removeSuccessor(LoopHeader);
1168   }
1169
1170   numLoopcontPatternMatch += NumCont;
1171
1172   return NumCont;
1173 }
1174
1175
1176 bool AMDGPUCFGStructurizer::isSameloopDetachedContbreak(
1177     MachineBasicBlock *Src1MBB, MachineBasicBlock *Src2MBB) {
1178   if (Src1MBB->succ_size() == 0) {
1179     MachineLoop *LoopRep = MLI->getLoopFor(Src1MBB);
1180     if (LoopRep&& LoopRep == MLI->getLoopFor(Src2MBB)) {
1181       MachineBasicBlock *&TheEntry = LLInfoMap[LoopRep];
1182       if (TheEntry) {
1183         DEBUG(
1184           dbgs() << "isLoopContBreakBlock yes src1 = BB"
1185                  << Src1MBB->getNumber()
1186                  << " src2 = BB" << Src2MBB->getNumber() << "\n";
1187         );
1188         return true;
1189       }
1190     }
1191   }
1192   return false;
1193 }
1194
1195 int AMDGPUCFGStructurizer::handleJumpintoIf(MachineBasicBlock *HeadMBB,
1196     MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
1197   int Num = handleJumpintoIfImp(HeadMBB, TrueMBB, FalseMBB);
1198   if (Num == 0) {
1199     DEBUG(
1200       dbgs() << "handleJumpintoIf swap trueBlk and FalseBlk" << "\n";
1201     );
1202     Num = handleJumpintoIfImp(HeadMBB, FalseMBB, TrueMBB);
1203   }
1204   return Num;
1205 }
1206
1207 int AMDGPUCFGStructurizer::handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
1208     MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
1209   int Num = 0;
1210   MachineBasicBlock *DownBlk;
1211
1212   //trueBlk could be the common post dominator
1213   DownBlk = TrueMBB;
1214
1215   DEBUG(
1216     dbgs() << "handleJumpintoIfImp head = BB" << HeadMBB->getNumber()
1217            << " true = BB" << TrueMBB->getNumber()
1218            << ", numSucc=" << TrueMBB->succ_size()
1219            << " false = BB" << FalseMBB->getNumber() << "\n";
1220   );
1221
1222   while (DownBlk) {
1223     DEBUG(
1224       dbgs() << "check down = BB" << DownBlk->getNumber();
1225     );
1226
1227     if (singlePathTo(FalseMBB, DownBlk) == SinglePath_InPath) {
1228       DEBUG(
1229         dbgs() << " working\n";
1230       );
1231
1232       Num += cloneOnSideEntryTo(HeadMBB, TrueMBB, DownBlk);
1233       Num += cloneOnSideEntryTo(HeadMBB, FalseMBB, DownBlk);
1234
1235       numClonedBlock += Num;
1236       Num += serialPatternMatch(*HeadMBB->succ_begin());
1237       Num += serialPatternMatch(*std::next(HeadMBB->succ_begin()));
1238       Num += ifPatternMatch(HeadMBB);
1239       assert(Num > 0);
1240
1241       break;
1242     }
1243     DEBUG(
1244       dbgs() << " not working\n";
1245     );
1246     DownBlk = (DownBlk->succ_size() == 1) ? (*DownBlk->succ_begin()) : nullptr;
1247   } // walk down the postDomTree
1248
1249   return Num;
1250 }
1251
1252 void AMDGPUCFGStructurizer::showImproveSimpleJumpintoIf(
1253     MachineBasicBlock *HeadMBB, MachineBasicBlock *TrueMBB,
1254     MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB, bool Detail) {
1255   dbgs() << "head = BB" << HeadMBB->getNumber()
1256          << " size = " << HeadMBB->size();
1257   if (Detail) {
1258     dbgs() << "\n";
1259     HeadMBB->print(dbgs());
1260     dbgs() << "\n";
1261   }
1262
1263   if (TrueMBB) {
1264     dbgs() << ", true = BB" << TrueMBB->getNumber() << " size = "
1265            << TrueMBB->size() << " numPred = " << TrueMBB->pred_size();
1266     if (Detail) {
1267       dbgs() << "\n";
1268       TrueMBB->print(dbgs());
1269       dbgs() << "\n";
1270     }
1271   }
1272   if (FalseMBB) {
1273     dbgs() << ", false = BB" << FalseMBB->getNumber() << " size = "
1274            << FalseMBB->size() << " numPred = " << FalseMBB->pred_size();
1275     if (Detail) {
1276       dbgs() << "\n";
1277       FalseMBB->print(dbgs());
1278       dbgs() << "\n";
1279     }
1280   }
1281   if (LandMBB) {
1282     dbgs() << ", land = BB" << LandMBB->getNumber() << " size = "
1283            << LandMBB->size() << " numPred = " << LandMBB->pred_size();
1284     if (Detail) {
1285       dbgs() << "\n";
1286       LandMBB->print(dbgs());
1287       dbgs() << "\n";
1288     }
1289   }
1290
1291     dbgs() << "\n";
1292 }
1293
1294 int AMDGPUCFGStructurizer::improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
1295     MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
1296     MachineBasicBlock **LandMBBPtr) {
1297   bool MigrateTrue = false;
1298   bool MigrateFalse = false;
1299
1300   MachineBasicBlock *LandBlk = *LandMBBPtr;
1301
1302   assert((!TrueMBB || TrueMBB->succ_size() <= 1)
1303          && (!FalseMBB || FalseMBB->succ_size() <= 1));
1304
1305   if (TrueMBB == FalseMBB)
1306     return 0;
1307
1308   MigrateTrue = needMigrateBlock(TrueMBB);
1309   MigrateFalse = needMigrateBlock(FalseMBB);
1310
1311   if (!MigrateTrue && !MigrateFalse)
1312     return 0;
1313
1314   // If we need to migrate either trueBlk and falseBlk, migrate the rest that
1315   // have more than one predecessors.  without doing this, its predecessor
1316   // rather than headBlk will have undefined value in initReg.
1317   if (!MigrateTrue && TrueMBB && TrueMBB->pred_size() > 1)
1318     MigrateTrue = true;
1319   if (!MigrateFalse && FalseMBB && FalseMBB->pred_size() > 1)
1320     MigrateFalse = true;
1321
1322   DEBUG(
1323     dbgs() << "before improveSimpleJumpintoIf: ";
1324     showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
1325   );
1326
1327   // org: headBlk => if () {trueBlk} else {falseBlk} => landBlk
1328   //
1329   // new: headBlk => if () {initReg = 1; org trueBlk branch} else
1330   //      {initReg = 0; org falseBlk branch }
1331   //      => landBlk => if (initReg) {org trueBlk} else {org falseBlk}
1332   //      => org landBlk
1333   //      if landBlk->pred_size() > 2, put the about if-else inside
1334   //      if (initReg !=2) {...}
1335   //
1336   // add initReg = initVal to headBlk
1337
1338   const TargetRegisterClass * I32RC = TRI->getCFGStructurizerRegClass(MVT::i32);
1339   if (!MigrateTrue || !MigrateFalse) {
1340     // XXX: We have an opportunity here to optimize the "branch into if" case
1341     // here.  Branch into if looks like this:
1342     //                        entry
1343     //                       /     |
1344     //           diamond_head       branch_from
1345     //             /      \           |
1346     // diamond_false        diamond_true
1347     //             \      /
1348     //               done
1349     //
1350     // The diamond_head block begins the "if" and the diamond_true block
1351     // is the block being "branched into".
1352     //
1353     // If MigrateTrue is true, then TrueBB is the block being "branched into"
1354     // and if MigrateFalse is true, then FalseBB is the block being
1355     // "branched into"
1356     // 
1357     // Here is the pseudo code for how I think the optimization should work:
1358     // 1. Insert MOV GPR0, 0 before the branch instruction in diamond_head.
1359     // 2. Insert MOV GPR0, 1 before the branch instruction in branch_from.
1360     // 3. Move the branch instruction from diamond_head into its own basic
1361     //    block (new_block).
1362     // 4. Add an unconditional branch from diamond_head to new_block
1363     // 5. Replace the branch instruction in branch_from with an unconditional
1364     //    branch to new_block.  If branch_from has multiple predecessors, then
1365     //    we need to replace the True/False block in the branch
1366     //    instruction instead of replacing it.
1367     // 6. Change the condition of the branch instruction in new_block from
1368     //    COND to (COND || GPR0)
1369     //
1370     // In order insert these MOV instruction, we will need to use the
1371     // RegisterScavenger.  Usually liveness stops being tracked during
1372     // the late machine optimization passes, however if we implement
1373     // bool TargetRegisterInfo::requiresRegisterScavenging(
1374     //                                                const MachineFunction &MF)
1375     // and have it return true, liveness will be tracked correctly 
1376     // by generic optimization passes.  We will also need to make sure that
1377     // all of our target-specific passes that run after regalloc and before
1378     // the CFGStructurizer track liveness and we will need to modify this pass
1379     // to correctly track liveness.
1380     //
1381     // After the above changes, the new CFG should look like this:
1382     //                        entry
1383     //                       /     |
1384     //           diamond_head       branch_from
1385     //                       \     /
1386     //                      new_block
1387     //                      /      |
1388     //         diamond_false        diamond_true
1389     //                      \      /
1390     //                        done
1391     //
1392     // Without this optimization, we are forced to duplicate the diamond_true
1393     // block and we will end up with a CFG like this:
1394     //
1395     //                        entry
1396     //                       /     |
1397     //           diamond_head       branch_from
1398     //             /      \                   |
1399     // diamond_false        diamond_true      diamond_true (duplicate)
1400     //             \      /                   |
1401     //               done --------------------|
1402     //
1403     // Duplicating diamond_true can be very costly especially if it has a
1404     // lot of instructions.
1405     return 0;
1406   }
1407
1408   int NumNewBlk = 0;
1409
1410   bool LandBlkHasOtherPred = (LandBlk->pred_size() > 2);
1411
1412   //insert AMDGPU::ENDIF to avoid special case "input landBlk == NULL"
1413   MachineBasicBlock::iterator I = insertInstrBefore(LandBlk, AMDGPU::ENDIF);
1414
1415   if (LandBlkHasOtherPred) {
1416     llvm_unreachable("Extra register needed to handle CFG");
1417     unsigned CmpResReg =
1418       HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
1419     llvm_unreachable("Extra compare instruction needed to handle CFG");
1420     insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET,
1421         CmpResReg, DebugLoc());
1422   }
1423
1424   // XXX: We are running this after RA, so creating virtual registers will
1425   // cause an assertion failure in the PostRA scheduling pass.
1426   unsigned InitReg =
1427     HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
1428   insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET, InitReg,
1429       DebugLoc());
1430
1431   if (MigrateTrue) {
1432     migrateInstruction(TrueMBB, LandBlk, I);
1433     // need to uncondionally insert the assignment to ensure a path from its
1434     // predecessor rather than headBlk has valid value in initReg if
1435     // (initVal != 1).
1436     llvm_unreachable("Extra register needed to handle CFG");
1437   }
1438   insertInstrBefore(I, AMDGPU::ELSE);
1439
1440   if (MigrateFalse) {
1441     migrateInstruction(FalseMBB, LandBlk, I);
1442     // need to uncondionally insert the assignment to ensure a path from its
1443     // predecessor rather than headBlk has valid value in initReg if
1444     // (initVal != 0)
1445     llvm_unreachable("Extra register needed to handle CFG");
1446   }
1447
1448   if (LandBlkHasOtherPred) {
1449     // add endif
1450     insertInstrBefore(I, AMDGPU::ENDIF);
1451
1452     // put initReg = 2 to other predecessors of landBlk
1453     for (MachineBasicBlock::pred_iterator PI = LandBlk->pred_begin(),
1454          PE = LandBlk->pred_end(); PI != PE; ++PI) {
1455       MachineBasicBlock *MBB = *PI;
1456       if (MBB != TrueMBB && MBB != FalseMBB)
1457         llvm_unreachable("Extra register needed to handle CFG");
1458     }
1459   }
1460   DEBUG(
1461     dbgs() << "result from improveSimpleJumpintoIf: ";
1462     showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
1463   );
1464
1465   // update landBlk
1466   *LandMBBPtr = LandBlk;
1467
1468   return NumNewBlk;
1469 }
1470
1471 void AMDGPUCFGStructurizer::handleLoopcontBlock(MachineBasicBlock *ContingMBB,
1472     MachineLoop *ContingLoop, MachineBasicBlock *ContMBB,
1473     MachineLoop *ContLoop) {
1474   DEBUG(dbgs() << "loopcontPattern cont = BB" << ContingMBB->getNumber()
1475                << " header = BB" << ContMBB->getNumber() << "\n";
1476         dbgs() << "Trying to continue loop-depth = "
1477                << getLoopDepth(ContLoop)
1478                << " from loop-depth = " << getLoopDepth(ContingLoop) << "\n";);
1479   settleLoopcontBlock(ContingMBB, ContMBB);
1480 }
1481
1482 void AMDGPUCFGStructurizer::mergeSerialBlock(MachineBasicBlock *DstMBB,
1483     MachineBasicBlock *SrcMBB) {
1484   DEBUG(
1485     dbgs() << "serialPattern BB" << DstMBB->getNumber()
1486            << " <= BB" << SrcMBB->getNumber() << "\n";
1487   );
1488   DstMBB->splice(DstMBB->end(), SrcMBB, SrcMBB->begin(), SrcMBB->end());
1489
1490   DstMBB->removeSuccessor(SrcMBB);
1491   cloneSuccessorList(DstMBB, SrcMBB);
1492
1493   removeSuccessor(SrcMBB);
1494   MLI->removeBlock(SrcMBB);
1495   retireBlock(SrcMBB);
1496 }
1497
1498 void AMDGPUCFGStructurizer::mergeIfthenelseBlock(MachineInstr *BranchMI,
1499     MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
1500     MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB) {
1501   assert (TrueMBB);
1502   DEBUG(
1503     dbgs() << "ifPattern BB" << MBB->getNumber();
1504     dbgs() << "{  ";
1505     if (TrueMBB) {
1506       dbgs() << "BB" << TrueMBB->getNumber();
1507     }
1508     dbgs() << "  } else ";
1509     dbgs() << "{  ";
1510     if (FalseMBB) {
1511       dbgs() << "BB" << FalseMBB->getNumber();
1512     }
1513     dbgs() << "  }\n ";
1514     dbgs() << "landBlock: ";
1515     if (!LandMBB) {
1516       dbgs() << "NULL";
1517     } else {
1518       dbgs() << "BB" << LandMBB->getNumber();
1519     }
1520     dbgs() << "\n";
1521   );
1522
1523   int OldOpcode = BranchMI->getOpcode();
1524   DebugLoc BranchDL = BranchMI->getDebugLoc();
1525
1526 //    transform to
1527 //    if cond
1528 //       trueBlk
1529 //    else
1530 //       falseBlk
1531 //    endif
1532 //    landBlk
1533
1534   MachineBasicBlock::iterator I = BranchMI;
1535   insertCondBranchBefore(I, getBranchNzeroOpcode(OldOpcode),
1536       BranchDL);
1537
1538   if (TrueMBB) {
1539     MBB->splice(I, TrueMBB, TrueMBB->begin(), TrueMBB->end());
1540     MBB->removeSuccessor(TrueMBB);
1541     if (LandMBB && TrueMBB->succ_size()!=0)
1542       TrueMBB->removeSuccessor(LandMBB);
1543     retireBlock(TrueMBB);
1544     MLI->removeBlock(TrueMBB);
1545   }
1546
1547   if (FalseMBB) {
1548     insertInstrBefore(I, AMDGPU::ELSE);
1549     MBB->splice(I, FalseMBB, FalseMBB->begin(),
1550                    FalseMBB->end());
1551     MBB->removeSuccessor(FalseMBB);
1552     if (LandMBB && FalseMBB->succ_size() != 0)
1553       FalseMBB->removeSuccessor(LandMBB);
1554     retireBlock(FalseMBB);
1555     MLI->removeBlock(FalseMBB);
1556   }
1557   insertInstrBefore(I, AMDGPU::ENDIF);
1558
1559   BranchMI->eraseFromParent();
1560
1561   if (LandMBB && TrueMBB && FalseMBB)
1562     MBB->addSuccessor(LandMBB);
1563
1564 }
1565
1566 void AMDGPUCFGStructurizer::mergeLooplandBlock(MachineBasicBlock *DstBlk,
1567     MachineBasicBlock *LandMBB) {
1568   DEBUG(dbgs() << "loopPattern header = BB" << DstBlk->getNumber()
1569                << " land = BB" << LandMBB->getNumber() << "\n";);
1570
1571   insertInstrBefore(DstBlk, AMDGPU::WHILELOOP, DebugLoc());
1572   insertInstrEnd(DstBlk, AMDGPU::ENDLOOP, DebugLoc());
1573   DstBlk->addSuccessor(LandMBB);
1574   DstBlk->removeSuccessor(DstBlk);
1575 }
1576
1577
1578 void AMDGPUCFGStructurizer::mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
1579     MachineBasicBlock *LandMBB) {
1580   DEBUG(dbgs() << "loopbreakPattern exiting = BB" << ExitingMBB->getNumber()
1581                << " land = BB" << LandMBB->getNumber() << "\n";);
1582   MachineInstr *BranchMI = getLoopendBlockBranchInstr(ExitingMBB);
1583   assert(BranchMI && isCondBranch(BranchMI));
1584   DebugLoc DL = BranchMI->getDebugLoc();
1585   MachineBasicBlock *TrueBranch = getTrueBranch(BranchMI);
1586   MachineBasicBlock::iterator I = BranchMI;
1587   if (TrueBranch != LandMBB)
1588     reversePredicateSetter(I);
1589   insertCondBranchBefore(ExitingMBB, I, AMDGPU::IF_PREDICATE_SET, AMDGPU::PREDICATE_BIT, DL);
1590   insertInstrBefore(I, AMDGPU::BREAK);
1591   insertInstrBefore(I, AMDGPU::ENDIF);
1592   //now branchInst can be erase safely
1593   BranchMI->eraseFromParent();
1594   //now take care of successors, retire blocks
1595   ExitingMBB->removeSuccessor(LandMBB);
1596 }
1597
1598 void AMDGPUCFGStructurizer::settleLoopcontBlock(MachineBasicBlock *ContingMBB,
1599     MachineBasicBlock *ContMBB) {
1600   DEBUG(dbgs() << "settleLoopcontBlock conting = BB"
1601                << ContingMBB->getNumber()
1602                << ", cont = BB" << ContMBB->getNumber() << "\n";);
1603
1604   MachineInstr *MI = getLoopendBlockBranchInstr(ContingMBB);
1605   if (MI) {
1606     assert(isCondBranch(MI));
1607     MachineBasicBlock::iterator I = MI;
1608     MachineBasicBlock *TrueBranch = getTrueBranch(MI);
1609     int OldOpcode = MI->getOpcode();
1610     DebugLoc DL = MI->getDebugLoc();
1611
1612     bool UseContinueLogical = ((&*ContingMBB->rbegin()) == MI);
1613
1614     if (UseContinueLogical == false) {
1615       int BranchOpcode =
1616           TrueBranch == ContMBB ? getBranchNzeroOpcode(OldOpcode) :
1617           getBranchZeroOpcode(OldOpcode);
1618       insertCondBranchBefore(I, BranchOpcode, DL);
1619       // insertEnd to ensure phi-moves, if exist, go before the continue-instr.
1620       insertInstrEnd(ContingMBB, AMDGPU::CONTINUE, DL);
1621       insertInstrEnd(ContingMBB, AMDGPU::ENDIF, DL);
1622     } else {
1623       int BranchOpcode =
1624           TrueBranch == ContMBB ? getContinueNzeroOpcode(OldOpcode) :
1625           getContinueZeroOpcode(OldOpcode);
1626       insertCondBranchBefore(I, BranchOpcode, DL);
1627     }
1628
1629     MI->eraseFromParent();
1630   } else {
1631     // if we've arrived here then we've already erased the branch instruction
1632     // travel back up the basic block to see the last reference of our debug
1633     // location we've just inserted that reference here so it should be
1634     // representative insertEnd to ensure phi-moves, if exist, go before the
1635     // continue-instr.
1636     insertInstrEnd(ContingMBB, AMDGPU::CONTINUE,
1637         getLastDebugLocInBB(ContingMBB));
1638   }
1639 }
1640
1641 int AMDGPUCFGStructurizer::cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
1642     MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB) {
1643   int Cloned = 0;
1644   assert(PreMBB->isSuccessor(SrcMBB));
1645   while (SrcMBB && SrcMBB != DstMBB) {
1646     assert(SrcMBB->succ_size() == 1);
1647     if (SrcMBB->pred_size() > 1) {
1648       SrcMBB = cloneBlockForPredecessor(SrcMBB, PreMBB);
1649       ++Cloned;
1650     }
1651
1652     PreMBB = SrcMBB;
1653     SrcMBB = *SrcMBB->succ_begin();
1654   }
1655
1656   return Cloned;
1657 }
1658
1659 MachineBasicBlock *
1660 AMDGPUCFGStructurizer::cloneBlockForPredecessor(MachineBasicBlock *MBB,
1661     MachineBasicBlock *PredMBB) {
1662   assert(PredMBB->isSuccessor(MBB) &&
1663          "succBlk is not a prececessor of curBlk");
1664
1665   MachineBasicBlock *CloneMBB = clone(MBB);  //clone instructions
1666   replaceInstrUseOfBlockWith(PredMBB, MBB, CloneMBB);
1667   //srcBlk, oldBlk, newBlk
1668
1669   PredMBB->removeSuccessor(MBB);
1670   PredMBB->addSuccessor(CloneMBB);
1671
1672   // add all successor to cloneBlk
1673   cloneSuccessorList(CloneMBB, MBB);
1674
1675   numClonedInstr += MBB->size();
1676
1677   DEBUG(
1678     dbgs() << "Cloned block: " << "BB"
1679            << MBB->getNumber() << "size " << MBB->size() << "\n";
1680   );
1681
1682   SHOWNEWBLK(CloneMBB, "result of Cloned block: ");
1683
1684   return CloneMBB;
1685 }
1686
1687 void AMDGPUCFGStructurizer::migrateInstruction(MachineBasicBlock *SrcMBB,
1688     MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I) {
1689   MachineBasicBlock::iterator SpliceEnd;
1690   //look for the input branchinstr, not the AMDGPU branchinstr
1691   MachineInstr *BranchMI = getNormalBlockBranchInstr(SrcMBB);
1692   if (!BranchMI) {
1693     DEBUG(
1694       dbgs() << "migrateInstruction don't see branch instr\n" ;
1695     );
1696     SpliceEnd = SrcMBB->end();
1697   } else {
1698     DEBUG(
1699       dbgs() << "migrateInstruction see branch instr\n" ;
1700       BranchMI->dump();
1701     );
1702     SpliceEnd = BranchMI;
1703   }
1704   DEBUG(
1705     dbgs() << "migrateInstruction before splice dstSize = " << DstMBB->size()
1706       << "srcSize = " << SrcMBB->size() << "\n";
1707   );
1708
1709   //splice insert before insertPos
1710   DstMBB->splice(I, SrcMBB, SrcMBB->begin(), SpliceEnd);
1711
1712   DEBUG(
1713     dbgs() << "migrateInstruction after splice dstSize = " << DstMBB->size()
1714       << "srcSize = " << SrcMBB->size() << "\n";
1715   );
1716 }
1717
1718 MachineBasicBlock *
1719 AMDGPUCFGStructurizer::normalizeInfiniteLoopExit(MachineLoop* LoopRep) {
1720   MachineBasicBlock *LoopHeader = LoopRep->getHeader();
1721   MachineBasicBlock *LoopLatch = LoopRep->getLoopLatch();
1722   const TargetRegisterClass * I32RC = TRI->getCFGStructurizerRegClass(MVT::i32);
1723
1724   if (!LoopHeader || !LoopLatch)
1725     return nullptr;
1726   MachineInstr *BranchMI = getLoopendBlockBranchInstr(LoopLatch);
1727   // Is LoopRep an infinite loop ?
1728   if (!BranchMI || !isUncondBranch(BranchMI))
1729     return nullptr;
1730
1731   MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
1732   FuncRep->push_back(DummyExitBlk);  //insert to function
1733   SHOWNEWBLK(DummyExitBlk, "DummyExitBlock to normalize infiniteLoop: ");
1734   DEBUG(dbgs() << "Old branch instr: " << *BranchMI << "\n";);
1735   MachineBasicBlock::iterator I = BranchMI;
1736   unsigned ImmReg = FuncRep->getRegInfo().createVirtualRegister(I32RC);
1737   llvm_unreachable("Extra register needed to handle CFG");
1738   MachineInstr *NewMI = insertInstrBefore(I, AMDGPU::BRANCH_COND_i32);
1739   MachineInstrBuilder MIB(*FuncRep, NewMI);
1740   MIB.addMBB(LoopHeader);
1741   MIB.addReg(ImmReg, false);
1742   SHOWNEWINSTR(NewMI);
1743   BranchMI->eraseFromParent();
1744   LoopLatch->addSuccessor(DummyExitBlk);
1745
1746   return DummyExitBlk;
1747 }
1748
1749 void AMDGPUCFGStructurizer::removeUnconditionalBranch(MachineBasicBlock *MBB) {
1750   MachineInstr *BranchMI;
1751
1752   // I saw two unconditional branch in one basic block in example
1753   // test_fc_do_while_or.c need to fix the upstream on this to remove the loop.
1754   while ((BranchMI = getLoopendBlockBranchInstr(MBB))
1755           && isUncondBranch(BranchMI)) {
1756     DEBUG(dbgs() << "Removing uncond branch instr"; BranchMI->dump(););
1757     BranchMI->eraseFromParent();
1758   }
1759 }
1760
1761 void AMDGPUCFGStructurizer::removeRedundantConditionalBranch(
1762     MachineBasicBlock *MBB) {
1763   if (MBB->succ_size() != 2)
1764     return;
1765   MachineBasicBlock *MBB1 = *MBB->succ_begin();
1766   MachineBasicBlock *MBB2 = *std::next(MBB->succ_begin());
1767   if (MBB1 != MBB2)
1768     return;
1769
1770   MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
1771   assert(BranchMI && isCondBranch(BranchMI));
1772   DEBUG(dbgs() << "Removing unneeded cond branch instr"; BranchMI->dump(););
1773   BranchMI->eraseFromParent();
1774   SHOWNEWBLK(MBB1, "Removing redundant successor");
1775   MBB->removeSuccessor(MBB1);
1776 }
1777
1778 void AMDGPUCFGStructurizer::addDummyExitBlock(
1779     SmallVectorImpl<MachineBasicBlock*> &RetMBB) {
1780   MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
1781   FuncRep->push_back(DummyExitBlk);  //insert to function
1782   insertInstrEnd(DummyExitBlk, AMDGPU::RETURN);
1783
1784   for (SmallVectorImpl<MachineBasicBlock *>::iterator It = RetMBB.begin(),
1785        E = RetMBB.end(); It != E; ++It) {
1786     MachineBasicBlock *MBB = *It;
1787     MachineInstr *MI = getReturnInstr(MBB);
1788     if (MI)
1789       MI->eraseFromParent();
1790     MBB->addSuccessor(DummyExitBlk);
1791     DEBUG(
1792       dbgs() << "Add dummyExitBlock to BB" << MBB->getNumber()
1793              << " successors\n";
1794     );
1795   }
1796   SHOWNEWBLK(DummyExitBlk, "DummyExitBlock: ");
1797 }
1798
1799 void AMDGPUCFGStructurizer::removeSuccessor(MachineBasicBlock *MBB) {
1800   while (MBB->succ_size())
1801     MBB->removeSuccessor(*MBB->succ_begin());
1802 }
1803
1804 void AMDGPUCFGStructurizer::recordSccnum(MachineBasicBlock *MBB,
1805     int SccNum) {
1806   BlockInformation *&srcBlkInfo = BlockInfoMap[MBB];
1807   if (!srcBlkInfo)
1808     srcBlkInfo = new BlockInformation();
1809   srcBlkInfo->SccNum = SccNum;
1810 }
1811
1812 void AMDGPUCFGStructurizer::retireBlock(MachineBasicBlock *MBB) {
1813   DEBUG(
1814         dbgs() << "Retiring BB" << MBB->getNumber() << "\n";
1815   );
1816
1817   BlockInformation *&SrcBlkInfo = BlockInfoMap[MBB];
1818
1819   if (!SrcBlkInfo)
1820     SrcBlkInfo = new BlockInformation();
1821
1822   SrcBlkInfo->IsRetired = true;
1823   assert(MBB->succ_size() == 0 && MBB->pred_size() == 0
1824          && "can't retire block yet");
1825 }
1826
1827 void AMDGPUCFGStructurizer::setLoopLandBlock(MachineLoop *loopRep,
1828     MachineBasicBlock *MBB) {
1829   MachineBasicBlock *&TheEntry = LLInfoMap[loopRep];
1830   if (!MBB) {
1831     MBB = FuncRep->CreateMachineBasicBlock();
1832     FuncRep->push_back(MBB);  //insert to function
1833     SHOWNEWBLK(MBB, "DummyLandingBlock for loop without break: ");
1834   }
1835   TheEntry = MBB;
1836   DEBUG(
1837     dbgs() << "setLoopLandBlock loop-header = BB"
1838            << loopRep->getHeader()->getNumber()
1839            << "  landing-block = BB" << MBB->getNumber() << "\n";
1840   );
1841 }
1842
1843 MachineBasicBlock *
1844 AMDGPUCFGStructurizer::findNearestCommonPostDom(MachineBasicBlock *MBB1,
1845     MachineBasicBlock *MBB2) {
1846
1847   if (PDT->dominates(MBB1, MBB2))
1848     return MBB1;
1849   if (PDT->dominates(MBB2, MBB1))
1850     return MBB2;
1851
1852   MachineDomTreeNode *Node1 = PDT->getNode(MBB1);
1853   MachineDomTreeNode *Node2 = PDT->getNode(MBB2);
1854
1855   // Handle newly cloned node.
1856   if (!Node1 && MBB1->succ_size() == 1)
1857     return findNearestCommonPostDom(*MBB1->succ_begin(), MBB2);
1858   if (!Node2 && MBB2->succ_size() == 1)
1859     return findNearestCommonPostDom(MBB1, *MBB2->succ_begin());
1860
1861   if (!Node1 || !Node2)
1862     return nullptr;
1863
1864   Node1 = Node1->getIDom();
1865   while (Node1) {
1866     if (PDT->dominates(Node1, Node2))
1867       return Node1->getBlock();
1868     Node1 = Node1->getIDom();
1869   }
1870
1871   return nullptr;
1872 }
1873
1874 MachineBasicBlock *
1875 AMDGPUCFGStructurizer::findNearestCommonPostDom(
1876     std::set<MachineBasicBlock *> &MBBs) {
1877   MachineBasicBlock *CommonDom;
1878   std::set<MachineBasicBlock *>::const_iterator It = MBBs.begin();
1879   std::set<MachineBasicBlock *>::const_iterator E = MBBs.end();
1880   for (CommonDom = *It; It != E && CommonDom; ++It) {
1881     MachineBasicBlock *MBB = *It;
1882     if (MBB != CommonDom)
1883       CommonDom = findNearestCommonPostDom(MBB, CommonDom);
1884   }
1885
1886   DEBUG(
1887     dbgs() << "Common post dominator for exit blocks is ";
1888     if (CommonDom)
1889           dbgs() << "BB" << CommonDom->getNumber() << "\n";
1890     else
1891       dbgs() << "NULL\n";
1892   );
1893
1894   return CommonDom;
1895 }
1896
1897 char AMDGPUCFGStructurizer::ID = 0;
1898
1899 } // end anonymous namespace
1900
1901
1902 INITIALIZE_PASS_BEGIN(AMDGPUCFGStructurizer, "amdgpustructurizer",
1903                       "AMDGPU CFG Structurizer", false, false)
1904 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
1905 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
1906 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
1907 INITIALIZE_PASS_END(AMDGPUCFGStructurizer, "amdgpustructurizer",
1908                       "AMDGPU CFG Structurizer", false, false)
1909
1910 FunctionPass *llvm::createAMDGPUCFGStructurizerPass() {
1911   return new AMDGPUCFGStructurizer();
1912 }