blob: 79cee08168b1b1c5be2c404a24fdc20ddbbc632d [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- 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
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000011#include "AMDGPU.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000012#include "AMDGPUInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000013#include "AMDGPUSubtarget.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000014#include "R600InstrInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "llvm/ADT/DepthFirstIterator.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000016#include "llvm/ADT/SCCIterator.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#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"
Chandler Carruthbe810232013-01-02 10:22:59 +000026#include "llvm/CodeGen/MachinePostDominators.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000028#include "llvm/IR/Dominators.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/Target/TargetInstrInfo.h"
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000032#include "llvm/Target/TargetMachine.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000033#include <deque>
Tom Stellard75aadc22012-12-11 21:25:42 +000034
35using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "structcfg"
38
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000039#define DEFAULT_VEC_SLOTS 8
40
Tom Stellard75aadc22012-12-11 21:25:42 +000041// TODO: move-begin.
42
43//===----------------------------------------------------------------------===//
44//
45// Statistics for CFGStructurizer.
46//
47//===----------------------------------------------------------------------===//
48
49STATISTIC(numSerialPatternMatch, "CFGStructurizer number of serial pattern "
50 "matched");
51STATISTIC(numIfPatternMatch, "CFGStructurizer number of if pattern "
52 "matched");
Tom Stellard75aadc22012-12-11 21:25:42 +000053STATISTIC(numLoopcontPatternMatch, "CFGStructurizer number of loop-continue "
54 "pattern matched");
Tom Stellard75aadc22012-12-11 21:25:42 +000055STATISTIC(numClonedBlock, "CFGStructurizer cloned blocks");
56STATISTIC(numClonedInstr, "CFGStructurizer cloned instructions");
57
Tom Stellardf2ba9722013-12-11 17:51:47 +000058namespace llvm {
59 void initializeAMDGPUCFGStructurizerPass(PassRegistry&);
60}
61
Tom Stellard75aadc22012-12-11 21:25:42 +000062//===----------------------------------------------------------------------===//
63//
64// Miscellaneous utility for CFGStructurizer.
65//
66//===----------------------------------------------------------------------===//
Benjamin Kramer635e3682013-05-23 15:43:05 +000067namespace {
Tom Stellard75aadc22012-12-11 21:25:42 +000068#define SHOWNEWINSTR(i) \
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +000069 DEBUG(dbgs() << "New instr: " << *i << "\n");
Tom Stellard75aadc22012-12-11 21:25:42 +000070
71#define SHOWNEWBLK(b, msg) \
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +000072DEBUG( \
73 dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
74 dbgs() << "\n"; \
75);
Tom Stellard75aadc22012-12-11 21:25:42 +000076
77#define SHOWBLK_DETAIL(b, msg) \
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +000078DEBUG( \
Tom Stellard75aadc22012-12-11 21:25:42 +000079 if (b) { \
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +000080 dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
81 b->print(dbgs()); \
82 dbgs() << "\n"; \
Tom Stellard75aadc22012-12-11 21:25:42 +000083 } \
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +000084);
Tom Stellard75aadc22012-12-11 21:25:42 +000085
86#define INVALIDSCCNUM -1
Tom Stellard75aadc22012-12-11 21:25:42 +000087
88template<class NodeT>
Craig Topperb94011f2013-07-14 04:42:23 +000089void ReverseVector(SmallVectorImpl<NodeT *> &Src) {
Tom Stellard75aadc22012-12-11 21:25:42 +000090 size_t sz = Src.size();
91 for (size_t i = 0; i < sz/2; ++i) {
92 NodeT *t = Src[i];
93 Src[i] = Src[sz - i - 1];
94 Src[sz - i - 1] = t;
95 }
96}
97
Benjamin Kramer635e3682013-05-23 15:43:05 +000098} // end anonymous namespace
Tom Stellard75aadc22012-12-11 21:25:42 +000099
100//===----------------------------------------------------------------------===//
101//
102// supporting data structure for CFGStructurizer
103//
104//===----------------------------------------------------------------------===//
105
Tom Stellard75aadc22012-12-11 21:25:42 +0000106
Vincent Lejeune960a6222013-07-19 21:45:06 +0000107namespace {
108
Tom Stellard75aadc22012-12-11 21:25:42 +0000109class BlockInformation {
110public:
Vincent Lejeune960a6222013-07-19 21:45:06 +0000111 bool IsRetired;
112 int SccNum;
113 BlockInformation() : IsRetired(false), SccNum(INVALIDSCCNUM) {}
Tom Stellard75aadc22012-12-11 21:25:42 +0000114};
115
Benjamin Kramer635e3682013-05-23 15:43:05 +0000116} // end anonymous namespace
Tom Stellard75aadc22012-12-11 21:25:42 +0000117
118//===----------------------------------------------------------------------===//
119//
120// CFGStructurizer
121//
122//===----------------------------------------------------------------------===//
123
Benjamin Kramer635e3682013-05-23 15:43:05 +0000124namespace {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000125class AMDGPUCFGStructurizer : public MachineFunctionPass {
Tom Stellard75aadc22012-12-11 21:25:42 +0000126public:
Vincent Lejeune960a6222013-07-19 21:45:06 +0000127 typedef SmallVector<MachineBasicBlock *, 32> MBBVector;
128 typedef std::map<MachineBasicBlock *, BlockInformation *> MBBInfoMap;
129 typedef std::map<MachineLoop *, MachineBasicBlock *> LoopLandInfoMap;
130
131 enum PathToKind {
Tom Stellard75aadc22012-12-11 21:25:42 +0000132 Not_SinglePath = 0,
133 SinglePath_InPath = 1,
134 SinglePath_NotInPath = 2
Vincent Lejeune960a6222013-07-19 21:45:06 +0000135 };
Tom Stellard75aadc22012-12-11 21:25:42 +0000136
Vincent Lejeune960a6222013-07-19 21:45:06 +0000137 static char ID;
Tom Stellard75aadc22012-12-11 21:25:42 +0000138
Tom Stellardf2ba9722013-12-11 17:51:47 +0000139 AMDGPUCFGStructurizer() :
Craig Topper062a2ba2014-04-25 05:30:21 +0000140 MachineFunctionPass(ID), TII(nullptr), TRI(nullptr) {
Tom Stellardf2ba9722013-12-11 17:51:47 +0000141 initializeAMDGPUCFGStructurizerPass(*PassRegistry::getPassRegistry());
142 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000143
Craig Topper5656db42014-04-29 07:57:24 +0000144 const char *getPassName() const override {
Tom Stellardf2ba9722013-12-11 17:51:47 +0000145 return "AMDGPU Control Flow Graph structurizer Pass";
Vincent Lejeune960a6222013-07-19 21:45:06 +0000146 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000147
Craig Topper5656db42014-04-29 07:57:24 +0000148 void getAnalysisUsage(AnalysisUsage &AU) const override {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000149 AU.addPreserved<MachineFunctionAnalysis>();
150 AU.addRequired<MachineFunctionAnalysis>();
151 AU.addRequired<MachineDominatorTree>();
152 AU.addRequired<MachinePostDominatorTree>();
153 AU.addRequired<MachineLoopInfo>();
154 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000155
156 /// Perform the CFG structurization
Vincent Lejeune960a6222013-07-19 21:45:06 +0000157 bool run();
Tom Stellard75aadc22012-12-11 21:25:42 +0000158
159 /// Perform the CFG preparation
Vincent Lejeune960a6222013-07-19 21:45:06 +0000160 /// This step will remove every unconditionnal/dead jump instructions and make
161 /// sure all loops have an exit block
162 bool prepare();
Tom Stellard75aadc22012-12-11 21:25:42 +0000163
Craig Topper5656db42014-04-29 07:57:24 +0000164 bool runOnMachineFunction(MachineFunction &MF) override {
Eric Christopherfc6de422014-08-05 02:39:49 +0000165 TII = static_cast<const R600InstrInfo *>(MF.getSubtarget().getInstrInfo());
Tom Stellardf2ba9722013-12-11 17:51:47 +0000166 TRI = &TII->getRegisterInfo();
Vincent Lejeune960a6222013-07-19 21:45:06 +0000167 DEBUG(MF.dump(););
168 OrderedBlks.clear();
Jan Vesely7a9cca92015-03-13 17:32:46 +0000169 Visited.clear();
Vincent Lejeune960a6222013-07-19 21:45:06 +0000170 FuncRep = &MF;
171 MLI = &getAnalysis<MachineLoopInfo>();
172 DEBUG(dbgs() << "LoopInfo:\n"; PrintLoopinfo(*MLI););
173 MDT = &getAnalysis<MachineDominatorTree>();
Craig Toppere73658d2014-04-28 04:05:08 +0000174 DEBUG(MDT->print(dbgs(), (const llvm::Module*)nullptr););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000175 PDT = &getAnalysis<MachinePostDominatorTree>();
176 DEBUG(PDT->print(dbgs()););
177 prepare();
178 run();
179 DEBUG(MF.dump(););
180 return true;
181 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000182
Vincent Lejeune960a6222013-07-19 21:45:06 +0000183protected:
Vincent Lejeune960a6222013-07-19 21:45:06 +0000184 MachineDominatorTree *MDT;
185 MachinePostDominatorTree *PDT;
186 MachineLoopInfo *MLI;
187 const R600InstrInfo *TII;
Matt Arsenault1fafdc82015-09-19 06:41:10 +0000188 const R600RegisterInfo *TRI;
Tom Stellard75aadc22012-12-11 21:25:42 +0000189
Vincent Lejeune960a6222013-07-19 21:45:06 +0000190 // PRINT FUNCTIONS
191 /// Print the ordered Blocks.
192 void printOrderedBlocks() const {
193 size_t i = 0;
194 for (MBBVector::const_iterator iterBlk = OrderedBlks.begin(),
195 iterBlkEnd = OrderedBlks.end(); iterBlk != iterBlkEnd; ++iterBlk, ++i) {
196 dbgs() << "BB" << (*iterBlk)->getNumber();
197 dbgs() << "(" << getSCCNum(*iterBlk) << "," << (*iterBlk)->size() << ")";
198 if (i != 0 && i % 10 == 0) {
199 dbgs() << "\n";
200 } else {
201 dbgs() << " ";
202 }
203 }
204 }
205 static void PrintLoopinfo(const MachineLoopInfo &LoopInfo) {
206 for (MachineLoop::iterator iter = LoopInfo.begin(),
207 iterEnd = LoopInfo.end(); iter != iterEnd; ++iter) {
208 (*iter)->print(dbgs(), 0);
209 }
210 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000211
Vincent Lejeune960a6222013-07-19 21:45:06 +0000212 // UTILITY FUNCTIONS
213 int getSCCNum(MachineBasicBlock *MBB) const;
214 MachineBasicBlock *getLoopLandInfo(MachineLoop *LoopRep) const;
215 bool hasBackEdge(MachineBasicBlock *MBB) const;
216 static unsigned getLoopDepth(MachineLoop *LoopRep);
217 bool isRetiredBlock(MachineBasicBlock *MBB) const;
218 bool isActiveLoophead(MachineBasicBlock *MBB) const;
219 PathToKind singlePathTo(MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
220 bool AllowSideEntry = true) const;
221 int countActiveBlock(MBBVector::const_iterator It,
222 MBBVector::const_iterator E) const;
223 bool needMigrateBlock(MachineBasicBlock *MBB) const;
224
225 // Utility Functions
226 void reversePredicateSetter(MachineBasicBlock::iterator I);
227 /// Compute the reversed DFS post order of Blocks
228 void orderBlocks(MachineFunction *MF);
229
Alp Tokercb402912014-01-24 17:20:08 +0000230 // Function originally from CFGStructTraits
Vincent Lejeune960a6222013-07-19 21:45:06 +0000231 void insertInstrEnd(MachineBasicBlock *MBB, int NewOpcode,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000232 const DebugLoc &DL = DebugLoc());
Vincent Lejeune960a6222013-07-19 21:45:06 +0000233 MachineInstr *insertInstrBefore(MachineBasicBlock *MBB, int NewOpcode,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000234 const DebugLoc &DL = DebugLoc());
Vincent Lejeune960a6222013-07-19 21:45:06 +0000235 MachineInstr *insertInstrBefore(MachineBasicBlock::iterator I, int NewOpcode);
236 void insertCondBranchBefore(MachineBasicBlock::iterator I, int NewOpcode,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000237 const DebugLoc &DL);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000238 void insertCondBranchBefore(MachineBasicBlock *MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000239 MachineBasicBlock::iterator I, int NewOpcode,
240 int RegNum, const DebugLoc &DL);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000241 void insertCondBranchEnd(MachineBasicBlock *MBB, int NewOpcode, int RegNum);
242 static int getBranchNzeroOpcode(int OldOpcode);
243 static int getBranchZeroOpcode(int OldOpcode);
244 static int getContinueNzeroOpcode(int OldOpcode);
245 static int getContinueZeroOpcode(int OldOpcode);
246 static MachineBasicBlock *getTrueBranch(MachineInstr *MI);
247 static void setTrueBranch(MachineInstr *MI, MachineBasicBlock *MBB);
248 static MachineBasicBlock *getFalseBranch(MachineBasicBlock *MBB,
249 MachineInstr *MI);
250 static bool isCondBranch(MachineInstr *MI);
251 static bool isUncondBranch(MachineInstr *MI);
252 static DebugLoc getLastDebugLocInBB(MachineBasicBlock *MBB);
253 static MachineInstr *getNormalBlockBranchInstr(MachineBasicBlock *MBB);
254 /// The correct naming for this is getPossibleLoopendBlockBranchInstr.
255 ///
256 /// BB with backward-edge could have move instructions after the branch
257 /// instruction. Such move instruction "belong to" the loop backward-edge.
258 MachineInstr *getLoopendBlockBranchInstr(MachineBasicBlock *MBB);
259 static MachineInstr *getReturnInstr(MachineBasicBlock *MBB);
260 static MachineInstr *getContinueInstr(MachineBasicBlock *MBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000261 static bool isReturnBlock(MachineBasicBlock *MBB);
262 static void cloneSuccessorList(MachineBasicBlock *DstMBB,
263 MachineBasicBlock *SrcMBB) ;
264 static MachineBasicBlock *clone(MachineBasicBlock *MBB);
265 /// MachineBasicBlock::ReplaceUsesOfBlockWith doesn't serve the purpose
266 /// because the AMDGPU instruction is not recognized as terminator fix this
267 /// and retire this routine
268 void replaceInstrUseOfBlockWith(MachineBasicBlock *SrcMBB,
269 MachineBasicBlock *OldMBB, MachineBasicBlock *NewBlk);
270 static void wrapup(MachineBasicBlock *MBB);
271
272
273 int patternMatch(MachineBasicBlock *MBB);
274 int patternMatchGroup(MachineBasicBlock *MBB);
275 int serialPatternMatch(MachineBasicBlock *MBB);
276 int ifPatternMatch(MachineBasicBlock *MBB);
277 int loopendPatternMatch();
278 int mergeLoop(MachineLoop *LoopRep);
279 int loopcontPatternMatch(MachineLoop *LoopRep, MachineBasicBlock *LoopHeader);
280
281 void handleLoopcontBlock(MachineBasicBlock *ContingMBB,
282 MachineLoop *ContingLoop, MachineBasicBlock *ContMBB,
283 MachineLoop *ContLoop);
284 /// return true iff src1Blk->succ_size() == 0 && src1Blk and src2Blk are in
285 /// the same loop with LoopLandInfo without explicitly keeping track of
286 /// loopContBlks and loopBreakBlks, this is a method to get the information.
287 bool isSameloopDetachedContbreak(MachineBasicBlock *Src1MBB,
288 MachineBasicBlock *Src2MBB);
289 int handleJumpintoIf(MachineBasicBlock *HeadMBB,
290 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
291 int handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
292 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
293 int improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
294 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
295 MachineBasicBlock **LandMBBPtr);
296 void showImproveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
297 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
298 MachineBasicBlock *LandMBB, bool Detail = false);
299 int cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
300 MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB);
301 void mergeSerialBlock(MachineBasicBlock *DstMBB,
302 MachineBasicBlock *SrcMBB);
303
304 void mergeIfthenelseBlock(MachineInstr *BranchMI,
305 MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
306 MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB);
307 void mergeLooplandBlock(MachineBasicBlock *DstMBB,
308 MachineBasicBlock *LandMBB);
309 void mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
310 MachineBasicBlock *LandMBB);
311 void settleLoopcontBlock(MachineBasicBlock *ContingMBB,
312 MachineBasicBlock *ContMBB);
313 /// normalizeInfiniteLoopExit change
314 /// B1:
315 /// uncond_br LoopHeader
316 ///
317 /// to
318 /// B1:
319 /// cond_br 1 LoopHeader dummyExit
320 /// and return the newly added dummy exit block
321 MachineBasicBlock *normalizeInfiniteLoopExit(MachineLoop *LoopRep);
322 void removeUnconditionalBranch(MachineBasicBlock *MBB);
323 /// Remove duplicate branches instructions in a block.
324 /// For instance
325 /// B0:
326 /// cond_br X B1 B2
327 /// cond_br X B1 B2
328 /// is transformed to
329 /// B0:
330 /// cond_br X B1 B2
331 void removeRedundantConditionalBranch(MachineBasicBlock *MBB);
332 void addDummyExitBlock(SmallVectorImpl<MachineBasicBlock *> &RetMBB);
333 void removeSuccessor(MachineBasicBlock *MBB);
334 MachineBasicBlock *cloneBlockForPredecessor(MachineBasicBlock *MBB,
335 MachineBasicBlock *PredMBB);
336 void migrateInstruction(MachineBasicBlock *SrcMBB,
337 MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I);
338 void recordSccnum(MachineBasicBlock *MBB, int SCCNum);
339 void retireBlock(MachineBasicBlock *MBB);
Craig Topper062a2ba2014-04-25 05:30:21 +0000340 void setLoopLandBlock(MachineLoop *LoopRep, MachineBasicBlock *MBB = nullptr);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000341
342 MachineBasicBlock *findNearestCommonPostDom(std::set<MachineBasicBlock *>&);
Sylvestre Ledru469de192014-08-11 18:04:46 +0000343 /// This is work around solution for findNearestCommonDominator not available
Vincent Lejeune960a6222013-07-19 21:45:06 +0000344 /// to post dom a proper fix should go to Dominators.h.
345 MachineBasicBlock *findNearestCommonPostDom(MachineBasicBlock *MBB1,
346 MachineBasicBlock *MBB2);
347
348private:
349 MBBInfoMap BlockInfoMap;
350 LoopLandInfoMap LLInfoMap;
351 std::map<MachineLoop *, bool> Visited;
352 MachineFunction *FuncRep;
353 SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> OrderedBlks;
354};
355
356int AMDGPUCFGStructurizer::getSCCNum(MachineBasicBlock *MBB) const {
357 MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
358 if (It == BlockInfoMap.end())
359 return INVALIDSCCNUM;
360 return (*It).second->SccNum;
Tom Stellard75aadc22012-12-11 21:25:42 +0000361}
362
Vincent Lejeune960a6222013-07-19 21:45:06 +0000363MachineBasicBlock *AMDGPUCFGStructurizer::getLoopLandInfo(MachineLoop *LoopRep)
364 const {
365 LoopLandInfoMap::const_iterator It = LLInfoMap.find(LoopRep);
366 if (It == LLInfoMap.end())
Craig Topper062a2ba2014-04-25 05:30:21 +0000367 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000368 return (*It).second;
369}
370
371bool AMDGPUCFGStructurizer::hasBackEdge(MachineBasicBlock *MBB) const {
372 MachineLoop *LoopRep = MLI->getLoopFor(MBB);
373 if (!LoopRep)
374 return false;
375 MachineBasicBlock *LoopHeader = LoopRep->getHeader();
376 return MBB->isSuccessor(LoopHeader);
377}
378
379unsigned AMDGPUCFGStructurizer::getLoopDepth(MachineLoop *LoopRep) {
380 return LoopRep ? LoopRep->getLoopDepth() : 0;
381}
382
383bool AMDGPUCFGStructurizer::isRetiredBlock(MachineBasicBlock *MBB) const {
384 MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
385 if (It == BlockInfoMap.end())
386 return false;
387 return (*It).second->IsRetired;
388}
389
390bool AMDGPUCFGStructurizer::isActiveLoophead(MachineBasicBlock *MBB) const {
391 MachineLoop *LoopRep = MLI->getLoopFor(MBB);
392 while (LoopRep && LoopRep->getHeader() == MBB) {
393 MachineBasicBlock *LoopLand = getLoopLandInfo(LoopRep);
394 if(!LoopLand)
395 return true;
396 if (!isRetiredBlock(LoopLand))
397 return true;
398 LoopRep = LoopRep->getParentLoop();
399 }
400 return false;
401}
402AMDGPUCFGStructurizer::PathToKind AMDGPUCFGStructurizer::singlePathTo(
403 MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
404 bool AllowSideEntry) const {
405 assert(DstMBB);
406 if (SrcMBB == DstMBB)
407 return SinglePath_InPath;
408 while (SrcMBB && SrcMBB->succ_size() == 1) {
409 SrcMBB = *SrcMBB->succ_begin();
410 if (SrcMBB == DstMBB)
411 return SinglePath_InPath;
412 if (!AllowSideEntry && SrcMBB->pred_size() > 1)
413 return Not_SinglePath;
414 }
415 if (SrcMBB && SrcMBB->succ_size()==0)
416 return SinglePath_NotInPath;
417 return Not_SinglePath;
418}
419
420int AMDGPUCFGStructurizer::countActiveBlock(MBBVector::const_iterator It,
421 MBBVector::const_iterator E) const {
422 int Count = 0;
423 while (It != E) {
424 if (!isRetiredBlock(*It))
425 ++Count;
426 ++It;
427 }
428 return Count;
429}
430
431bool AMDGPUCFGStructurizer::needMigrateBlock(MachineBasicBlock *MBB) const {
432 unsigned BlockSizeThreshold = 30;
433 unsigned CloneInstrThreshold = 100;
434 bool MultiplePreds = MBB && (MBB->pred_size() > 1);
435
436 if(!MultiplePreds)
437 return false;
438 unsigned BlkSize = MBB->size();
439 return ((BlkSize > BlockSizeThreshold) &&
440 (BlkSize * (MBB->pred_size() - 1) > CloneInstrThreshold));
441}
442
443void AMDGPUCFGStructurizer::reversePredicateSetter(
444 MachineBasicBlock::iterator I) {
445 while (I--) {
446 if (I->getOpcode() == AMDGPU::PRED_X) {
447 switch (static_cast<MachineInstr *>(I)->getOperand(2).getImm()) {
448 case OPCODE_IS_ZERO_INT:
449 static_cast<MachineInstr *>(I)->getOperand(2)
450 .setImm(OPCODE_IS_NOT_ZERO_INT);
451 return;
452 case OPCODE_IS_NOT_ZERO_INT:
453 static_cast<MachineInstr *>(I)->getOperand(2)
454 .setImm(OPCODE_IS_ZERO_INT);
455 return;
456 case OPCODE_IS_ZERO:
457 static_cast<MachineInstr *>(I)->getOperand(2)
458 .setImm(OPCODE_IS_NOT_ZERO);
459 return;
460 case OPCODE_IS_NOT_ZERO:
461 static_cast<MachineInstr *>(I)->getOperand(2)
462 .setImm(OPCODE_IS_ZERO);
463 return;
464 default:
465 llvm_unreachable("PRED_X Opcode invalid!");
466 }
467 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000468 }
469}
470
Vincent Lejeune960a6222013-07-19 21:45:06 +0000471void AMDGPUCFGStructurizer::insertInstrEnd(MachineBasicBlock *MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000472 int NewOpcode, const DebugLoc &DL) {
473 MachineInstr *MI =
474 MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000475 MBB->push_back(MI);
476 //assume the instruction doesn't take any reg operand ...
477 SHOWNEWINSTR(MI);
478}
Tom Stellard75aadc22012-12-11 21:25:42 +0000479
Vincent Lejeune960a6222013-07-19 21:45:06 +0000480MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(MachineBasicBlock *MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000481 int NewOpcode,
482 const DebugLoc &DL) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000483 MachineInstr *MI =
484 MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
485 if (MBB->begin() != MBB->end())
486 MBB->insert(MBB->begin(), MI);
487 else
488 MBB->push_back(MI);
489 SHOWNEWINSTR(MI);
490 return MI;
491}
492
493MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(
494 MachineBasicBlock::iterator I, int NewOpcode) {
495 MachineInstr *OldMI = &(*I);
496 MachineBasicBlock *MBB = OldMI->getParent();
497 MachineInstr *NewMBB =
498 MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DebugLoc());
499 MBB->insert(I, NewMBB);
500 //assume the instruction doesn't take any reg operand ...
501 SHOWNEWINSTR(NewMBB);
502 return NewMBB;
503}
504
505void AMDGPUCFGStructurizer::insertCondBranchBefore(
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000506 MachineBasicBlock::iterator I, int NewOpcode, const DebugLoc &DL) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000507 MachineInstr *OldMI = &(*I);
508 MachineBasicBlock *MBB = OldMI->getParent();
509 MachineFunction *MF = MBB->getParent();
510 MachineInstr *NewMI = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
511 MBB->insert(I, NewMI);
512 MachineInstrBuilder MIB(*MF, NewMI);
513 MIB.addReg(OldMI->getOperand(1).getReg(), false);
514 SHOWNEWINSTR(NewMI);
515 //erase later oldInstr->eraseFromParent();
516}
517
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000518void AMDGPUCFGStructurizer::insertCondBranchBefore(
519 MachineBasicBlock *blk, MachineBasicBlock::iterator I, int NewOpcode,
520 int RegNum, const DebugLoc &DL) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000521 MachineFunction *MF = blk->getParent();
522 MachineInstr *NewInstr = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
523 //insert before
524 blk->insert(I, NewInstr);
525 MachineInstrBuilder(*MF, NewInstr).addReg(RegNum, false);
526 SHOWNEWINSTR(NewInstr);
527}
528
529void AMDGPUCFGStructurizer::insertCondBranchEnd(MachineBasicBlock *MBB,
530 int NewOpcode, int RegNum) {
531 MachineFunction *MF = MBB->getParent();
532 MachineInstr *NewInstr =
533 MF->CreateMachineInstr(TII->get(NewOpcode), DebugLoc());
534 MBB->push_back(NewInstr);
535 MachineInstrBuilder(*MF, NewInstr).addReg(RegNum, false);
536 SHOWNEWINSTR(NewInstr);
537}
538
539int AMDGPUCFGStructurizer::getBranchNzeroOpcode(int OldOpcode) {
540 switch(OldOpcode) {
541 case AMDGPU::JUMP_COND:
542 case AMDGPU::JUMP: return AMDGPU::IF_PREDICATE_SET;
543 case AMDGPU::BRANCH_COND_i32:
544 case AMDGPU::BRANCH_COND_f32: return AMDGPU::IF_LOGICALNZ_f32;
545 default: llvm_unreachable("internal error");
546 }
547 return -1;
548}
549
550int AMDGPUCFGStructurizer::getBranchZeroOpcode(int OldOpcode) {
551 switch(OldOpcode) {
552 case AMDGPU::JUMP_COND:
553 case AMDGPU::JUMP: return AMDGPU::IF_PREDICATE_SET;
554 case AMDGPU::BRANCH_COND_i32:
555 case AMDGPU::BRANCH_COND_f32: return AMDGPU::IF_LOGICALZ_f32;
556 default: llvm_unreachable("internal error");
557 }
558 return -1;
559}
560
561int AMDGPUCFGStructurizer::getContinueNzeroOpcode(int OldOpcode) {
562 switch(OldOpcode) {
563 case AMDGPU::JUMP_COND:
564 case AMDGPU::JUMP: return AMDGPU::CONTINUE_LOGICALNZ_i32;
565 default: llvm_unreachable("internal error");
566 };
567 return -1;
568}
569
570int AMDGPUCFGStructurizer::getContinueZeroOpcode(int OldOpcode) {
571 switch(OldOpcode) {
572 case AMDGPU::JUMP_COND:
573 case AMDGPU::JUMP: return AMDGPU::CONTINUE_LOGICALZ_i32;
574 default: llvm_unreachable("internal error");
575 }
576 return -1;
577}
578
579MachineBasicBlock *AMDGPUCFGStructurizer::getTrueBranch(MachineInstr *MI) {
580 return MI->getOperand(0).getMBB();
581}
582
583void AMDGPUCFGStructurizer::setTrueBranch(MachineInstr *MI,
584 MachineBasicBlock *MBB) {
585 MI->getOperand(0).setMBB(MBB);
586}
587
588MachineBasicBlock *
589AMDGPUCFGStructurizer::getFalseBranch(MachineBasicBlock *MBB,
590 MachineInstr *MI) {
591 assert(MBB->succ_size() == 2);
592 MachineBasicBlock *TrueBranch = getTrueBranch(MI);
593 MachineBasicBlock::succ_iterator It = MBB->succ_begin();
594 MachineBasicBlock::succ_iterator Next = It;
595 ++Next;
596 return (*It == TrueBranch) ? *Next : *It;
597}
598
599bool AMDGPUCFGStructurizer::isCondBranch(MachineInstr *MI) {
600 switch (MI->getOpcode()) {
601 case AMDGPU::JUMP_COND:
602 case AMDGPU::BRANCH_COND_i32:
603 case AMDGPU::BRANCH_COND_f32: return true;
604 default:
605 return false;
606 }
607 return false;
608}
609
610bool AMDGPUCFGStructurizer::isUncondBranch(MachineInstr *MI) {
611 switch (MI->getOpcode()) {
612 case AMDGPU::JUMP:
613 case AMDGPU::BRANCH:
614 return true;
615 default:
616 return false;
617 }
618 return false;
619}
620
621DebugLoc AMDGPUCFGStructurizer::getLastDebugLocInBB(MachineBasicBlock *MBB) {
622 //get DebugLoc from the first MachineBasicBlock instruction with debug info
623 DebugLoc DL;
624 for (MachineBasicBlock::iterator It = MBB->begin(); It != MBB->end();
625 ++It) {
626 MachineInstr *instr = &(*It);
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000627 if (instr->getDebugLoc())
Vincent Lejeune960a6222013-07-19 21:45:06 +0000628 DL = instr->getDebugLoc();
629 }
630 return DL;
631}
632
633MachineInstr *AMDGPUCFGStructurizer::getNormalBlockBranchInstr(
634 MachineBasicBlock *MBB) {
635 MachineBasicBlock::reverse_iterator It = MBB->rbegin();
636 MachineInstr *MI = &*It;
637 if (MI && (isCondBranch(MI) || isUncondBranch(MI)))
638 return MI;
Craig Topper062a2ba2014-04-25 05:30:21 +0000639 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000640}
641
642MachineInstr *AMDGPUCFGStructurizer::getLoopendBlockBranchInstr(
643 MachineBasicBlock *MBB) {
644 for (MachineBasicBlock::reverse_iterator It = MBB->rbegin(), E = MBB->rend();
645 It != E; ++It) {
646 // FIXME: Simplify
647 MachineInstr *MI = &*It;
648 if (MI) {
649 if (isCondBranch(MI) || isUncondBranch(MI))
650 return MI;
651 else if (!TII->isMov(MI->getOpcode()))
652 break;
653 }
654 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000655 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000656}
657
658MachineInstr *AMDGPUCFGStructurizer::getReturnInstr(MachineBasicBlock *MBB) {
659 MachineBasicBlock::reverse_iterator It = MBB->rbegin();
660 if (It != MBB->rend()) {
661 MachineInstr *instr = &(*It);
662 if (instr->getOpcode() == AMDGPU::RETURN)
663 return instr;
664 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000665 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000666}
667
668MachineInstr *AMDGPUCFGStructurizer::getContinueInstr(MachineBasicBlock *MBB) {
669 MachineBasicBlock::reverse_iterator It = MBB->rbegin();
670 if (It != MBB->rend()) {
671 MachineInstr *MI = &(*It);
672 if (MI->getOpcode() == AMDGPU::CONTINUE)
673 return MI;
674 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000675 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000676}
677
Vincent Lejeune960a6222013-07-19 21:45:06 +0000678bool AMDGPUCFGStructurizer::isReturnBlock(MachineBasicBlock *MBB) {
679 MachineInstr *MI = getReturnInstr(MBB);
680 bool IsReturn = (MBB->succ_size() == 0);
681 if (MI)
682 assert(IsReturn);
683 else if (IsReturn)
684 DEBUG(
685 dbgs() << "BB" << MBB->getNumber()
686 <<" is return block without RETURN instr\n";);
687 return IsReturn;
688}
689
690void AMDGPUCFGStructurizer::cloneSuccessorList(MachineBasicBlock *DstMBB,
691 MachineBasicBlock *SrcMBB) {
692 for (MachineBasicBlock::succ_iterator It = SrcMBB->succ_begin(),
693 iterEnd = SrcMBB->succ_end(); It != iterEnd; ++It)
694 DstMBB->addSuccessor(*It); // *iter's predecessor is also taken care of
695}
696
697MachineBasicBlock *AMDGPUCFGStructurizer::clone(MachineBasicBlock *MBB) {
698 MachineFunction *Func = MBB->getParent();
699 MachineBasicBlock *NewMBB = Func->CreateMachineBasicBlock();
700 Func->push_back(NewMBB); //insert to function
701 for (MachineBasicBlock::iterator It = MBB->begin(), E = MBB->end();
702 It != E; ++It) {
703 MachineInstr *MI = Func->CloneMachineInstr(It);
704 NewMBB->push_back(MI);
705 }
706 return NewMBB;
707}
708
709void AMDGPUCFGStructurizer::replaceInstrUseOfBlockWith(
710 MachineBasicBlock *SrcMBB, MachineBasicBlock *OldMBB,
711 MachineBasicBlock *NewBlk) {
712 MachineInstr *BranchMI = getLoopendBlockBranchInstr(SrcMBB);
713 if (BranchMI && isCondBranch(BranchMI) &&
714 getTrueBranch(BranchMI) == OldMBB)
715 setTrueBranch(BranchMI, NewBlk);
716}
717
718void AMDGPUCFGStructurizer::wrapup(MachineBasicBlock *MBB) {
719 assert((!MBB->getParent()->getJumpTableInfo()
720 || MBB->getParent()->getJumpTableInfo()->isEmpty())
721 && "found a jump table");
722
723 //collect continue right before endloop
724 SmallVector<MachineInstr *, DEFAULT_VEC_SLOTS> ContInstr;
725 MachineBasicBlock::iterator Pre = MBB->begin();
726 MachineBasicBlock::iterator E = MBB->end();
727 MachineBasicBlock::iterator It = Pre;
728 while (It != E) {
729 if (Pre->getOpcode() == AMDGPU::CONTINUE
730 && It->getOpcode() == AMDGPU::ENDLOOP)
731 ContInstr.push_back(Pre);
732 Pre = It;
733 ++It;
734 }
735
736 //delete continue right before endloop
737 for (unsigned i = 0; i < ContInstr.size(); ++i)
738 ContInstr[i]->eraseFromParent();
739
740 // TODO to fix up jump table so later phase won't be confused. if
741 // (jumpTableInfo->isEmpty() == false) { need to clean the jump table, but
742 // there isn't such an interface yet. alternatively, replace all the other
743 // blocks in the jump table with the entryBlk //}
744
745}
746
747
748bool AMDGPUCFGStructurizer::prepare() {
749 bool Changed = false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000750
751 //FIXME: if not reducible flow graph, make it so ???
752
Vincent Lejeune960a6222013-07-19 21:45:06 +0000753 DEBUG(dbgs() << "AMDGPUCFGStructurizer::prepare\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000754
Vincent Lejeune960a6222013-07-19 21:45:06 +0000755 orderBlocks(FuncRep);
Tom Stellard75aadc22012-12-11 21:25:42 +0000756
Vincent Lejeune960a6222013-07-19 21:45:06 +0000757 SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> RetBlks;
Tom Stellard75aadc22012-12-11 21:25:42 +0000758
Vincent Lejeune960a6222013-07-19 21:45:06 +0000759 // Add an ExitBlk to loop that don't have one
760 for (MachineLoopInfo::iterator It = MLI->begin(),
761 E = MLI->end(); It != E; ++It) {
762 MachineLoop *LoopRep = (*It);
763 MBBVector ExitingMBBs;
764 LoopRep->getExitingBlocks(ExitingMBBs);
Tom Stellard75aadc22012-12-11 21:25:42 +0000765
Vincent Lejeune960a6222013-07-19 21:45:06 +0000766 if (ExitingMBBs.size() == 0) {
767 MachineBasicBlock* DummyExitBlk = normalizeInfiniteLoopExit(LoopRep);
768 if (DummyExitBlk)
769 RetBlks.push_back(DummyExitBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +0000770 }
771 }
772
773 // Remove unconditional branch instr.
774 // Add dummy exit block iff there are multiple returns.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000775 for (SmallVectorImpl<MachineBasicBlock *>::const_iterator
776 It = OrderedBlks.begin(), E = OrderedBlks.end(); It != E; ++It) {
777 MachineBasicBlock *MBB = *It;
778 removeUnconditionalBranch(MBB);
779 removeRedundantConditionalBranch(MBB);
780 if (isReturnBlock(MBB)) {
781 RetBlks.push_back(MBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000782 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000783 assert(MBB->succ_size() <= 2);
Tom Stellard75aadc22012-12-11 21:25:42 +0000784 }
785
Vincent Lejeune960a6222013-07-19 21:45:06 +0000786 if (RetBlks.size() >= 2) {
787 addDummyExitBlock(RetBlks);
788 Changed = true;
789 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000790
Vincent Lejeune960a6222013-07-19 21:45:06 +0000791 return Changed;
792}
793
794bool AMDGPUCFGStructurizer::run() {
Tom Stellard75aadc22012-12-11 21:25:42 +0000795
796 //Assume reducible CFG...
Matt Arsenaultdb8b1d52014-03-24 20:29:02 +0000797 DEBUG(dbgs() << "AMDGPUCFGStructurizer::run\n");
Tom Stellard75aadc22012-12-11 21:25:42 +0000798
Tom Stellard75aadc22012-12-11 21:25:42 +0000799#ifdef STRESSTEST
800 //Use the worse block ordering to test the algorithm.
801 ReverseVector(orderedBlks);
802#endif
803
Vincent Lejeune960a6222013-07-19 21:45:06 +0000804 DEBUG(dbgs() << "Ordered blocks:\n"; printOrderedBlocks(););
805 int NumIter = 0;
806 bool Finish = false;
807 MachineBasicBlock *MBB;
808 bool MakeProgress = false;
809 int NumRemainedBlk = countActiveBlock(OrderedBlks.begin(),
810 OrderedBlks.end());
Tom Stellard75aadc22012-12-11 21:25:42 +0000811
812 do {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000813 ++NumIter;
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000814 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000815 dbgs() << "numIter = " << NumIter
816 << ", numRemaintedBlk = " << NumRemainedBlk << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000817 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000818
Vincent Lejeune960a6222013-07-19 21:45:06 +0000819 SmallVectorImpl<MachineBasicBlock *>::const_iterator It =
820 OrderedBlks.begin();
821 SmallVectorImpl<MachineBasicBlock *>::const_iterator E =
822 OrderedBlks.end();
Tom Stellard75aadc22012-12-11 21:25:42 +0000823
Vincent Lejeune960a6222013-07-19 21:45:06 +0000824 SmallVectorImpl<MachineBasicBlock *>::const_iterator SccBeginIter =
825 It;
Craig Topper062a2ba2014-04-25 05:30:21 +0000826 MachineBasicBlock *SccBeginMBB = nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000827 int SccNumBlk = 0; // The number of active blocks, init to a
Tom Stellard75aadc22012-12-11 21:25:42 +0000828 // maximum possible number.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000829 int SccNumIter; // Number of iteration in this SCC.
Tom Stellard75aadc22012-12-11 21:25:42 +0000830
Vincent Lejeune960a6222013-07-19 21:45:06 +0000831 while (It != E) {
832 MBB = *It;
Tom Stellard75aadc22012-12-11 21:25:42 +0000833
Vincent Lejeune960a6222013-07-19 21:45:06 +0000834 if (!SccBeginMBB) {
835 SccBeginIter = It;
836 SccBeginMBB = MBB;
837 SccNumIter = 0;
838 SccNumBlk = NumRemainedBlk; // Init to maximum possible number.
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000839 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000840 dbgs() << "start processing SCC" << getSCCNum(SccBeginMBB);
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000841 dbgs() << "\n";
842 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000843 }
844
Vincent Lejeune960a6222013-07-19 21:45:06 +0000845 if (!isRetiredBlock(MBB))
846 patternMatch(MBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000847
Vincent Lejeune960a6222013-07-19 21:45:06 +0000848 ++It;
Tom Stellard75aadc22012-12-11 21:25:42 +0000849
Vincent Lejeune960a6222013-07-19 21:45:06 +0000850 bool ContNextScc = true;
851 if (It == E
852 || getSCCNum(SccBeginMBB) != getSCCNum(*It)) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000853 // Just finish one scc.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000854 ++SccNumIter;
855 int sccRemainedNumBlk = countActiveBlock(SccBeginIter, It);
856 if (sccRemainedNumBlk != 1 && sccRemainedNumBlk >= SccNumBlk) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000857 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000858 dbgs() << "Can't reduce SCC " << getSCCNum(MBB)
859 << ", sccNumIter = " << SccNumIter;
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000860 dbgs() << "doesn't make any progress\n";
861 );
Vincent Lejeune960a6222013-07-19 21:45:06 +0000862 ContNextScc = true;
863 } else if (sccRemainedNumBlk != 1 && sccRemainedNumBlk < SccNumBlk) {
864 SccNumBlk = sccRemainedNumBlk;
865 It = SccBeginIter;
866 ContNextScc = false;
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000867 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000868 dbgs() << "repeat processing SCC" << getSCCNum(MBB)
Matt Arsenaultdb8b1d52014-03-24 20:29:02 +0000869 << "sccNumIter = " << SccNumIter << '\n';
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000870 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000871 } else {
872 // Finish the current scc.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000873 ContNextScc = true;
Tom Stellard75aadc22012-12-11 21:25:42 +0000874 }
875 } else {
876 // Continue on next component in the current scc.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000877 ContNextScc = false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000878 }
879
Vincent Lejeune960a6222013-07-19 21:45:06 +0000880 if (ContNextScc)
Craig Topper062a2ba2014-04-25 05:30:21 +0000881 SccBeginMBB = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +0000882 } //while, "one iteration" over the function.
883
Vincent Lejeune960a6222013-07-19 21:45:06 +0000884 MachineBasicBlock *EntryMBB =
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000885 &*GraphTraits<MachineFunction *>::nodes_begin(FuncRep);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000886 if (EntryMBB->succ_size() == 0) {
887 Finish = true;
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000888 DEBUG(
889 dbgs() << "Reduce to one block\n";
890 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000891 } else {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000892 int NewnumRemainedBlk
893 = countActiveBlock(OrderedBlks.begin(), OrderedBlks.end());
Tom Stellard75aadc22012-12-11 21:25:42 +0000894 // consider cloned blocks ??
Vincent Lejeune960a6222013-07-19 21:45:06 +0000895 if (NewnumRemainedBlk == 1 || NewnumRemainedBlk < NumRemainedBlk) {
896 MakeProgress = true;
897 NumRemainedBlk = NewnumRemainedBlk;
Tom Stellard75aadc22012-12-11 21:25:42 +0000898 } else {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000899 MakeProgress = false;
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000900 DEBUG(
901 dbgs() << "No progress\n";
902 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000903 }
904 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000905 } while (!Finish && MakeProgress);
Tom Stellard75aadc22012-12-11 21:25:42 +0000906
907 // Misc wrap up to maintain the consistency of the Function representation.
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000908 wrapup(&*GraphTraits<MachineFunction *>::nodes_begin(FuncRep));
Tom Stellard75aadc22012-12-11 21:25:42 +0000909
910 // Detach retired Block, release memory.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000911 for (MBBInfoMap::iterator It = BlockInfoMap.begin(), E = BlockInfoMap.end();
912 It != E; ++It) {
913 if ((*It).second && (*It).second->IsRetired) {
914 assert(((*It).first)->getNumber() != -1);
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000915 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000916 dbgs() << "Erase BB" << ((*It).first)->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000917 );
Vincent Lejeune960a6222013-07-19 21:45:06 +0000918 (*It).first->eraseFromParent(); //Remove from the parent Function.
Tom Stellard75aadc22012-12-11 21:25:42 +0000919 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000920 delete (*It).second;
Tom Stellard75aadc22012-12-11 21:25:42 +0000921 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000922 BlockInfoMap.clear();
923 LLInfoMap.clear();
Tom Stellard75aadc22012-12-11 21:25:42 +0000924
Matt Arsenaultdb8b1d52014-03-24 20:29:02 +0000925 if (!Finish) {
926 DEBUG(FuncRep->viewCFG());
Matt Arsenault5de68cb2016-03-02 03:33:55 +0000927 report_fatal_error("IRREDUCIBLE_CFG");
Matt Arsenaultdb8b1d52014-03-24 20:29:02 +0000928 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000929
930 return true;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000931}
Tom Stellard75aadc22012-12-11 21:25:42 +0000932
Tom Stellard75aadc22012-12-11 21:25:42 +0000933
Vincent Lejeune960a6222013-07-19 21:45:06 +0000934
935void AMDGPUCFGStructurizer::orderBlocks(MachineFunction *MF) {
936 int SccNum = 0;
937 MachineBasicBlock *MBB;
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000938 for (scc_iterator<MachineFunction *> It = scc_begin(MF); !It.isAtEnd();
939 ++It, ++SccNum) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000940 const std::vector<MachineBasicBlock *> &SccNext = *It;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000941 for (std::vector<MachineBasicBlock *>::const_iterator
942 blockIter = SccNext.begin(), blockEnd = SccNext.end();
Tom Stellard75aadc22012-12-11 21:25:42 +0000943 blockIter != blockEnd; ++blockIter) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000944 MBB = *blockIter;
945 OrderedBlks.push_back(MBB);
946 recordSccnum(MBB, SccNum);
Tom Stellard75aadc22012-12-11 21:25:42 +0000947 }
948 }
949
950 //walk through all the block in func to check for unreachable
Vincent Lejeune960a6222013-07-19 21:45:06 +0000951 typedef GraphTraits<MachineFunction *> GTM;
952 MachineFunction::iterator It = GTM::nodes_begin(MF), E = GTM::nodes_end(MF);
953 for (; It != E; ++It) {
954 MachineBasicBlock *MBB = &(*It);
955 SccNum = getSCCNum(MBB);
956 if (SccNum == INVALIDSCCNUM)
957 dbgs() << "unreachable block BB" << MBB->getNumber() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +0000958 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000959}
Tom Stellard75aadc22012-12-11 21:25:42 +0000960
Vincent Lejeune960a6222013-07-19 21:45:06 +0000961int AMDGPUCFGStructurizer::patternMatch(MachineBasicBlock *MBB) {
962 int NumMatch = 0;
963 int CurMatch;
Tom Stellard75aadc22012-12-11 21:25:42 +0000964
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000965 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000966 dbgs() << "Begin patternMatch BB" << MBB->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000967 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000968
Vincent Lejeune960a6222013-07-19 21:45:06 +0000969 while ((CurMatch = patternMatchGroup(MBB)) > 0)
970 NumMatch += CurMatch;
Tom Stellard75aadc22012-12-11 21:25:42 +0000971
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000972 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +0000973 dbgs() << "End patternMatch BB" << MBB->getNumber()
974 << ", numMatch = " << NumMatch << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +0000975 );
Tom Stellard75aadc22012-12-11 21:25:42 +0000976
Vincent Lejeune960a6222013-07-19 21:45:06 +0000977 return NumMatch;
978}
Tom Stellard75aadc22012-12-11 21:25:42 +0000979
Vincent Lejeune960a6222013-07-19 21:45:06 +0000980int AMDGPUCFGStructurizer::patternMatchGroup(MachineBasicBlock *MBB) {
981 int NumMatch = 0;
982 NumMatch += loopendPatternMatch();
983 NumMatch += serialPatternMatch(MBB);
984 NumMatch += ifPatternMatch(MBB);
985 return NumMatch;
986}
Tom Stellard75aadc22012-12-11 21:25:42 +0000987
Vincent Lejeune960a6222013-07-19 21:45:06 +0000988
989int AMDGPUCFGStructurizer::serialPatternMatch(MachineBasicBlock *MBB) {
990 if (MBB->succ_size() != 1)
Tom Stellard75aadc22012-12-11 21:25:42 +0000991 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000992
Vincent Lejeune960a6222013-07-19 21:45:06 +0000993 MachineBasicBlock *childBlk = *MBB->succ_begin();
994 if (childBlk->pred_size() != 1 || isActiveLoophead(childBlk))
Tom Stellard75aadc22012-12-11 21:25:42 +0000995 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000996
Vincent Lejeune960a6222013-07-19 21:45:06 +0000997 mergeSerialBlock(MBB, childBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +0000998 ++numSerialPatternMatch;
999 return 1;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001000}
Tom Stellard75aadc22012-12-11 21:25:42 +00001001
Vincent Lejeune960a6222013-07-19 21:45:06 +00001002int AMDGPUCFGStructurizer::ifPatternMatch(MachineBasicBlock *MBB) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001003 //two edges
Vincent Lejeune960a6222013-07-19 21:45:06 +00001004 if (MBB->succ_size() != 2)
Tom Stellard75aadc22012-12-11 21:25:42 +00001005 return 0;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001006 if (hasBackEdge(MBB))
Tom Stellard75aadc22012-12-11 21:25:42 +00001007 return 0;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001008 MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
1009 if (!BranchMI)
Tom Stellard75aadc22012-12-11 21:25:42 +00001010 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001011
Vincent Lejeune960a6222013-07-19 21:45:06 +00001012 assert(isCondBranch(BranchMI));
Tom Stellard827ec9b2013-11-18 19:43:38 +00001013 int NumMatch = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001014
Vincent Lejeune960a6222013-07-19 21:45:06 +00001015 MachineBasicBlock *TrueMBB = getTrueBranch(BranchMI);
Tom Stellard827ec9b2013-11-18 19:43:38 +00001016 NumMatch += serialPatternMatch(TrueMBB);
1017 NumMatch += ifPatternMatch(TrueMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001018 MachineBasicBlock *FalseMBB = getFalseBranch(MBB, BranchMI);
Tom Stellard827ec9b2013-11-18 19:43:38 +00001019 NumMatch += serialPatternMatch(FalseMBB);
1020 NumMatch += ifPatternMatch(FalseMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001021 MachineBasicBlock *LandBlk;
1022 int Cloned = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001023
Vincent Lejeune960a6222013-07-19 21:45:06 +00001024 assert (!TrueMBB->succ_empty() || !FalseMBB->succ_empty());
Tom Stellard75aadc22012-12-11 21:25:42 +00001025 // TODO: Simplify
Vincent Lejeune960a6222013-07-19 21:45:06 +00001026 if (TrueMBB->succ_size() == 1 && FalseMBB->succ_size() == 1
1027 && *TrueMBB->succ_begin() == *FalseMBB->succ_begin()) {
1028 // Diamond pattern
1029 LandBlk = *TrueMBB->succ_begin();
1030 } else if (TrueMBB->succ_size() == 1 && *TrueMBB->succ_begin() == FalseMBB) {
1031 // Triangle pattern, false is empty
1032 LandBlk = FalseMBB;
Craig Topper062a2ba2014-04-25 05:30:21 +00001033 FalseMBB = nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001034 } else if (FalseMBB->succ_size() == 1
1035 && *FalseMBB->succ_begin() == TrueMBB) {
1036 // Triangle pattern, true is empty
Vincent Lejeune8b8a7b52013-07-19 21:45:15 +00001037 // We reverse the predicate to make a triangle, empty false pattern;
1038 std::swap(TrueMBB, FalseMBB);
1039 reversePredicateSetter(MBB->end());
1040 LandBlk = FalseMBB;
Craig Topper062a2ba2014-04-25 05:30:21 +00001041 FalseMBB = nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001042 } else if (FalseMBB->succ_size() == 1
1043 && isSameloopDetachedContbreak(TrueMBB, FalseMBB)) {
1044 LandBlk = *FalseMBB->succ_begin();
1045 } else if (TrueMBB->succ_size() == 1
1046 && isSameloopDetachedContbreak(FalseMBB, TrueMBB)) {
1047 LandBlk = *TrueMBB->succ_begin();
Tom Stellard75aadc22012-12-11 21:25:42 +00001048 } else {
Tom Stellard827ec9b2013-11-18 19:43:38 +00001049 return NumMatch + handleJumpintoIf(MBB, TrueMBB, FalseMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001050 }
1051
1052 // improveSimpleJumpinfoIf can handle the case where landBlk == NULL but the
1053 // new BB created for landBlk==NULL may introduce new challenge to the
1054 // reduction process.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001055 if (LandBlk &&
1056 ((TrueMBB && TrueMBB->pred_size() > 1)
1057 || (FalseMBB && FalseMBB->pred_size() > 1))) {
1058 Cloned += improveSimpleJumpintoIf(MBB, TrueMBB, FalseMBB, &LandBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +00001059 }
1060
Vincent Lejeune960a6222013-07-19 21:45:06 +00001061 if (TrueMBB && TrueMBB->pred_size() > 1) {
1062 TrueMBB = cloneBlockForPredecessor(TrueMBB, MBB);
1063 ++Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +00001064 }
1065
Vincent Lejeune960a6222013-07-19 21:45:06 +00001066 if (FalseMBB && FalseMBB->pred_size() > 1) {
1067 FalseMBB = cloneBlockForPredecessor(FalseMBB, MBB);
1068 ++Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +00001069 }
1070
Vincent Lejeune960a6222013-07-19 21:45:06 +00001071 mergeIfthenelseBlock(BranchMI, MBB, TrueMBB, FalseMBB, LandBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +00001072
1073 ++numIfPatternMatch;
1074
Vincent Lejeune960a6222013-07-19 21:45:06 +00001075 numClonedBlock += Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +00001076
Tom Stellard827ec9b2013-11-18 19:43:38 +00001077 return 1 + Cloned + NumMatch;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001078}
Tom Stellard75aadc22012-12-11 21:25:42 +00001079
Vincent Lejeune960a6222013-07-19 21:45:06 +00001080int AMDGPUCFGStructurizer::loopendPatternMatch() {
Jan Vesely18b289f2015-03-13 17:32:43 +00001081 std::deque<MachineLoop *> NestedLoops;
1082 for (auto &It: *MLI)
1083 for (MachineLoop *ML : depth_first(It))
1084 NestedLoops.push_front(ML);
David Blaikieceec2bd2014-04-11 01:50:01 +00001085
Vincent Lejeune960a6222013-07-19 21:45:06 +00001086 if (NestedLoops.size() == 0)
Tom Stellard75aadc22012-12-11 21:25:42 +00001087 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001088
Jan Vesely18b289f2015-03-13 17:32:43 +00001089 // Process nested loop outside->inside (we did push_front),
1090 // so "continue" to a outside loop won't be mistaken as "break"
1091 // of the current loop.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001092 int Num = 0;
Jan Vesely18b289f2015-03-13 17:32:43 +00001093 for (MachineLoop *ExaminedLoop : NestedLoops) {
Vincent Lejeune960a6222013-07-19 21:45:06 +00001094 if (ExaminedLoop->getNumBlocks() == 0 || Visited[ExaminedLoop])
Tom Stellard75aadc22012-12-11 21:25:42 +00001095 continue;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001096 DEBUG(dbgs() << "Processing:\n"; ExaminedLoop->dump(););
1097 int NumBreak = mergeLoop(ExaminedLoop);
1098 if (NumBreak == -1)
Tom Stellard75aadc22012-12-11 21:25:42 +00001099 break;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001100 Num += NumBreak;
1101 }
1102 return Num;
1103}
Tom Stellard75aadc22012-12-11 21:25:42 +00001104
Vincent Lejeune960a6222013-07-19 21:45:06 +00001105int AMDGPUCFGStructurizer::mergeLoop(MachineLoop *LoopRep) {
1106 MachineBasicBlock *LoopHeader = LoopRep->getHeader();
1107 MBBVector ExitingMBBs;
1108 LoopRep->getExitingBlocks(ExitingMBBs);
1109 assert(!ExitingMBBs.empty() && "Infinite Loop not supported");
1110 DEBUG(dbgs() << "Loop has " << ExitingMBBs.size() << " exiting blocks\n";);
1111 // We assume a single ExitBlk
1112 MBBVector ExitBlks;
1113 LoopRep->getExitBlocks(ExitBlks);
1114 SmallPtrSet<MachineBasicBlock *, 2> ExitBlkSet;
1115 for (unsigned i = 0, e = ExitBlks.size(); i < e; ++i)
1116 ExitBlkSet.insert(ExitBlks[i]);
1117 assert(ExitBlkSet.size() == 1);
1118 MachineBasicBlock *ExitBlk = *ExitBlks.begin();
1119 assert(ExitBlk && "Loop has several exit block");
1120 MBBVector LatchBlks;
1121 typedef GraphTraits<Inverse<MachineBasicBlock*> > InvMBBTraits;
1122 InvMBBTraits::ChildIteratorType PI = InvMBBTraits::child_begin(LoopHeader),
1123 PE = InvMBBTraits::child_end(LoopHeader);
1124 for (; PI != PE; PI++) {
1125 if (LoopRep->contains(*PI))
1126 LatchBlks.push_back(*PI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001127 }
1128
Vincent Lejeune960a6222013-07-19 21:45:06 +00001129 for (unsigned i = 0, e = ExitingMBBs.size(); i < e; ++i)
1130 mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk);
1131 for (unsigned i = 0, e = LatchBlks.size(); i < e; ++i)
1132 settleLoopcontBlock(LatchBlks[i], LoopHeader);
1133 int Match = 0;
1134 do {
1135 Match = 0;
1136 Match += serialPatternMatch(LoopHeader);
1137 Match += ifPatternMatch(LoopHeader);
1138 } while (Match > 0);
1139 mergeLooplandBlock(LoopHeader, ExitBlk);
1140 MachineLoop *ParentLoop = LoopRep->getParentLoop();
1141 if (ParentLoop)
1142 MLI->changeLoopFor(LoopHeader, ParentLoop);
1143 else
1144 MLI->removeBlock(LoopHeader);
1145 Visited[LoopRep] = true;
1146 return 1;
1147}
Tom Stellard75aadc22012-12-11 21:25:42 +00001148
Vincent Lejeune960a6222013-07-19 21:45:06 +00001149int AMDGPUCFGStructurizer::loopcontPatternMatch(MachineLoop *LoopRep,
1150 MachineBasicBlock *LoopHeader) {
1151 int NumCont = 0;
1152 SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> ContMBB;
1153 typedef GraphTraits<Inverse<MachineBasicBlock *> > GTIM;
1154 GTIM::ChildIteratorType It = GTIM::child_begin(LoopHeader),
1155 E = GTIM::child_end(LoopHeader);
1156 for (; It != E; ++It) {
1157 MachineBasicBlock *MBB = *It;
1158 if (LoopRep->contains(MBB)) {
1159 handleLoopcontBlock(MBB, MLI->getLoopFor(MBB),
1160 LoopHeader, LoopRep);
1161 ContMBB.push_back(MBB);
1162 ++NumCont;
Tom Stellard75aadc22012-12-11 21:25:42 +00001163 }
1164 }
1165
Vincent Lejeune960a6222013-07-19 21:45:06 +00001166 for (SmallVectorImpl<MachineBasicBlock *>::iterator It = ContMBB.begin(),
1167 E = ContMBB.end(); It != E; ++It) {
Cong Houc1069892015-12-13 09:26:17 +00001168 (*It)->removeSuccessor(LoopHeader, true);
Tom Stellard75aadc22012-12-11 21:25:42 +00001169 }
1170
Vincent Lejeune960a6222013-07-19 21:45:06 +00001171 numLoopcontPatternMatch += NumCont;
Tom Stellard75aadc22012-12-11 21:25:42 +00001172
Vincent Lejeune960a6222013-07-19 21:45:06 +00001173 return NumCont;
1174}
Tom Stellard75aadc22012-12-11 21:25:42 +00001175
1176
Vincent Lejeune960a6222013-07-19 21:45:06 +00001177bool AMDGPUCFGStructurizer::isSameloopDetachedContbreak(
1178 MachineBasicBlock *Src1MBB, MachineBasicBlock *Src2MBB) {
1179 if (Src1MBB->succ_size() == 0) {
1180 MachineLoop *LoopRep = MLI->getLoopFor(Src1MBB);
1181 if (LoopRep&& LoopRep == MLI->getLoopFor(Src2MBB)) {
1182 MachineBasicBlock *&TheEntry = LLInfoMap[LoopRep];
1183 if (TheEntry) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001184 DEBUG(
1185 dbgs() << "isLoopContBreakBlock yes src1 = BB"
Vincent Lejeune960a6222013-07-19 21:45:06 +00001186 << Src1MBB->getNumber()
1187 << " src2 = BB" << Src2MBB->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001188 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001189 return true;
1190 }
1191 }
1192 }
1193 return false;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001194}
Tom Stellard75aadc22012-12-11 21:25:42 +00001195
Vincent Lejeune960a6222013-07-19 21:45:06 +00001196int AMDGPUCFGStructurizer::handleJumpintoIf(MachineBasicBlock *HeadMBB,
1197 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
1198 int Num = handleJumpintoIfImp(HeadMBB, TrueMBB, FalseMBB);
1199 if (Num == 0) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001200 DEBUG(
1201 dbgs() << "handleJumpintoIf swap trueBlk and FalseBlk" << "\n";
1202 );
Vincent Lejeune960a6222013-07-19 21:45:06 +00001203 Num = handleJumpintoIfImp(HeadMBB, FalseMBB, TrueMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001204 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001205 return Num;
Tom Stellard75aadc22012-12-11 21:25:42 +00001206}
1207
Vincent Lejeune960a6222013-07-19 21:45:06 +00001208int AMDGPUCFGStructurizer::handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
1209 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
1210 int Num = 0;
1211 MachineBasicBlock *DownBlk;
Tom Stellard75aadc22012-12-11 21:25:42 +00001212
1213 //trueBlk could be the common post dominator
Vincent Lejeune960a6222013-07-19 21:45:06 +00001214 DownBlk = TrueMBB;
Tom Stellard75aadc22012-12-11 21:25:42 +00001215
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001216 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001217 dbgs() << "handleJumpintoIfImp head = BB" << HeadMBB->getNumber()
1218 << " true = BB" << TrueMBB->getNumber()
1219 << ", numSucc=" << TrueMBB->succ_size()
1220 << " false = BB" << FalseMBB->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001221 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001222
Vincent Lejeune960a6222013-07-19 21:45:06 +00001223 while (DownBlk) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001224 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001225 dbgs() << "check down = BB" << DownBlk->getNumber();
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001226 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001227
Vincent Lejeune960a6222013-07-19 21:45:06 +00001228 if (singlePathTo(FalseMBB, DownBlk) == SinglePath_InPath) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001229 DEBUG(
1230 dbgs() << " working\n";
1231 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001232
Vincent Lejeune960a6222013-07-19 21:45:06 +00001233 Num += cloneOnSideEntryTo(HeadMBB, TrueMBB, DownBlk);
1234 Num += cloneOnSideEntryTo(HeadMBB, FalseMBB, DownBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +00001235
Vincent Lejeune960a6222013-07-19 21:45:06 +00001236 numClonedBlock += Num;
1237 Num += serialPatternMatch(*HeadMBB->succ_begin());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001238 Num += serialPatternMatch(*std::next(HeadMBB->succ_begin()));
Vincent Lejeune960a6222013-07-19 21:45:06 +00001239 Num += ifPatternMatch(HeadMBB);
1240 assert(Num > 0);
Tom Stellard75aadc22012-12-11 21:25:42 +00001241
1242 break;
1243 }
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001244 DEBUG(
1245 dbgs() << " not working\n";
1246 );
Craig Topper062a2ba2014-04-25 05:30:21 +00001247 DownBlk = (DownBlk->succ_size() == 1) ? (*DownBlk->succ_begin()) : nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001248 } // walk down the postDomTree
1249
Vincent Lejeune960a6222013-07-19 21:45:06 +00001250 return Num;
1251}
Tom Stellard75aadc22012-12-11 21:25:42 +00001252
Vincent Lejeune960a6222013-07-19 21:45:06 +00001253void AMDGPUCFGStructurizer::showImproveSimpleJumpintoIf(
1254 MachineBasicBlock *HeadMBB, MachineBasicBlock *TrueMBB,
1255 MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB, bool Detail) {
1256 dbgs() << "head = BB" << HeadMBB->getNumber()
1257 << " size = " << HeadMBB->size();
1258 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001259 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001260 HeadMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001261 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001262 }
1263
Vincent Lejeune960a6222013-07-19 21:45:06 +00001264 if (TrueMBB) {
1265 dbgs() << ", true = BB" << TrueMBB->getNumber() << " size = "
1266 << TrueMBB->size() << " numPred = " << TrueMBB->pred_size();
1267 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001268 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001269 TrueMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001270 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001271 }
1272 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001273 if (FalseMBB) {
1274 dbgs() << ", false = BB" << FalseMBB->getNumber() << " size = "
1275 << FalseMBB->size() << " numPred = " << FalseMBB->pred_size();
1276 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001277 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001278 FalseMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001279 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001280 }
1281 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001282 if (LandMBB) {
1283 dbgs() << ", land = BB" << LandMBB->getNumber() << " size = "
1284 << LandMBB->size() << " numPred = " << LandMBB->pred_size();
1285 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001286 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001287 LandMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001288 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001289 }
1290 }
1291
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001292 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001293}
Tom Stellard75aadc22012-12-11 21:25:42 +00001294
Vincent Lejeune960a6222013-07-19 21:45:06 +00001295int AMDGPUCFGStructurizer::improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
1296 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
1297 MachineBasicBlock **LandMBBPtr) {
1298 bool MigrateTrue = false;
1299 bool MigrateFalse = false;
Tom Stellard75aadc22012-12-11 21:25:42 +00001300
Vincent Lejeune960a6222013-07-19 21:45:06 +00001301 MachineBasicBlock *LandBlk = *LandMBBPtr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001302
Vincent Lejeune960a6222013-07-19 21:45:06 +00001303 assert((!TrueMBB || TrueMBB->succ_size() <= 1)
1304 && (!FalseMBB || FalseMBB->succ_size() <= 1));
Tom Stellard75aadc22012-12-11 21:25:42 +00001305
Vincent Lejeune960a6222013-07-19 21:45:06 +00001306 if (TrueMBB == FalseMBB)
Tom Stellard75aadc22012-12-11 21:25:42 +00001307 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001308
Vincent Lejeune960a6222013-07-19 21:45:06 +00001309 MigrateTrue = needMigrateBlock(TrueMBB);
1310 MigrateFalse = needMigrateBlock(FalseMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001311
Vincent Lejeune960a6222013-07-19 21:45:06 +00001312 if (!MigrateTrue && !MigrateFalse)
Tom Stellard75aadc22012-12-11 21:25:42 +00001313 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001314
1315 // If we need to migrate either trueBlk and falseBlk, migrate the rest that
1316 // have more than one predecessors. without doing this, its predecessor
1317 // rather than headBlk will have undefined value in initReg.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001318 if (!MigrateTrue && TrueMBB && TrueMBB->pred_size() > 1)
1319 MigrateTrue = true;
1320 if (!MigrateFalse && FalseMBB && FalseMBB->pred_size() > 1)
1321 MigrateFalse = true;
Tom Stellard75aadc22012-12-11 21:25:42 +00001322
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001323 DEBUG(
1324 dbgs() << "before improveSimpleJumpintoIf: ";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001325 showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001326 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001327
1328 // org: headBlk => if () {trueBlk} else {falseBlk} => landBlk
1329 //
1330 // new: headBlk => if () {initReg = 1; org trueBlk branch} else
1331 // {initReg = 0; org falseBlk branch }
1332 // => landBlk => if (initReg) {org trueBlk} else {org falseBlk}
1333 // => org landBlk
1334 // if landBlk->pred_size() > 2, put the about if-else inside
1335 // if (initReg !=2) {...}
1336 //
1337 // add initReg = initVal to headBlk
1338
1339 const TargetRegisterClass * I32RC = TRI->getCFGStructurizerRegClass(MVT::i32);
Tom Stellardb34186a2013-10-16 17:06:02 +00001340 if (!MigrateTrue || !MigrateFalse) {
1341 // XXX: We have an opportunity here to optimize the "branch into if" case
1342 // here. Branch into if looks like this:
1343 // entry
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001344 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001345 // diamond_head branch_from
1346 // / \ |
1347 // diamond_false diamond_true
1348 // \ /
1349 // done
1350 //
1351 // The diamond_head block begins the "if" and the diamond_true block
1352 // is the block being "branched into".
1353 //
1354 // If MigrateTrue is true, then TrueBB is the block being "branched into"
1355 // and if MigrateFalse is true, then FalseBB is the block being
1356 // "branched into"
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001357 //
Tom Stellardb34186a2013-10-16 17:06:02 +00001358 // Here is the pseudo code for how I think the optimization should work:
1359 // 1. Insert MOV GPR0, 0 before the branch instruction in diamond_head.
1360 // 2. Insert MOV GPR0, 1 before the branch instruction in branch_from.
1361 // 3. Move the branch instruction from diamond_head into its own basic
1362 // block (new_block).
1363 // 4. Add an unconditional branch from diamond_head to new_block
1364 // 5. Replace the branch instruction in branch_from with an unconditional
1365 // branch to new_block. If branch_from has multiple predecessors, then
1366 // we need to replace the True/False block in the branch
1367 // instruction instead of replacing it.
1368 // 6. Change the condition of the branch instruction in new_block from
1369 // COND to (COND || GPR0)
1370 //
1371 // In order insert these MOV instruction, we will need to use the
1372 // RegisterScavenger. Usually liveness stops being tracked during
1373 // the late machine optimization passes, however if we implement
1374 // bool TargetRegisterInfo::requiresRegisterScavenging(
1375 // const MachineFunction &MF)
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001376 // and have it return true, liveness will be tracked correctly
Tom Stellardb34186a2013-10-16 17:06:02 +00001377 // by generic optimization passes. We will also need to make sure that
1378 // all of our target-specific passes that run after regalloc and before
1379 // the CFGStructurizer track liveness and we will need to modify this pass
1380 // to correctly track liveness.
1381 //
1382 // After the above changes, the new CFG should look like this:
1383 // entry
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001384 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001385 // diamond_head branch_from
1386 // \ /
1387 // new_block
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001388 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001389 // diamond_false diamond_true
1390 // \ /
1391 // done
1392 //
1393 // Without this optimization, we are forced to duplicate the diamond_true
1394 // block and we will end up with a CFG like this:
1395 //
1396 // entry
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001397 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001398 // diamond_head branch_from
1399 // / \ |
1400 // diamond_false diamond_true diamond_true (duplicate)
1401 // \ / |
1402 // done --------------------|
1403 //
1404 // Duplicating diamond_true can be very costly especially if it has a
1405 // lot of instructions.
1406 return 0;
1407 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001408
Vincent Lejeune960a6222013-07-19 21:45:06 +00001409 int NumNewBlk = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001410
Vincent Lejeune960a6222013-07-19 21:45:06 +00001411 bool LandBlkHasOtherPred = (LandBlk->pred_size() > 2);
Tom Stellard75aadc22012-12-11 21:25:42 +00001412
1413 //insert AMDGPU::ENDIF to avoid special case "input landBlk == NULL"
Vincent Lejeune960a6222013-07-19 21:45:06 +00001414 MachineBasicBlock::iterator I = insertInstrBefore(LandBlk, AMDGPU::ENDIF);
Tom Stellard75aadc22012-12-11 21:25:42 +00001415
Vincent Lejeune960a6222013-07-19 21:45:06 +00001416 if (LandBlkHasOtherPred) {
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001417 report_fatal_error("Extra register needed to handle CFG");
Vincent Lejeune960a6222013-07-19 21:45:06 +00001418 unsigned CmpResReg =
1419 HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001420 report_fatal_error("Extra compare instruction needed to handle CFG");
Vincent Lejeune960a6222013-07-19 21:45:06 +00001421 insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET,
1422 CmpResReg, DebugLoc());
Tom Stellard75aadc22012-12-11 21:25:42 +00001423 }
1424
Tom Stellard69f86d12013-10-16 17:05:56 +00001425 // XXX: We are running this after RA, so creating virtual registers will
1426 // cause an assertion failure in the PostRA scheduling pass.
1427 unsigned InitReg =
1428 HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001429 insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET, InitReg,
1430 DebugLoc());
Tom Stellard75aadc22012-12-11 21:25:42 +00001431
Vincent Lejeune960a6222013-07-19 21:45:06 +00001432 if (MigrateTrue) {
1433 migrateInstruction(TrueMBB, LandBlk, I);
Tom Stellard75aadc22012-12-11 21:25:42 +00001434 // need to uncondionally insert the assignment to ensure a path from its
1435 // predecessor rather than headBlk has valid value in initReg if
1436 // (initVal != 1).
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001437 report_fatal_error("Extra register needed to handle CFG");
Tom Stellard75aadc22012-12-11 21:25:42 +00001438 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001439 insertInstrBefore(I, AMDGPU::ELSE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001440
Vincent Lejeune960a6222013-07-19 21:45:06 +00001441 if (MigrateFalse) {
1442 migrateInstruction(FalseMBB, LandBlk, I);
Tom Stellard75aadc22012-12-11 21:25:42 +00001443 // need to uncondionally insert the assignment to ensure a path from its
1444 // predecessor rather than headBlk has valid value in initReg if
1445 // (initVal != 0)
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001446 report_fatal_error("Extra register needed to handle CFG");
Tom Stellard75aadc22012-12-11 21:25:42 +00001447 }
1448
Vincent Lejeune960a6222013-07-19 21:45:06 +00001449 if (LandBlkHasOtherPred) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001450 // add endif
Vincent Lejeune960a6222013-07-19 21:45:06 +00001451 insertInstrBefore(I, AMDGPU::ENDIF);
Tom Stellard75aadc22012-12-11 21:25:42 +00001452
1453 // put initReg = 2 to other predecessors of landBlk
Vincent Lejeune960a6222013-07-19 21:45:06 +00001454 for (MachineBasicBlock::pred_iterator PI = LandBlk->pred_begin(),
1455 PE = LandBlk->pred_end(); PI != PE; ++PI) {
1456 MachineBasicBlock *MBB = *PI;
1457 if (MBB != TrueMBB && MBB != FalseMBB)
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001458 report_fatal_error("Extra register needed to handle CFG");
Vincent Lejeune960a6222013-07-19 21:45:06 +00001459 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001460 }
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001461 DEBUG(
1462 dbgs() << "result from improveSimpleJumpintoIf: ";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001463 showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001464 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001465
1466 // update landBlk
Vincent Lejeune960a6222013-07-19 21:45:06 +00001467 *LandMBBPtr = LandBlk;
Tom Stellard75aadc22012-12-11 21:25:42 +00001468
Vincent Lejeune960a6222013-07-19 21:45:06 +00001469 return NumNewBlk;
1470}
Tom Stellard75aadc22012-12-11 21:25:42 +00001471
Vincent Lejeune960a6222013-07-19 21:45:06 +00001472void AMDGPUCFGStructurizer::handleLoopcontBlock(MachineBasicBlock *ContingMBB,
1473 MachineLoop *ContingLoop, MachineBasicBlock *ContMBB,
1474 MachineLoop *ContLoop) {
1475 DEBUG(dbgs() << "loopcontPattern cont = BB" << ContingMBB->getNumber()
1476 << " header = BB" << ContMBB->getNumber() << "\n";
1477 dbgs() << "Trying to continue loop-depth = "
1478 << getLoopDepth(ContLoop)
1479 << " from loop-depth = " << getLoopDepth(ContingLoop) << "\n";);
1480 settleLoopcontBlock(ContingMBB, ContMBB);
1481}
1482
1483void AMDGPUCFGStructurizer::mergeSerialBlock(MachineBasicBlock *DstMBB,
1484 MachineBasicBlock *SrcMBB) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001485 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001486 dbgs() << "serialPattern BB" << DstMBB->getNumber()
1487 << " <= BB" << SrcMBB->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001488 );
Vincent Lejeune960a6222013-07-19 21:45:06 +00001489 DstMBB->splice(DstMBB->end(), SrcMBB, SrcMBB->begin(), SrcMBB->end());
Tom Stellard75aadc22012-12-11 21:25:42 +00001490
Cong Houc1069892015-12-13 09:26:17 +00001491 DstMBB->removeSuccessor(SrcMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001492 cloneSuccessorList(DstMBB, SrcMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001493
Vincent Lejeune960a6222013-07-19 21:45:06 +00001494 removeSuccessor(SrcMBB);
1495 MLI->removeBlock(SrcMBB);
1496 retireBlock(SrcMBB);
1497}
Tom Stellard75aadc22012-12-11 21:25:42 +00001498
Vincent Lejeune960a6222013-07-19 21:45:06 +00001499void AMDGPUCFGStructurizer::mergeIfthenelseBlock(MachineInstr *BranchMI,
1500 MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
1501 MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB) {
Vincent Lejeune8b8a7b52013-07-19 21:45:15 +00001502 assert (TrueMBB);
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001503 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001504 dbgs() << "ifPattern BB" << MBB->getNumber();
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001505 dbgs() << "{ ";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001506 if (TrueMBB) {
1507 dbgs() << "BB" << TrueMBB->getNumber();
Tom Stellard75aadc22012-12-11 21:25:42 +00001508 }
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001509 dbgs() << " } else ";
1510 dbgs() << "{ ";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001511 if (FalseMBB) {
1512 dbgs() << "BB" << FalseMBB->getNumber();
Tom Stellard75aadc22012-12-11 21:25:42 +00001513 }
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001514 dbgs() << " }\n ";
1515 dbgs() << "landBlock: ";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001516 if (!LandMBB) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001517 dbgs() << "NULL";
Tom Stellard75aadc22012-12-11 21:25:42 +00001518 } else {
Vincent Lejeune960a6222013-07-19 21:45:06 +00001519 dbgs() << "BB" << LandMBB->getNumber();
Tom Stellard75aadc22012-12-11 21:25:42 +00001520 }
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001521 dbgs() << "\n";
1522 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001523
Vincent Lejeune960a6222013-07-19 21:45:06 +00001524 int OldOpcode = BranchMI->getOpcode();
1525 DebugLoc BranchDL = BranchMI->getDebugLoc();
Tom Stellard75aadc22012-12-11 21:25:42 +00001526
1527// transform to
1528// if cond
1529// trueBlk
1530// else
1531// falseBlk
1532// endif
1533// landBlk
1534
Vincent Lejeune960a6222013-07-19 21:45:06 +00001535 MachineBasicBlock::iterator I = BranchMI;
1536 insertCondBranchBefore(I, getBranchNzeroOpcode(OldOpcode),
1537 BranchDL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001538
Vincent Lejeune960a6222013-07-19 21:45:06 +00001539 if (TrueMBB) {
1540 MBB->splice(I, TrueMBB, TrueMBB->begin(), TrueMBB->end());
Cong Houc1069892015-12-13 09:26:17 +00001541 MBB->removeSuccessor(TrueMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001542 if (LandMBB && TrueMBB->succ_size()!=0)
Cong Houc1069892015-12-13 09:26:17 +00001543 TrueMBB->removeSuccessor(LandMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001544 retireBlock(TrueMBB);
1545 MLI->removeBlock(TrueMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001546 }
1547
Vincent Lejeune960a6222013-07-19 21:45:06 +00001548 if (FalseMBB) {
1549 insertInstrBefore(I, AMDGPU::ELSE);
1550 MBB->splice(I, FalseMBB, FalseMBB->begin(),
1551 FalseMBB->end());
Cong Houc1069892015-12-13 09:26:17 +00001552 MBB->removeSuccessor(FalseMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001553 if (LandMBB && FalseMBB->succ_size() != 0)
Cong Houc1069892015-12-13 09:26:17 +00001554 FalseMBB->removeSuccessor(LandMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001555 retireBlock(FalseMBB);
1556 MLI->removeBlock(FalseMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001557 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001558 insertInstrBefore(I, AMDGPU::ENDIF);
1559
1560 BranchMI->eraseFromParent();
1561
1562 if (LandMBB && TrueMBB && FalseMBB)
1563 MBB->addSuccessor(LandMBB);
1564
1565}
1566
1567void AMDGPUCFGStructurizer::mergeLooplandBlock(MachineBasicBlock *DstBlk,
1568 MachineBasicBlock *LandMBB) {
1569 DEBUG(dbgs() << "loopPattern header = BB" << DstBlk->getNumber()
1570 << " land = BB" << LandMBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001571
Vincent Lejeune0c5ed2b2013-07-31 19:31:14 +00001572 insertInstrBefore(DstBlk, AMDGPU::WHILELOOP, DebugLoc());
1573 insertInstrEnd(DstBlk, AMDGPU::ENDLOOP, DebugLoc());
Cong Houd97c1002015-12-01 05:29:22 +00001574 DstBlk->replaceSuccessor(DstBlk, LandMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001575}
Tom Stellard75aadc22012-12-11 21:25:42 +00001576
Tom Stellard75aadc22012-12-11 21:25:42 +00001577
Vincent Lejeune960a6222013-07-19 21:45:06 +00001578void 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);
Vincent Lejeune0c5ed2b2013-07-31 19:31:14 +00001589 insertCondBranchBefore(ExitingMBB, I, AMDGPU::IF_PREDICATE_SET, AMDGPU::PREDICATE_BIT, DL);
1590 insertInstrBefore(I, AMDGPU::BREAK);
1591 insertInstrBefore(I, AMDGPU::ENDIF);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001592 //now branchInst can be erase safely
1593 BranchMI->eraseFromParent();
1594 //now take care of successors, retire blocks
Cong Houc1069892015-12-13 09:26:17 +00001595 ExitingMBB->removeSuccessor(LandMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001596}
Tom Stellard75aadc22012-12-11 21:25:42 +00001597
Vincent Lejeune960a6222013-07-19 21:45:06 +00001598void AMDGPUCFGStructurizer::settleLoopcontBlock(MachineBasicBlock *ContingMBB,
1599 MachineBasicBlock *ContMBB) {
1600 DEBUG(dbgs() << "settleLoopcontBlock conting = BB"
1601 << ContingMBB->getNumber()
1602 << ", cont = BB" << ContMBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001603
Vincent Lejeune960a6222013-07-19 21:45:06 +00001604 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();
Tom Stellard75aadc22012-12-11 21:25:42 +00001611
Vincent Lejeune960a6222013-07-19 21:45:06 +00001612 bool UseContinueLogical = ((&*ContingMBB->rbegin()) == MI);
1613
David Blaikie4eaa79c2015-03-23 20:56:44 +00001614 if (!UseContinueLogical) {
Vincent Lejeune960a6222013-07-19 21:45:06 +00001615 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);
Tom Stellard75aadc22012-12-11 21:25:42 +00001627 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001628
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));
Tom Stellard75aadc22012-12-11 21:25:42 +00001638 }
1639}
1640
Vincent Lejeune960a6222013-07-19 21:45:06 +00001641int 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;
Tom Stellard75aadc22012-12-11 21:25:42 +00001650 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001651
Vincent Lejeune960a6222013-07-19 21:45:06 +00001652 PreMBB = SrcMBB;
1653 SrcMBB = *SrcMBB->succ_begin();
Tom Stellard75aadc22012-12-11 21:25:42 +00001654 }
1655
Vincent Lejeune960a6222013-07-19 21:45:06 +00001656 return Cloned;
1657}
Tom Stellard75aadc22012-12-11 21:25:42 +00001658
Vincent Lejeune960a6222013-07-19 21:45:06 +00001659MachineBasicBlock *
1660AMDGPUCFGStructurizer::cloneBlockForPredecessor(MachineBasicBlock *MBB,
1661 MachineBasicBlock *PredMBB) {
1662 assert(PredMBB->isSuccessor(MBB) &&
Tom Stellard75aadc22012-12-11 21:25:42 +00001663 "succBlk is not a prececessor of curBlk");
1664
Vincent Lejeune960a6222013-07-19 21:45:06 +00001665 MachineBasicBlock *CloneMBB = clone(MBB); //clone instructions
1666 replaceInstrUseOfBlockWith(PredMBB, MBB, CloneMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001667 //srcBlk, oldBlk, newBlk
1668
Cong Houd97c1002015-12-01 05:29:22 +00001669 PredMBB->replaceSuccessor(MBB, CloneMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001670
1671 // add all successor to cloneBlk
Vincent Lejeune960a6222013-07-19 21:45:06 +00001672 cloneSuccessorList(CloneMBB, MBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001673
Vincent Lejeune960a6222013-07-19 21:45:06 +00001674 numClonedInstr += MBB->size();
Tom Stellard75aadc22012-12-11 21:25:42 +00001675
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001676 DEBUG(
1677 dbgs() << "Cloned block: " << "BB"
Vincent Lejeune960a6222013-07-19 21:45:06 +00001678 << MBB->getNumber() << "size " << MBB->size() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001679 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001680
Vincent Lejeune960a6222013-07-19 21:45:06 +00001681 SHOWNEWBLK(CloneMBB, "result of Cloned block: ");
Tom Stellard75aadc22012-12-11 21:25:42 +00001682
Vincent Lejeune960a6222013-07-19 21:45:06 +00001683 return CloneMBB;
1684}
Tom Stellard75aadc22012-12-11 21:25:42 +00001685
Vincent Lejeune960a6222013-07-19 21:45:06 +00001686void AMDGPUCFGStructurizer::migrateInstruction(MachineBasicBlock *SrcMBB,
1687 MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I) {
1688 MachineBasicBlock::iterator SpliceEnd;
Tom Stellard75aadc22012-12-11 21:25:42 +00001689 //look for the input branchinstr, not the AMDGPU branchinstr
Vincent Lejeune960a6222013-07-19 21:45:06 +00001690 MachineInstr *BranchMI = getNormalBlockBranchInstr(SrcMBB);
1691 if (!BranchMI) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001692 DEBUG(
1693 dbgs() << "migrateInstruction don't see branch instr\n" ;
1694 );
Vincent Lejeune960a6222013-07-19 21:45:06 +00001695 SpliceEnd = SrcMBB->end();
Tom Stellard75aadc22012-12-11 21:25:42 +00001696 } else {
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001697 DEBUG(dbgs() << "migrateInstruction see branch instr: " << *BranchMI);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001698 SpliceEnd = BranchMI;
Tom Stellard75aadc22012-12-11 21:25:42 +00001699 }
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001700 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001701 dbgs() << "migrateInstruction before splice dstSize = " << DstMBB->size()
1702 << "srcSize = " << SrcMBB->size() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001703 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001704
1705 //splice insert before insertPos
Vincent Lejeune960a6222013-07-19 21:45:06 +00001706 DstMBB->splice(I, SrcMBB, SrcMBB->begin(), SpliceEnd);
Tom Stellard75aadc22012-12-11 21:25:42 +00001707
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001708 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001709 dbgs() << "migrateInstruction after splice dstSize = " << DstMBB->size()
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001710 << "srcSize = " << SrcMBB->size() << '\n';
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001711 );
Vincent Lejeune960a6222013-07-19 21:45:06 +00001712}
Tom Stellard75aadc22012-12-11 21:25:42 +00001713
Vincent Lejeune960a6222013-07-19 21:45:06 +00001714MachineBasicBlock *
1715AMDGPUCFGStructurizer::normalizeInfiniteLoopExit(MachineLoop* LoopRep) {
1716 MachineBasicBlock *LoopHeader = LoopRep->getHeader();
1717 MachineBasicBlock *LoopLatch = LoopRep->getLoopLatch();
Tom Stellard75aadc22012-12-11 21:25:42 +00001718
Vincent Lejeune960a6222013-07-19 21:45:06 +00001719 if (!LoopHeader || !LoopLatch)
Craig Topper062a2ba2014-04-25 05:30:21 +00001720 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001721 MachineInstr *BranchMI = getLoopendBlockBranchInstr(LoopLatch);
1722 // Is LoopRep an infinite loop ?
1723 if (!BranchMI || !isUncondBranch(BranchMI))
Craig Topper062a2ba2014-04-25 05:30:21 +00001724 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001725
Vincent Lejeune960a6222013-07-19 21:45:06 +00001726 MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
1727 FuncRep->push_back(DummyExitBlk); //insert to function
1728 SHOWNEWBLK(DummyExitBlk, "DummyExitBlock to normalize infiniteLoop: ");
1729 DEBUG(dbgs() << "Old branch instr: " << *BranchMI << "\n";);
Tom Stellard1d46fb22015-07-16 15:38:29 +00001730 LLVMContext &Ctx = LoopHeader->getParent()->getFunction()->getContext();
1731 Ctx.emitError("Extra register needed to handle CFG");
1732 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001733}
Tom Stellard75aadc22012-12-11 21:25:42 +00001734
Vincent Lejeune960a6222013-07-19 21:45:06 +00001735void AMDGPUCFGStructurizer::removeUnconditionalBranch(MachineBasicBlock *MBB) {
1736 MachineInstr *BranchMI;
Tom Stellard75aadc22012-12-11 21:25:42 +00001737
1738 // I saw two unconditional branch in one basic block in example
1739 // test_fc_do_while_or.c need to fix the upstream on this to remove the loop.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001740 while ((BranchMI = getLoopendBlockBranchInstr(MBB))
1741 && isUncondBranch(BranchMI)) {
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001742 DEBUG(dbgs() << "Removing uncond branch instr: " << *BranchMI);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001743 BranchMI->eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +00001744 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001745}
Tom Stellard75aadc22012-12-11 21:25:42 +00001746
Vincent Lejeune960a6222013-07-19 21:45:06 +00001747void AMDGPUCFGStructurizer::removeRedundantConditionalBranch(
1748 MachineBasicBlock *MBB) {
1749 if (MBB->succ_size() != 2)
1750 return;
1751 MachineBasicBlock *MBB1 = *MBB->succ_begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001752 MachineBasicBlock *MBB2 = *std::next(MBB->succ_begin());
Vincent Lejeune960a6222013-07-19 21:45:06 +00001753 if (MBB1 != MBB2)
1754 return;
Tom Stellard75aadc22012-12-11 21:25:42 +00001755
Vincent Lejeune960a6222013-07-19 21:45:06 +00001756 MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
1757 assert(BranchMI && isCondBranch(BranchMI));
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001758 DEBUG(dbgs() << "Removing unneeded cond branch instr: " << *BranchMI);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001759 BranchMI->eraseFromParent();
1760 SHOWNEWBLK(MBB1, "Removing redundant successor");
Cong Houc1069892015-12-13 09:26:17 +00001761 MBB->removeSuccessor(MBB1, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001762}
Tom Stellard75aadc22012-12-11 21:25:42 +00001763
Vincent Lejeune960a6222013-07-19 21:45:06 +00001764void AMDGPUCFGStructurizer::addDummyExitBlock(
1765 SmallVectorImpl<MachineBasicBlock*> &RetMBB) {
1766 MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
1767 FuncRep->push_back(DummyExitBlk); //insert to function
1768 insertInstrEnd(DummyExitBlk, AMDGPU::RETURN);
Tom Stellard75aadc22012-12-11 21:25:42 +00001769
Vincent Lejeune960a6222013-07-19 21:45:06 +00001770 for (SmallVectorImpl<MachineBasicBlock *>::iterator It = RetMBB.begin(),
1771 E = RetMBB.end(); It != E; ++It) {
1772 MachineBasicBlock *MBB = *It;
1773 MachineInstr *MI = getReturnInstr(MBB);
1774 if (MI)
1775 MI->eraseFromParent();
1776 MBB->addSuccessor(DummyExitBlk);
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001777 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001778 dbgs() << "Add dummyExitBlock to BB" << MBB->getNumber()
Tom Stellard75aadc22012-12-11 21:25:42 +00001779 << " successors\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001780 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001781 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001782 SHOWNEWBLK(DummyExitBlk, "DummyExitBlock: ");
Tom Stellard75aadc22012-12-11 21:25:42 +00001783}
1784
Vincent Lejeune960a6222013-07-19 21:45:06 +00001785void AMDGPUCFGStructurizer::removeSuccessor(MachineBasicBlock *MBB) {
1786 while (MBB->succ_size())
1787 MBB->removeSuccessor(*MBB->succ_begin());
Tom Stellard75aadc22012-12-11 21:25:42 +00001788}
1789
Vincent Lejeune960a6222013-07-19 21:45:06 +00001790void AMDGPUCFGStructurizer::recordSccnum(MachineBasicBlock *MBB,
1791 int SccNum) {
1792 BlockInformation *&srcBlkInfo = BlockInfoMap[MBB];
1793 if (!srcBlkInfo)
1794 srcBlkInfo = new BlockInformation();
1795 srcBlkInfo->SccNum = SccNum;
Tom Stellard75aadc22012-12-11 21:25:42 +00001796}
1797
Vincent Lejeune960a6222013-07-19 21:45:06 +00001798void AMDGPUCFGStructurizer::retireBlock(MachineBasicBlock *MBB) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001799 DEBUG(
Vincent Lejeune960a6222013-07-19 21:45:06 +00001800 dbgs() << "Retiring BB" << MBB->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001801 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001802
Vincent Lejeune960a6222013-07-19 21:45:06 +00001803 BlockInformation *&SrcBlkInfo = BlockInfoMap[MBB];
Tom Stellard75aadc22012-12-11 21:25:42 +00001804
Vincent Lejeune960a6222013-07-19 21:45:06 +00001805 if (!SrcBlkInfo)
1806 SrcBlkInfo = new BlockInformation();
Tom Stellard75aadc22012-12-11 21:25:42 +00001807
Vincent Lejeune960a6222013-07-19 21:45:06 +00001808 SrcBlkInfo->IsRetired = true;
1809 assert(MBB->succ_size() == 0 && MBB->pred_size() == 0
Tom Stellard75aadc22012-12-11 21:25:42 +00001810 && "can't retire block yet");
1811}
1812
Vincent Lejeune960a6222013-07-19 21:45:06 +00001813void AMDGPUCFGStructurizer::setLoopLandBlock(MachineLoop *loopRep,
1814 MachineBasicBlock *MBB) {
1815 MachineBasicBlock *&TheEntry = LLInfoMap[loopRep];
1816 if (!MBB) {
1817 MBB = FuncRep->CreateMachineBasicBlock();
1818 FuncRep->push_back(MBB); //insert to function
1819 SHOWNEWBLK(MBB, "DummyLandingBlock for loop without break: ");
Tom Stellard75aadc22012-12-11 21:25:42 +00001820 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001821 TheEntry = MBB;
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001822 DEBUG(
1823 dbgs() << "setLoopLandBlock loop-header = BB"
Tom Stellard75aadc22012-12-11 21:25:42 +00001824 << loopRep->getHeader()->getNumber()
Vincent Lejeune960a6222013-07-19 21:45:06 +00001825 << " landing-block = BB" << MBB->getNumber() << "\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001826 );
Vincent Lejeune960a6222013-07-19 21:45:06 +00001827}
Tom Stellard75aadc22012-12-11 21:25:42 +00001828
Vincent Lejeune960a6222013-07-19 21:45:06 +00001829MachineBasicBlock *
1830AMDGPUCFGStructurizer::findNearestCommonPostDom(MachineBasicBlock *MBB1,
1831 MachineBasicBlock *MBB2) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001832
Vincent Lejeune960a6222013-07-19 21:45:06 +00001833 if (PDT->dominates(MBB1, MBB2))
1834 return MBB1;
1835 if (PDT->dominates(MBB2, MBB1))
1836 return MBB2;
Tom Stellard75aadc22012-12-11 21:25:42 +00001837
Vincent Lejeune960a6222013-07-19 21:45:06 +00001838 MachineDomTreeNode *Node1 = PDT->getNode(MBB1);
1839 MachineDomTreeNode *Node2 = PDT->getNode(MBB2);
Tom Stellard75aadc22012-12-11 21:25:42 +00001840
1841 // Handle newly cloned node.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001842 if (!Node1 && MBB1->succ_size() == 1)
1843 return findNearestCommonPostDom(*MBB1->succ_begin(), MBB2);
1844 if (!Node2 && MBB2->succ_size() == 1)
1845 return findNearestCommonPostDom(MBB1, *MBB2->succ_begin());
Tom Stellard75aadc22012-12-11 21:25:42 +00001846
Vincent Lejeune960a6222013-07-19 21:45:06 +00001847 if (!Node1 || !Node2)
Craig Topper062a2ba2014-04-25 05:30:21 +00001848 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001849
Vincent Lejeune960a6222013-07-19 21:45:06 +00001850 Node1 = Node1->getIDom();
1851 while (Node1) {
1852 if (PDT->dominates(Node1, Node2))
1853 return Node1->getBlock();
1854 Node1 = Node1->getIDom();
Tom Stellard75aadc22012-12-11 21:25:42 +00001855 }
1856
Craig Topper062a2ba2014-04-25 05:30:21 +00001857 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001858}
1859
Vincent Lejeune960a6222013-07-19 21:45:06 +00001860MachineBasicBlock *
1861AMDGPUCFGStructurizer::findNearestCommonPostDom(
1862 std::set<MachineBasicBlock *> &MBBs) {
1863 MachineBasicBlock *CommonDom;
1864 std::set<MachineBasicBlock *>::const_iterator It = MBBs.begin();
1865 std::set<MachineBasicBlock *>::const_iterator E = MBBs.end();
1866 for (CommonDom = *It; It != E && CommonDom; ++It) {
1867 MachineBasicBlock *MBB = *It;
1868 if (MBB != CommonDom)
1869 CommonDom = findNearestCommonPostDom(MBB, CommonDom);
Tom Stellard75aadc22012-12-11 21:25:42 +00001870 }
1871
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001872 DEBUG(
1873 dbgs() << "Common post dominator for exit blocks is ";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001874 if (CommonDom)
1875 dbgs() << "BB" << CommonDom->getNumber() << "\n";
1876 else
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001877 dbgs() << "NULL\n";
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001878 );
Tom Stellard75aadc22012-12-11 21:25:42 +00001879
Vincent Lejeune960a6222013-07-19 21:45:06 +00001880 return CommonDom;
1881}
1882
1883char AMDGPUCFGStructurizer::ID = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001884
Benjamin Kramer635e3682013-05-23 15:43:05 +00001885} // end anonymous namespace
Tom Stellard75aadc22012-12-11 21:25:42 +00001886
Tom Stellard75aadc22012-12-11 21:25:42 +00001887
Tom Stellardf2ba9722013-12-11 17:51:47 +00001888INITIALIZE_PASS_BEGIN(AMDGPUCFGStructurizer, "amdgpustructurizer",
1889 "AMDGPU CFG Structurizer", false, false)
1890INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
1891INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
1892INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
1893INITIALIZE_PASS_END(AMDGPUCFGStructurizer, "amdgpustructurizer",
1894 "AMDGPU CFG Structurizer", false, false)
1895
1896FunctionPass *llvm::createAMDGPUCFGStructurizerPass() {
1897 return new AMDGPUCFGStructurizer();
Tom Stellard75aadc22012-12-11 21:25:42 +00001898}