blob: 12f2e9519c9ecca0470c0ba2e03b9cee6d4af1af [file] [log] [blame]
Eugene Zelenkod16eff82017-08-08 23:53:55 +00001//===- AMDILCFGStructurizer.cpp - CFG Structurizer ------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellard75aadc22012-12-11 21:25:42 +00006//
Tom Stellard75aadc22012-12-11 21:25:42 +00007//==-----------------------------------------------------------------------===//
8
Tom Stellarda6c6e1b2013-06-07 20:37:48 +00009#include "AMDGPU.h"
Eric Christopherd9134482014-08-04 21:25:23 +000010#include "AMDGPUSubtarget.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000011#include "R600InstrInfo.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000012#include "R600RegisterInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000013#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000014#include "llvm/ADT/DepthFirstIterator.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000015#include "llvm/ADT/SCCIterator.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000016#include "llvm/ADT/SmallPtrSet.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000019#include "llvm/ADT/StringRef.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/CodeGen/MachineDominators.h"
22#include "llvm/CodeGen/MachineFunction.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000024#include "llvm/CodeGen/MachineInstr.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko06f90ef2017-01-21 01:34:25 +000026#include "llvm/CodeGen/MachineJumpTableInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000028#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000029#include "llvm/CodeGen/MachinePostDominators.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000031#include "llvm/IR/DebugLoc.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/Pass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/Support/Debug.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000035#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000036#include "llvm/Support/MachineValueType.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000037#include "llvm/Support/raw_ostream.h"
Eugene Zelenko66203762017-01-21 00:53:49 +000038#include <cassert>
39#include <cstddef>
Benjamin Kramer799003b2015-03-23 19:32:43 +000040#include <deque>
Eugene Zelenko66203762017-01-21 00:53:49 +000041#include <iterator>
42#include <map>
43#include <utility>
44#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000045
46using namespace llvm;
47
Chandler Carruth84e68b22014-04-22 02:41:26 +000048#define DEBUG_TYPE "structcfg"
49
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000050#define DEFAULT_VEC_SLOTS 8
51
Tom Stellard75aadc22012-12-11 21:25:42 +000052// TODO: move-begin.
53
54//===----------------------------------------------------------------------===//
55//
56// Statistics for CFGStructurizer.
57//
58//===----------------------------------------------------------------------===//
59
60STATISTIC(numSerialPatternMatch, "CFGStructurizer number of serial pattern "
61 "matched");
62STATISTIC(numIfPatternMatch, "CFGStructurizer number of if pattern "
63 "matched");
Tom Stellard75aadc22012-12-11 21:25:42 +000064STATISTIC(numClonedBlock, "CFGStructurizer cloned blocks");
65STATISTIC(numClonedInstr, "CFGStructurizer cloned instructions");
66
Tom Stellardf2ba9722013-12-11 17:51:47 +000067namespace llvm {
Eugene Zelenko66203762017-01-21 00:53:49 +000068
Eugene Zelenkod16eff82017-08-08 23:53:55 +000069void initializeAMDGPUCFGStructurizerPass(PassRegistry &);
Eugene Zelenko66203762017-01-21 00:53:49 +000070
71} // end namespace llvm
72
73namespace {
Tom Stellardf2ba9722013-12-11 17:51:47 +000074
Tom Stellard75aadc22012-12-11 21:25:42 +000075//===----------------------------------------------------------------------===//
76//
77// Miscellaneous utility for CFGStructurizer.
78//
79//===----------------------------------------------------------------------===//
Eugene Zelenko66203762017-01-21 00:53:49 +000080
Nicola Zaghend34e60c2018-05-14 12:53:11 +000081#define SHOWNEWINSTR(i) LLVM_DEBUG(dbgs() << "New instr: " << *i << "\n");
Tom Stellard75aadc22012-12-11 21:25:42 +000082
Nicola Zaghend34e60c2018-05-14 12:53:11 +000083#define SHOWNEWBLK(b, msg) \
84 LLVM_DEBUG(dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
85 dbgs() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +000086
Nicola Zaghend34e60c2018-05-14 12:53:11 +000087#define SHOWBLK_DETAIL(b, msg) \
88 LLVM_DEBUG(if (b) { \
89 dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
90 b->print(dbgs()); \
91 dbgs() << "\n"; \
92 });
Tom Stellard75aadc22012-12-11 21:25:42 +000093
94#define INVALIDSCCNUM -1
Tom Stellard75aadc22012-12-11 21:25:42 +000095
Tom Stellard75aadc22012-12-11 21:25:42 +000096//===----------------------------------------------------------------------===//
97//
98// supporting data structure for CFGStructurizer
99//
100//===----------------------------------------------------------------------===//
101
Tom Stellard75aadc22012-12-11 21:25:42 +0000102class BlockInformation {
103public:
Eugene Zelenko66203762017-01-21 00:53:49 +0000104 bool IsRetired = false;
105 int SccNum = INVALIDSCCNUM;
Tom Stellard75aadc22012-12-11 21:25:42 +0000106
Eugene Zelenko66203762017-01-21 00:53:49 +0000107 BlockInformation() = default;
108};
Tom Stellard75aadc22012-12-11 21:25:42 +0000109
110//===----------------------------------------------------------------------===//
111//
112// CFGStructurizer
113//
114//===----------------------------------------------------------------------===//
115
Vincent Lejeune960a6222013-07-19 21:45:06 +0000116class AMDGPUCFGStructurizer : public MachineFunctionPass {
Tom Stellard75aadc22012-12-11 21:25:42 +0000117public:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000118 using MBBVector = SmallVector<MachineBasicBlock *, 32>;
119 using MBBInfoMap = std::map<MachineBasicBlock *, BlockInformation *>;
120 using LoopLandInfoMap = std::map<MachineLoop *, MachineBasicBlock *>;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000121
122 enum PathToKind {
Tom Stellard75aadc22012-12-11 21:25:42 +0000123 Not_SinglePath = 0,
124 SinglePath_InPath = 1,
125 SinglePath_NotInPath = 2
Vincent Lejeune960a6222013-07-19 21:45:06 +0000126 };
Tom Stellard75aadc22012-12-11 21:25:42 +0000127
Vincent Lejeune960a6222013-07-19 21:45:06 +0000128 static char ID;
Tom Stellard75aadc22012-12-11 21:25:42 +0000129
Eugene Zelenko66203762017-01-21 00:53:49 +0000130 AMDGPUCFGStructurizer() : MachineFunctionPass(ID) {
Tom Stellardf2ba9722013-12-11 17:51:47 +0000131 initializeAMDGPUCFGStructurizerPass(*PassRegistry::getPassRegistry());
132 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000133
Mehdi Amini117296c2016-10-01 02:56:57 +0000134 StringRef getPassName() const override {
Tom Stellardf2ba9722013-12-11 17:51:47 +0000135 return "AMDGPU Control Flow Graph structurizer Pass";
Vincent Lejeune960a6222013-07-19 21:45:06 +0000136 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000137
Craig Topper5656db42014-04-29 07:57:24 +0000138 void getAnalysisUsage(AnalysisUsage &AU) const override {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000139 AU.addRequired<MachineDominatorTree>();
140 AU.addRequired<MachinePostDominatorTree>();
141 AU.addRequired<MachineLoopInfo>();
Matthias Braun733fe362016-08-24 01:52:46 +0000142 MachineFunctionPass::getAnalysisUsage(AU);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000143 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000144
145 /// Perform the CFG structurization
Vincent Lejeune960a6222013-07-19 21:45:06 +0000146 bool run();
Tom Stellard75aadc22012-12-11 21:25:42 +0000147
148 /// Perform the CFG preparation
Vincent Lejeune960a6222013-07-19 21:45:06 +0000149 /// This step will remove every unconditionnal/dead jump instructions and make
150 /// sure all loops have an exit block
151 bool prepare();
Tom Stellard75aadc22012-12-11 21:25:42 +0000152
Craig Topper5656db42014-04-29 07:57:24 +0000153 bool runOnMachineFunction(MachineFunction &MF) override {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000154 TII = MF.getSubtarget<R600Subtarget>().getInstrInfo();
Tom Stellardf2ba9722013-12-11 17:51:47 +0000155 TRI = &TII->getRegisterInfo();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000156 LLVM_DEBUG(MF.dump(););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000157 OrderedBlks.clear();
Jan Vesely7a9cca92015-03-13 17:32:46 +0000158 Visited.clear();
Vincent Lejeune960a6222013-07-19 21:45:06 +0000159 FuncRep = &MF;
160 MLI = &getAnalysis<MachineLoopInfo>();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000161 LLVM_DEBUG(dbgs() << "LoopInfo:\n"; PrintLoopinfo(*MLI););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000162 MDT = &getAnalysis<MachineDominatorTree>();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000163 LLVM_DEBUG(MDT->print(dbgs(), (const Module *)nullptr););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000164 PDT = &getAnalysis<MachinePostDominatorTree>();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000165 LLVM_DEBUG(PDT->print(dbgs()););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000166 prepare();
167 run();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000168 LLVM_DEBUG(MF.dump(););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000169 return true;
170 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000171
Vincent Lejeune960a6222013-07-19 21:45:06 +0000172protected:
Vincent Lejeune960a6222013-07-19 21:45:06 +0000173 MachineDominatorTree *MDT;
174 MachinePostDominatorTree *PDT;
175 MachineLoopInfo *MLI;
Eugene Zelenko66203762017-01-21 00:53:49 +0000176 const R600InstrInfo *TII = nullptr;
177 const R600RegisterInfo *TRI = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +0000178
Vincent Lejeune960a6222013-07-19 21:45:06 +0000179 // PRINT FUNCTIONS
180 /// Print the ordered Blocks.
181 void printOrderedBlocks() const {
182 size_t i = 0;
183 for (MBBVector::const_iterator iterBlk = OrderedBlks.begin(),
184 iterBlkEnd = OrderedBlks.end(); iterBlk != iterBlkEnd; ++iterBlk, ++i) {
185 dbgs() << "BB" << (*iterBlk)->getNumber();
186 dbgs() << "(" << getSCCNum(*iterBlk) << "," << (*iterBlk)->size() << ")";
187 if (i != 0 && i % 10 == 0) {
188 dbgs() << "\n";
189 } else {
190 dbgs() << " ";
191 }
192 }
193 }
Eugene Zelenko66203762017-01-21 00:53:49 +0000194
Vincent Lejeune960a6222013-07-19 21:45:06 +0000195 static void PrintLoopinfo(const MachineLoopInfo &LoopInfo) {
196 for (MachineLoop::iterator iter = LoopInfo.begin(),
197 iterEnd = LoopInfo.end(); iter != iterEnd; ++iter) {
198 (*iter)->print(dbgs(), 0);
199 }
200 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000201
Vincent Lejeune960a6222013-07-19 21:45:06 +0000202 // UTILITY FUNCTIONS
203 int getSCCNum(MachineBasicBlock *MBB) const;
204 MachineBasicBlock *getLoopLandInfo(MachineLoop *LoopRep) const;
205 bool hasBackEdge(MachineBasicBlock *MBB) const;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000206 bool isRetiredBlock(MachineBasicBlock *MBB) const;
207 bool isActiveLoophead(MachineBasicBlock *MBB) const;
208 PathToKind singlePathTo(MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
209 bool AllowSideEntry = true) const;
210 int countActiveBlock(MBBVector::const_iterator It,
211 MBBVector::const_iterator E) const;
212 bool needMigrateBlock(MachineBasicBlock *MBB) const;
213
214 // Utility Functions
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000215 void reversePredicateSetter(MachineBasicBlock::iterator I,
216 MachineBasicBlock &MBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000217 /// Compute the reversed DFS post order of Blocks
218 void orderBlocks(MachineFunction *MF);
219
Alp Tokercb402912014-01-24 17:20:08 +0000220 // Function originally from CFGStructTraits
Vincent Lejeune960a6222013-07-19 21:45:06 +0000221 void insertInstrEnd(MachineBasicBlock *MBB, int NewOpcode,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000222 const DebugLoc &DL = DebugLoc());
Vincent Lejeune960a6222013-07-19 21:45:06 +0000223 MachineInstr *insertInstrBefore(MachineBasicBlock *MBB, int NewOpcode,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000224 const DebugLoc &DL = DebugLoc());
Vincent Lejeune960a6222013-07-19 21:45:06 +0000225 MachineInstr *insertInstrBefore(MachineBasicBlock::iterator I, int NewOpcode);
226 void insertCondBranchBefore(MachineBasicBlock::iterator I, int NewOpcode,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000227 const DebugLoc &DL);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000228 void insertCondBranchBefore(MachineBasicBlock *MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000229 MachineBasicBlock::iterator I, int NewOpcode,
230 int RegNum, const DebugLoc &DL);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000231
Vincent Lejeune960a6222013-07-19 21:45:06 +0000232 static int getBranchNzeroOpcode(int OldOpcode);
233 static int getBranchZeroOpcode(int OldOpcode);
234 static int getContinueNzeroOpcode(int OldOpcode);
235 static int getContinueZeroOpcode(int OldOpcode);
236 static MachineBasicBlock *getTrueBranch(MachineInstr *MI);
237 static void setTrueBranch(MachineInstr *MI, MachineBasicBlock *MBB);
238 static MachineBasicBlock *getFalseBranch(MachineBasicBlock *MBB,
239 MachineInstr *MI);
240 static bool isCondBranch(MachineInstr *MI);
241 static bool isUncondBranch(MachineInstr *MI);
242 static DebugLoc getLastDebugLocInBB(MachineBasicBlock *MBB);
243 static MachineInstr *getNormalBlockBranchInstr(MachineBasicBlock *MBB);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000244
Vincent Lejeune960a6222013-07-19 21:45:06 +0000245 /// The correct naming for this is getPossibleLoopendBlockBranchInstr.
246 ///
247 /// BB with backward-edge could have move instructions after the branch
248 /// instruction. Such move instruction "belong to" the loop backward-edge.
249 MachineInstr *getLoopendBlockBranchInstr(MachineBasicBlock *MBB);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000250
Vincent Lejeune960a6222013-07-19 21:45:06 +0000251 static MachineInstr *getReturnInstr(MachineBasicBlock *MBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000252 static bool isReturnBlock(MachineBasicBlock *MBB);
253 static void cloneSuccessorList(MachineBasicBlock *DstMBB,
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000254 MachineBasicBlock *SrcMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000255 static MachineBasicBlock *clone(MachineBasicBlock *MBB);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000256
Vincent Lejeune960a6222013-07-19 21:45:06 +0000257 /// MachineBasicBlock::ReplaceUsesOfBlockWith doesn't serve the purpose
258 /// because the AMDGPU instruction is not recognized as terminator fix this
259 /// and retire this routine
260 void replaceInstrUseOfBlockWith(MachineBasicBlock *SrcMBB,
261 MachineBasicBlock *OldMBB, MachineBasicBlock *NewBlk);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000262
Vincent Lejeune960a6222013-07-19 21:45:06 +0000263 static void wrapup(MachineBasicBlock *MBB);
264
Vincent Lejeune960a6222013-07-19 21:45:06 +0000265 int patternMatch(MachineBasicBlock *MBB);
266 int patternMatchGroup(MachineBasicBlock *MBB);
267 int serialPatternMatch(MachineBasicBlock *MBB);
268 int ifPatternMatch(MachineBasicBlock *MBB);
269 int loopendPatternMatch();
270 int mergeLoop(MachineLoop *LoopRep);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000271
Vincent Lejeune960a6222013-07-19 21:45:06 +0000272 /// return true iff src1Blk->succ_size() == 0 && src1Blk and src2Blk are in
273 /// the same loop with LoopLandInfo without explicitly keeping track of
274 /// loopContBlks and loopBreakBlks, this is a method to get the information.
275 bool isSameloopDetachedContbreak(MachineBasicBlock *Src1MBB,
276 MachineBasicBlock *Src2MBB);
277 int handleJumpintoIf(MachineBasicBlock *HeadMBB,
278 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
279 int handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
280 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
281 int improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
282 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
283 MachineBasicBlock **LandMBBPtr);
284 void showImproveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
285 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
286 MachineBasicBlock *LandMBB, bool Detail = false);
287 int cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
288 MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB);
289 void mergeSerialBlock(MachineBasicBlock *DstMBB,
290 MachineBasicBlock *SrcMBB);
291
292 void mergeIfthenelseBlock(MachineInstr *BranchMI,
293 MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
294 MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB);
295 void mergeLooplandBlock(MachineBasicBlock *DstMBB,
296 MachineBasicBlock *LandMBB);
297 void mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
298 MachineBasicBlock *LandMBB);
299 void settleLoopcontBlock(MachineBasicBlock *ContingMBB,
300 MachineBasicBlock *ContMBB);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000301
Vincent Lejeune960a6222013-07-19 21:45:06 +0000302 /// normalizeInfiniteLoopExit change
303 /// B1:
304 /// uncond_br LoopHeader
305 ///
306 /// to
307 /// B1:
308 /// cond_br 1 LoopHeader dummyExit
309 /// and return the newly added dummy exit block
310 MachineBasicBlock *normalizeInfiniteLoopExit(MachineLoop *LoopRep);
311 void removeUnconditionalBranch(MachineBasicBlock *MBB);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000312
Vincent Lejeune960a6222013-07-19 21:45:06 +0000313 /// Remove duplicate branches instructions in a block.
314 /// For instance
315 /// B0:
316 /// cond_br X B1 B2
317 /// cond_br X B1 B2
318 /// is transformed to
319 /// B0:
320 /// cond_br X B1 B2
321 void removeRedundantConditionalBranch(MachineBasicBlock *MBB);
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000322
Vincent Lejeune960a6222013-07-19 21:45:06 +0000323 void addDummyExitBlock(SmallVectorImpl<MachineBasicBlock *> &RetMBB);
324 void removeSuccessor(MachineBasicBlock *MBB);
325 MachineBasicBlock *cloneBlockForPredecessor(MachineBasicBlock *MBB,
326 MachineBasicBlock *PredMBB);
327 void migrateInstruction(MachineBasicBlock *SrcMBB,
328 MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I);
329 void recordSccnum(MachineBasicBlock *MBB, int SCCNum);
330 void retireBlock(MachineBasicBlock *MBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000331
Vincent Lejeune960a6222013-07-19 21:45:06 +0000332private:
333 MBBInfoMap BlockInfoMap;
334 LoopLandInfoMap LLInfoMap;
335 std::map<MachineLoop *, bool> Visited;
336 MachineFunction *FuncRep;
337 SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> OrderedBlks;
338};
339
Eugene Zelenko66203762017-01-21 00:53:49 +0000340} // end anonymous namespace
341
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000342char AMDGPUCFGStructurizer::ID = 0;
343
Vincent Lejeune960a6222013-07-19 21:45:06 +0000344int AMDGPUCFGStructurizer::getSCCNum(MachineBasicBlock *MBB) const {
345 MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
346 if (It == BlockInfoMap.end())
347 return INVALIDSCCNUM;
348 return (*It).second->SccNum;
Tom Stellard75aadc22012-12-11 21:25:42 +0000349}
350
Vincent Lejeune960a6222013-07-19 21:45:06 +0000351MachineBasicBlock *AMDGPUCFGStructurizer::getLoopLandInfo(MachineLoop *LoopRep)
352 const {
353 LoopLandInfoMap::const_iterator It = LLInfoMap.find(LoopRep);
354 if (It == LLInfoMap.end())
Craig Topper062a2ba2014-04-25 05:30:21 +0000355 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000356 return (*It).second;
357}
358
359bool AMDGPUCFGStructurizer::hasBackEdge(MachineBasicBlock *MBB) const {
360 MachineLoop *LoopRep = MLI->getLoopFor(MBB);
361 if (!LoopRep)
362 return false;
363 MachineBasicBlock *LoopHeader = LoopRep->getHeader();
364 return MBB->isSuccessor(LoopHeader);
365}
366
Vincent Lejeune960a6222013-07-19 21:45:06 +0000367bool AMDGPUCFGStructurizer::isRetiredBlock(MachineBasicBlock *MBB) const {
368 MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
369 if (It == BlockInfoMap.end())
370 return false;
371 return (*It).second->IsRetired;
372}
373
374bool AMDGPUCFGStructurizer::isActiveLoophead(MachineBasicBlock *MBB) const {
375 MachineLoop *LoopRep = MLI->getLoopFor(MBB);
376 while (LoopRep && LoopRep->getHeader() == MBB) {
377 MachineBasicBlock *LoopLand = getLoopLandInfo(LoopRep);
378 if(!LoopLand)
379 return true;
380 if (!isRetiredBlock(LoopLand))
381 return true;
382 LoopRep = LoopRep->getParentLoop();
383 }
384 return false;
385}
Eugene Zelenko66203762017-01-21 00:53:49 +0000386
Vincent Lejeune960a6222013-07-19 21:45:06 +0000387AMDGPUCFGStructurizer::PathToKind AMDGPUCFGStructurizer::singlePathTo(
388 MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
389 bool AllowSideEntry) const {
390 assert(DstMBB);
391 if (SrcMBB == DstMBB)
392 return SinglePath_InPath;
393 while (SrcMBB && SrcMBB->succ_size() == 1) {
394 SrcMBB = *SrcMBB->succ_begin();
395 if (SrcMBB == DstMBB)
396 return SinglePath_InPath;
397 if (!AllowSideEntry && SrcMBB->pred_size() > 1)
398 return Not_SinglePath;
399 }
400 if (SrcMBB && SrcMBB->succ_size()==0)
401 return SinglePath_NotInPath;
402 return Not_SinglePath;
403}
404
405int AMDGPUCFGStructurizer::countActiveBlock(MBBVector::const_iterator It,
406 MBBVector::const_iterator E) const {
407 int Count = 0;
408 while (It != E) {
409 if (!isRetiredBlock(*It))
410 ++Count;
411 ++It;
412 }
413 return Count;
414}
415
416bool AMDGPUCFGStructurizer::needMigrateBlock(MachineBasicBlock *MBB) const {
417 unsigned BlockSizeThreshold = 30;
418 unsigned CloneInstrThreshold = 100;
419 bool MultiplePreds = MBB && (MBB->pred_size() > 1);
420
421 if(!MultiplePreds)
422 return false;
423 unsigned BlkSize = MBB->size();
424 return ((BlkSize > BlockSizeThreshold) &&
425 (BlkSize * (MBB->pred_size() - 1) > CloneInstrThreshold));
426}
427
428void AMDGPUCFGStructurizer::reversePredicateSetter(
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000429 MachineBasicBlock::iterator I, MachineBasicBlock &MBB) {
Duncan P. N. Exon Smithf197b1f2016-08-12 05:05:36 +0000430 assert(I.isValid() && "Expected valid iterator");
Duncan P. N. Exon Smith221847e2016-07-08 19:00:17 +0000431 for (;; --I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000432 if (I == MBB.end())
433 continue;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000434 if (I->getOpcode() == R600::PRED_X) {
Duncan P. N. Exon Smithf197b1f2016-08-12 05:05:36 +0000435 switch (I->getOperand(2).getImm()) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000436 case R600::PRED_SETE_INT:
437 I->getOperand(2).setImm(R600::PRED_SETNE_INT);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000438 return;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000439 case R600::PRED_SETNE_INT:
440 I->getOperand(2).setImm(R600::PRED_SETE_INT);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000441 return;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000442 case R600::PRED_SETE:
443 I->getOperand(2).setImm(R600::PRED_SETNE);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000444 return;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000445 case R600::PRED_SETNE:
446 I->getOperand(2).setImm(R600::PRED_SETE);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000447 return;
448 default:
449 llvm_unreachable("PRED_X Opcode invalid!");
450 }
451 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000452 }
453}
454
Vincent Lejeune960a6222013-07-19 21:45:06 +0000455void AMDGPUCFGStructurizer::insertInstrEnd(MachineBasicBlock *MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000456 int NewOpcode, const DebugLoc &DL) {
457 MachineInstr *MI =
458 MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000459 MBB->push_back(MI);
460 //assume the instruction doesn't take any reg operand ...
461 SHOWNEWINSTR(MI);
462}
Tom Stellard75aadc22012-12-11 21:25:42 +0000463
Vincent Lejeune960a6222013-07-19 21:45:06 +0000464MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(MachineBasicBlock *MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000465 int NewOpcode,
466 const DebugLoc &DL) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000467 MachineInstr *MI =
468 MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
469 if (MBB->begin() != MBB->end())
470 MBB->insert(MBB->begin(), MI);
471 else
472 MBB->push_back(MI);
473 SHOWNEWINSTR(MI);
474 return MI;
475}
476
477MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(
478 MachineBasicBlock::iterator I, int NewOpcode) {
479 MachineInstr *OldMI = &(*I);
480 MachineBasicBlock *MBB = OldMI->getParent();
481 MachineInstr *NewMBB =
482 MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DebugLoc());
483 MBB->insert(I, NewMBB);
484 //assume the instruction doesn't take any reg operand ...
485 SHOWNEWINSTR(NewMBB);
486 return NewMBB;
487}
488
489void AMDGPUCFGStructurizer::insertCondBranchBefore(
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000490 MachineBasicBlock::iterator I, int NewOpcode, const DebugLoc &DL) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000491 MachineInstr *OldMI = &(*I);
492 MachineBasicBlock *MBB = OldMI->getParent();
493 MachineFunction *MF = MBB->getParent();
494 MachineInstr *NewMI = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
495 MBB->insert(I, NewMI);
496 MachineInstrBuilder MIB(*MF, NewMI);
497 MIB.addReg(OldMI->getOperand(1).getReg(), false);
498 SHOWNEWINSTR(NewMI);
499 //erase later oldInstr->eraseFromParent();
500}
501
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000502void AMDGPUCFGStructurizer::insertCondBranchBefore(
503 MachineBasicBlock *blk, MachineBasicBlock::iterator I, int NewOpcode,
504 int RegNum, const DebugLoc &DL) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000505 MachineFunction *MF = blk->getParent();
506 MachineInstr *NewInstr = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
507 //insert before
508 blk->insert(I, NewInstr);
509 MachineInstrBuilder(*MF, NewInstr).addReg(RegNum, false);
510 SHOWNEWINSTR(NewInstr);
511}
512
Vincent Lejeune960a6222013-07-19 21:45:06 +0000513int AMDGPUCFGStructurizer::getBranchNzeroOpcode(int OldOpcode) {
514 switch(OldOpcode) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000515 case R600::JUMP_COND:
516 case R600::JUMP: return R600::IF_PREDICATE_SET;
517 case R600::BRANCH_COND_i32:
518 case R600::BRANCH_COND_f32: return R600::IF_LOGICALNZ_f32;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000519 default: llvm_unreachable("internal error");
520 }
521 return -1;
522}
523
524int AMDGPUCFGStructurizer::getBranchZeroOpcode(int OldOpcode) {
525 switch(OldOpcode) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000526 case R600::JUMP_COND:
527 case R600::JUMP: return R600::IF_PREDICATE_SET;
528 case R600::BRANCH_COND_i32:
529 case R600::BRANCH_COND_f32: return R600::IF_LOGICALZ_f32;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000530 default: llvm_unreachable("internal error");
531 }
532 return -1;
533}
534
535int AMDGPUCFGStructurizer::getContinueNzeroOpcode(int OldOpcode) {
536 switch(OldOpcode) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000537 case R600::JUMP_COND:
538 case R600::JUMP: return R600::CONTINUE_LOGICALNZ_i32;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000539 default: llvm_unreachable("internal error");
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000540 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000541 return -1;
542}
543
544int AMDGPUCFGStructurizer::getContinueZeroOpcode(int OldOpcode) {
545 switch(OldOpcode) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000546 case R600::JUMP_COND:
547 case R600::JUMP: return R600::CONTINUE_LOGICALZ_i32;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000548 default: llvm_unreachable("internal error");
549 }
550 return -1;
551}
552
553MachineBasicBlock *AMDGPUCFGStructurizer::getTrueBranch(MachineInstr *MI) {
554 return MI->getOperand(0).getMBB();
555}
556
557void AMDGPUCFGStructurizer::setTrueBranch(MachineInstr *MI,
558 MachineBasicBlock *MBB) {
559 MI->getOperand(0).setMBB(MBB);
560}
561
562MachineBasicBlock *
563AMDGPUCFGStructurizer::getFalseBranch(MachineBasicBlock *MBB,
564 MachineInstr *MI) {
565 assert(MBB->succ_size() == 2);
566 MachineBasicBlock *TrueBranch = getTrueBranch(MI);
567 MachineBasicBlock::succ_iterator It = MBB->succ_begin();
568 MachineBasicBlock::succ_iterator Next = It;
569 ++Next;
570 return (*It == TrueBranch) ? *Next : *It;
571}
572
573bool AMDGPUCFGStructurizer::isCondBranch(MachineInstr *MI) {
574 switch (MI->getOpcode()) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000575 case R600::JUMP_COND:
576 case R600::BRANCH_COND_i32:
577 case R600::BRANCH_COND_f32: return true;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000578 default:
579 return false;
580 }
581 return false;
582}
583
584bool AMDGPUCFGStructurizer::isUncondBranch(MachineInstr *MI) {
585 switch (MI->getOpcode()) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000586 case R600::JUMP:
587 case R600::BRANCH:
Vincent Lejeune960a6222013-07-19 21:45:06 +0000588 return true;
589 default:
590 return false;
591 }
592 return false;
593}
594
595DebugLoc AMDGPUCFGStructurizer::getLastDebugLocInBB(MachineBasicBlock *MBB) {
596 //get DebugLoc from the first MachineBasicBlock instruction with debug info
597 DebugLoc DL;
598 for (MachineBasicBlock::iterator It = MBB->begin(); It != MBB->end();
599 ++It) {
600 MachineInstr *instr = &(*It);
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000601 if (instr->getDebugLoc())
Vincent Lejeune960a6222013-07-19 21:45:06 +0000602 DL = instr->getDebugLoc();
603 }
604 return DL;
605}
606
607MachineInstr *AMDGPUCFGStructurizer::getNormalBlockBranchInstr(
608 MachineBasicBlock *MBB) {
609 MachineBasicBlock::reverse_iterator It = MBB->rbegin();
610 MachineInstr *MI = &*It;
611 if (MI && (isCondBranch(MI) || isUncondBranch(MI)))
612 return MI;
Craig Topper062a2ba2014-04-25 05:30:21 +0000613 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000614}
615
616MachineInstr *AMDGPUCFGStructurizer::getLoopendBlockBranchInstr(
617 MachineBasicBlock *MBB) {
618 for (MachineBasicBlock::reverse_iterator It = MBB->rbegin(), E = MBB->rend();
619 It != E; ++It) {
620 // FIXME: Simplify
621 MachineInstr *MI = &*It;
622 if (MI) {
623 if (isCondBranch(MI) || isUncondBranch(MI))
624 return MI;
625 else if (!TII->isMov(MI->getOpcode()))
626 break;
627 }
628 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000629 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000630}
631
632MachineInstr *AMDGPUCFGStructurizer::getReturnInstr(MachineBasicBlock *MBB) {
633 MachineBasicBlock::reverse_iterator It = MBB->rbegin();
634 if (It != MBB->rend()) {
635 MachineInstr *instr = &(*It);
Tom Stellardc5a154d2018-06-28 23:47:12 +0000636 if (instr->getOpcode() == R600::RETURN)
Vincent Lejeune960a6222013-07-19 21:45:06 +0000637 return instr;
638 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000639 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000640}
641
Vincent Lejeune960a6222013-07-19 21:45:06 +0000642bool AMDGPUCFGStructurizer::isReturnBlock(MachineBasicBlock *MBB) {
643 MachineInstr *MI = getReturnInstr(MBB);
644 bool IsReturn = (MBB->succ_size() == 0);
645 if (MI)
646 assert(IsReturn);
647 else if (IsReturn)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000648 LLVM_DEBUG(dbgs() << "BB" << MBB->getNumber()
649 << " is return block without RETURN instr\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000650 return IsReturn;
651}
652
653void AMDGPUCFGStructurizer::cloneSuccessorList(MachineBasicBlock *DstMBB,
654 MachineBasicBlock *SrcMBB) {
655 for (MachineBasicBlock::succ_iterator It = SrcMBB->succ_begin(),
656 iterEnd = SrcMBB->succ_end(); It != iterEnd; ++It)
657 DstMBB->addSuccessor(*It); // *iter's predecessor is also taken care of
658}
659
660MachineBasicBlock *AMDGPUCFGStructurizer::clone(MachineBasicBlock *MBB) {
661 MachineFunction *Func = MBB->getParent();
662 MachineBasicBlock *NewMBB = Func->CreateMachineBasicBlock();
663 Func->push_back(NewMBB); //insert to function
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000664 for (const MachineInstr &It : *MBB)
665 NewMBB->push_back(Func->CloneMachineInstr(&It));
Vincent Lejeune960a6222013-07-19 21:45:06 +0000666 return NewMBB;
667}
668
669void AMDGPUCFGStructurizer::replaceInstrUseOfBlockWith(
670 MachineBasicBlock *SrcMBB, MachineBasicBlock *OldMBB,
671 MachineBasicBlock *NewBlk) {
672 MachineInstr *BranchMI = getLoopendBlockBranchInstr(SrcMBB);
673 if (BranchMI && isCondBranch(BranchMI) &&
674 getTrueBranch(BranchMI) == OldMBB)
675 setTrueBranch(BranchMI, NewBlk);
676}
677
678void AMDGPUCFGStructurizer::wrapup(MachineBasicBlock *MBB) {
679 assert((!MBB->getParent()->getJumpTableInfo()
680 || MBB->getParent()->getJumpTableInfo()->isEmpty())
681 && "found a jump table");
682
683 //collect continue right before endloop
684 SmallVector<MachineInstr *, DEFAULT_VEC_SLOTS> ContInstr;
685 MachineBasicBlock::iterator Pre = MBB->begin();
686 MachineBasicBlock::iterator E = MBB->end();
687 MachineBasicBlock::iterator It = Pre;
688 while (It != E) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000689 if (Pre->getOpcode() == R600::CONTINUE
690 && It->getOpcode() == R600::ENDLOOP)
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000691 ContInstr.push_back(&*Pre);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000692 Pre = It;
693 ++It;
694 }
695
696 //delete continue right before endloop
697 for (unsigned i = 0; i < ContInstr.size(); ++i)
698 ContInstr[i]->eraseFromParent();
699
700 // TODO to fix up jump table so later phase won't be confused. if
701 // (jumpTableInfo->isEmpty() == false) { need to clean the jump table, but
702 // there isn't such an interface yet. alternatively, replace all the other
703 // blocks in the jump table with the entryBlk //}
Vincent Lejeune960a6222013-07-19 21:45:06 +0000704}
705
Vincent Lejeune960a6222013-07-19 21:45:06 +0000706bool AMDGPUCFGStructurizer::prepare() {
707 bool Changed = false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000708
709 //FIXME: if not reducible flow graph, make it so ???
710
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000711 LLVM_DEBUG(dbgs() << "AMDGPUCFGStructurizer::prepare\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000712
Vincent Lejeune960a6222013-07-19 21:45:06 +0000713 orderBlocks(FuncRep);
Tom Stellard75aadc22012-12-11 21:25:42 +0000714
Vincent Lejeune960a6222013-07-19 21:45:06 +0000715 SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> RetBlks;
Tom Stellard75aadc22012-12-11 21:25:42 +0000716
Vincent Lejeune960a6222013-07-19 21:45:06 +0000717 // Add an ExitBlk to loop that don't have one
718 for (MachineLoopInfo::iterator It = MLI->begin(),
719 E = MLI->end(); It != E; ++It) {
720 MachineLoop *LoopRep = (*It);
721 MBBVector ExitingMBBs;
722 LoopRep->getExitingBlocks(ExitingMBBs);
Tom Stellard75aadc22012-12-11 21:25:42 +0000723
Vincent Lejeune960a6222013-07-19 21:45:06 +0000724 if (ExitingMBBs.size() == 0) {
725 MachineBasicBlock* DummyExitBlk = normalizeInfiniteLoopExit(LoopRep);
726 if (DummyExitBlk)
727 RetBlks.push_back(DummyExitBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +0000728 }
729 }
730
731 // Remove unconditional branch instr.
732 // Add dummy exit block iff there are multiple returns.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000733 for (SmallVectorImpl<MachineBasicBlock *>::const_iterator
734 It = OrderedBlks.begin(), E = OrderedBlks.end(); It != E; ++It) {
735 MachineBasicBlock *MBB = *It;
736 removeUnconditionalBranch(MBB);
737 removeRedundantConditionalBranch(MBB);
738 if (isReturnBlock(MBB)) {
739 RetBlks.push_back(MBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000740 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000741 assert(MBB->succ_size() <= 2);
Tom Stellard75aadc22012-12-11 21:25:42 +0000742 }
743
Vincent Lejeune960a6222013-07-19 21:45:06 +0000744 if (RetBlks.size() >= 2) {
745 addDummyExitBlock(RetBlks);
746 Changed = true;
747 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000748
Vincent Lejeune960a6222013-07-19 21:45:06 +0000749 return Changed;
750}
751
752bool AMDGPUCFGStructurizer::run() {
Tom Stellard75aadc22012-12-11 21:25:42 +0000753 //Assume reducible CFG...
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000754 LLVM_DEBUG(dbgs() << "AMDGPUCFGStructurizer::run\n");
Tom Stellard75aadc22012-12-11 21:25:42 +0000755
Tom Stellard75aadc22012-12-11 21:25:42 +0000756#ifdef STRESSTEST
757 //Use the worse block ordering to test the algorithm.
758 ReverseVector(orderedBlks);
759#endif
760
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000761 LLVM_DEBUG(dbgs() << "Ordered blocks:\n"; printOrderedBlocks(););
Vincent Lejeune960a6222013-07-19 21:45:06 +0000762 int NumIter = 0;
763 bool Finish = false;
764 MachineBasicBlock *MBB;
765 bool MakeProgress = false;
766 int NumRemainedBlk = countActiveBlock(OrderedBlks.begin(),
767 OrderedBlks.end());
Tom Stellard75aadc22012-12-11 21:25:42 +0000768
769 do {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000770 ++NumIter;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000771 LLVM_DEBUG(dbgs() << "numIter = " << NumIter
772 << ", numRemaintedBlk = " << NumRemainedBlk << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000773
Vincent Lejeune960a6222013-07-19 21:45:06 +0000774 SmallVectorImpl<MachineBasicBlock *>::const_iterator It =
775 OrderedBlks.begin();
776 SmallVectorImpl<MachineBasicBlock *>::const_iterator E =
777 OrderedBlks.end();
Tom Stellard75aadc22012-12-11 21:25:42 +0000778
Vincent Lejeune960a6222013-07-19 21:45:06 +0000779 SmallVectorImpl<MachineBasicBlock *>::const_iterator SccBeginIter =
780 It;
Craig Topper062a2ba2014-04-25 05:30:21 +0000781 MachineBasicBlock *SccBeginMBB = nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000782 int SccNumBlk = 0; // The number of active blocks, init to a
Tom Stellard75aadc22012-12-11 21:25:42 +0000783 // maximum possible number.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000784 int SccNumIter; // Number of iteration in this SCC.
Tom Stellard75aadc22012-12-11 21:25:42 +0000785
Vincent Lejeune960a6222013-07-19 21:45:06 +0000786 while (It != E) {
787 MBB = *It;
Tom Stellard75aadc22012-12-11 21:25:42 +0000788
Vincent Lejeune960a6222013-07-19 21:45:06 +0000789 if (!SccBeginMBB) {
790 SccBeginIter = It;
791 SccBeginMBB = MBB;
792 SccNumIter = 0;
793 SccNumBlk = NumRemainedBlk; // Init to maximum possible number.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000794 LLVM_DEBUG(dbgs() << "start processing SCC" << getSCCNum(SccBeginMBB);
795 dbgs() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000796 }
797
Vincent Lejeune960a6222013-07-19 21:45:06 +0000798 if (!isRetiredBlock(MBB))
799 patternMatch(MBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000800
Vincent Lejeune960a6222013-07-19 21:45:06 +0000801 ++It;
Tom Stellard75aadc22012-12-11 21:25:42 +0000802
Vincent Lejeune960a6222013-07-19 21:45:06 +0000803 bool ContNextScc = true;
804 if (It == E
805 || getSCCNum(SccBeginMBB) != getSCCNum(*It)) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000806 // Just finish one scc.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000807 ++SccNumIter;
808 int sccRemainedNumBlk = countActiveBlock(SccBeginIter, It);
809 if (sccRemainedNumBlk != 1 && sccRemainedNumBlk >= SccNumBlk) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000810 LLVM_DEBUG(dbgs() << "Can't reduce SCC " << getSCCNum(MBB)
811 << ", sccNumIter = " << SccNumIter;
812 dbgs() << "doesn't make any progress\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000813 ContNextScc = true;
814 } else if (sccRemainedNumBlk != 1 && sccRemainedNumBlk < SccNumBlk) {
815 SccNumBlk = sccRemainedNumBlk;
816 It = SccBeginIter;
817 ContNextScc = false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000818 LLVM_DEBUG(dbgs() << "repeat processing SCC" << getSCCNum(MBB)
819 << "sccNumIter = " << SccNumIter << '\n';);
Tom Stellard75aadc22012-12-11 21:25:42 +0000820 } else {
821 // Finish the current scc.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000822 ContNextScc = true;
Tom Stellard75aadc22012-12-11 21:25:42 +0000823 }
824 } else {
825 // Continue on next component in the current scc.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000826 ContNextScc = false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000827 }
828
Vincent Lejeune960a6222013-07-19 21:45:06 +0000829 if (ContNextScc)
Craig Topper062a2ba2014-04-25 05:30:21 +0000830 SccBeginMBB = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +0000831 } //while, "one iteration" over the function.
832
Vincent Lejeune960a6222013-07-19 21:45:06 +0000833 MachineBasicBlock *EntryMBB =
Tim Shenb5e0f5a2016-08-19 21:20:13 +0000834 *GraphTraits<MachineFunction *>::nodes_begin(FuncRep);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000835 if (EntryMBB->succ_size() == 0) {
836 Finish = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000837 LLVM_DEBUG(dbgs() << "Reduce to one block\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000838 } else {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000839 int NewnumRemainedBlk
840 = countActiveBlock(OrderedBlks.begin(), OrderedBlks.end());
Tom Stellard75aadc22012-12-11 21:25:42 +0000841 // consider cloned blocks ??
Vincent Lejeune960a6222013-07-19 21:45:06 +0000842 if (NewnumRemainedBlk == 1 || NewnumRemainedBlk < NumRemainedBlk) {
843 MakeProgress = true;
844 NumRemainedBlk = NewnumRemainedBlk;
Tom Stellard75aadc22012-12-11 21:25:42 +0000845 } else {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000846 MakeProgress = false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000847 LLVM_DEBUG(dbgs() << "No progress\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000848 }
849 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000850 } while (!Finish && MakeProgress);
Tom Stellard75aadc22012-12-11 21:25:42 +0000851
852 // Misc wrap up to maintain the consistency of the Function representation.
Tim Shenb5e0f5a2016-08-19 21:20:13 +0000853 wrapup(*GraphTraits<MachineFunction *>::nodes_begin(FuncRep));
Tom Stellard75aadc22012-12-11 21:25:42 +0000854
855 // Detach retired Block, release memory.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000856 for (MBBInfoMap::iterator It = BlockInfoMap.begin(), E = BlockInfoMap.end();
857 It != E; ++It) {
858 if ((*It).second && (*It).second->IsRetired) {
859 assert(((*It).first)->getNumber() != -1);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000860 LLVM_DEBUG(dbgs() << "Erase BB" << ((*It).first)->getNumber() << "\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000861 (*It).first->eraseFromParent(); //Remove from the parent Function.
Tom Stellard75aadc22012-12-11 21:25:42 +0000862 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000863 delete (*It).second;
Tom Stellard75aadc22012-12-11 21:25:42 +0000864 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000865 BlockInfoMap.clear();
866 LLInfoMap.clear();
Tom Stellard75aadc22012-12-11 21:25:42 +0000867
Matt Arsenaultdb8b1d52014-03-24 20:29:02 +0000868 if (!Finish) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000869 LLVM_DEBUG(FuncRep->viewCFG());
Matt Arsenault5de68cb2016-03-02 03:33:55 +0000870 report_fatal_error("IRREDUCIBLE_CFG");
Matt Arsenaultdb8b1d52014-03-24 20:29:02 +0000871 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000872
873 return true;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000874}
Tom Stellard75aadc22012-12-11 21:25:42 +0000875
Vincent Lejeune960a6222013-07-19 21:45:06 +0000876void AMDGPUCFGStructurizer::orderBlocks(MachineFunction *MF) {
877 int SccNum = 0;
878 MachineBasicBlock *MBB;
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000879 for (scc_iterator<MachineFunction *> It = scc_begin(MF); !It.isAtEnd();
880 ++It, ++SccNum) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000881 const std::vector<MachineBasicBlock *> &SccNext = *It;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000882 for (std::vector<MachineBasicBlock *>::const_iterator
883 blockIter = SccNext.begin(), blockEnd = SccNext.end();
Tom Stellard75aadc22012-12-11 21:25:42 +0000884 blockIter != blockEnd; ++blockIter) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000885 MBB = *blockIter;
886 OrderedBlks.push_back(MBB);
887 recordSccnum(MBB, SccNum);
Tom Stellard75aadc22012-12-11 21:25:42 +0000888 }
889 }
890
Daniel Berlin58a6e572017-02-09 20:37:24 +0000891 // walk through all the block in func to check for unreachable
Daniel Berlin73ad5cb2017-02-09 20:37:46 +0000892 for (auto *MBB : nodes(MF)) {
Vincent Lejeune960a6222013-07-19 21:45:06 +0000893 SccNum = getSCCNum(MBB);
894 if (SccNum == INVALIDSCCNUM)
895 dbgs() << "unreachable block BB" << MBB->getNumber() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +0000896 }
Vincent Lejeune960a6222013-07-19 21:45:06 +0000897}
Tom Stellard75aadc22012-12-11 21:25:42 +0000898
Vincent Lejeune960a6222013-07-19 21:45:06 +0000899int AMDGPUCFGStructurizer::patternMatch(MachineBasicBlock *MBB) {
900 int NumMatch = 0;
901 int CurMatch;
Tom Stellard75aadc22012-12-11 21:25:42 +0000902
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000903 LLVM_DEBUG(dbgs() << "Begin patternMatch BB" << MBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000904
Vincent Lejeune960a6222013-07-19 21:45:06 +0000905 while ((CurMatch = patternMatchGroup(MBB)) > 0)
906 NumMatch += CurMatch;
Tom Stellard75aadc22012-12-11 21:25:42 +0000907
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000908 LLVM_DEBUG(dbgs() << "End patternMatch BB" << MBB->getNumber()
909 << ", numMatch = " << NumMatch << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +0000910
Vincent Lejeune960a6222013-07-19 21:45:06 +0000911 return NumMatch;
912}
Tom Stellard75aadc22012-12-11 21:25:42 +0000913
Vincent Lejeune960a6222013-07-19 21:45:06 +0000914int AMDGPUCFGStructurizer::patternMatchGroup(MachineBasicBlock *MBB) {
915 int NumMatch = 0;
916 NumMatch += loopendPatternMatch();
917 NumMatch += serialPatternMatch(MBB);
918 NumMatch += ifPatternMatch(MBB);
919 return NumMatch;
920}
Tom Stellard75aadc22012-12-11 21:25:42 +0000921
Vincent Lejeune960a6222013-07-19 21:45:06 +0000922int AMDGPUCFGStructurizer::serialPatternMatch(MachineBasicBlock *MBB) {
923 if (MBB->succ_size() != 1)
Tom Stellard75aadc22012-12-11 21:25:42 +0000924 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000925
Vincent Lejeune960a6222013-07-19 21:45:06 +0000926 MachineBasicBlock *childBlk = *MBB->succ_begin();
927 if (childBlk->pred_size() != 1 || isActiveLoophead(childBlk))
Tom Stellard75aadc22012-12-11 21:25:42 +0000928 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000929
Vincent Lejeune960a6222013-07-19 21:45:06 +0000930 mergeSerialBlock(MBB, childBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +0000931 ++numSerialPatternMatch;
932 return 1;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000933}
Tom Stellard75aadc22012-12-11 21:25:42 +0000934
Vincent Lejeune960a6222013-07-19 21:45:06 +0000935int AMDGPUCFGStructurizer::ifPatternMatch(MachineBasicBlock *MBB) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000936 //two edges
Vincent Lejeune960a6222013-07-19 21:45:06 +0000937 if (MBB->succ_size() != 2)
Tom Stellard75aadc22012-12-11 21:25:42 +0000938 return 0;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000939 if (hasBackEdge(MBB))
Tom Stellard75aadc22012-12-11 21:25:42 +0000940 return 0;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000941 MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
942 if (!BranchMI)
Tom Stellard75aadc22012-12-11 21:25:42 +0000943 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000944
Vincent Lejeune960a6222013-07-19 21:45:06 +0000945 assert(isCondBranch(BranchMI));
Tom Stellard827ec9b2013-11-18 19:43:38 +0000946 int NumMatch = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000947
Vincent Lejeune960a6222013-07-19 21:45:06 +0000948 MachineBasicBlock *TrueMBB = getTrueBranch(BranchMI);
Tom Stellard827ec9b2013-11-18 19:43:38 +0000949 NumMatch += serialPatternMatch(TrueMBB);
950 NumMatch += ifPatternMatch(TrueMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000951 MachineBasicBlock *FalseMBB = getFalseBranch(MBB, BranchMI);
Tom Stellard827ec9b2013-11-18 19:43:38 +0000952 NumMatch += serialPatternMatch(FalseMBB);
953 NumMatch += ifPatternMatch(FalseMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +0000954 MachineBasicBlock *LandBlk;
955 int Cloned = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000956
Vincent Lejeune960a6222013-07-19 21:45:06 +0000957 assert (!TrueMBB->succ_empty() || !FalseMBB->succ_empty());
Tom Stellard75aadc22012-12-11 21:25:42 +0000958 // TODO: Simplify
Vincent Lejeune960a6222013-07-19 21:45:06 +0000959 if (TrueMBB->succ_size() == 1 && FalseMBB->succ_size() == 1
960 && *TrueMBB->succ_begin() == *FalseMBB->succ_begin()) {
961 // Diamond pattern
962 LandBlk = *TrueMBB->succ_begin();
963 } else if (TrueMBB->succ_size() == 1 && *TrueMBB->succ_begin() == FalseMBB) {
964 // Triangle pattern, false is empty
965 LandBlk = FalseMBB;
Craig Topper062a2ba2014-04-25 05:30:21 +0000966 FalseMBB = nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000967 } else if (FalseMBB->succ_size() == 1
968 && *FalseMBB->succ_begin() == TrueMBB) {
969 // Triangle pattern, true is empty
Vincent Lejeune8b8a7b52013-07-19 21:45:15 +0000970 // We reverse the predicate to make a triangle, empty false pattern;
971 std::swap(TrueMBB, FalseMBB);
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000972 reversePredicateSetter(MBB->end(), *MBB);
Vincent Lejeune8b8a7b52013-07-19 21:45:15 +0000973 LandBlk = FalseMBB;
Craig Topper062a2ba2014-04-25 05:30:21 +0000974 FalseMBB = nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +0000975 } else if (FalseMBB->succ_size() == 1
976 && isSameloopDetachedContbreak(TrueMBB, FalseMBB)) {
977 LandBlk = *FalseMBB->succ_begin();
978 } else if (TrueMBB->succ_size() == 1
979 && isSameloopDetachedContbreak(FalseMBB, TrueMBB)) {
980 LandBlk = *TrueMBB->succ_begin();
Tom Stellard75aadc22012-12-11 21:25:42 +0000981 } else {
Tom Stellard827ec9b2013-11-18 19:43:38 +0000982 return NumMatch + handleJumpintoIf(MBB, TrueMBB, FalseMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000983 }
984
985 // improveSimpleJumpinfoIf can handle the case where landBlk == NULL but the
986 // new BB created for landBlk==NULL may introduce new challenge to the
987 // reduction process.
Vincent Lejeune960a6222013-07-19 21:45:06 +0000988 if (LandBlk &&
989 ((TrueMBB && TrueMBB->pred_size() > 1)
990 || (FalseMBB && FalseMBB->pred_size() > 1))) {
991 Cloned += improveSimpleJumpintoIf(MBB, TrueMBB, FalseMBB, &LandBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +0000992 }
993
Vincent Lejeune960a6222013-07-19 21:45:06 +0000994 if (TrueMBB && TrueMBB->pred_size() > 1) {
995 TrueMBB = cloneBlockForPredecessor(TrueMBB, MBB);
996 ++Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +0000997 }
998
Vincent Lejeune960a6222013-07-19 21:45:06 +0000999 if (FalseMBB && FalseMBB->pred_size() > 1) {
1000 FalseMBB = cloneBlockForPredecessor(FalseMBB, MBB);
1001 ++Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +00001002 }
1003
Vincent Lejeune960a6222013-07-19 21:45:06 +00001004 mergeIfthenelseBlock(BranchMI, MBB, TrueMBB, FalseMBB, LandBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +00001005
1006 ++numIfPatternMatch;
1007
Vincent Lejeune960a6222013-07-19 21:45:06 +00001008 numClonedBlock += Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +00001009
Tom Stellard827ec9b2013-11-18 19:43:38 +00001010 return 1 + Cloned + NumMatch;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001011}
Tom Stellard75aadc22012-12-11 21:25:42 +00001012
Vincent Lejeune960a6222013-07-19 21:45:06 +00001013int AMDGPUCFGStructurizer::loopendPatternMatch() {
Jan Vesely18b289f2015-03-13 17:32:43 +00001014 std::deque<MachineLoop *> NestedLoops;
1015 for (auto &It: *MLI)
1016 for (MachineLoop *ML : depth_first(It))
1017 NestedLoops.push_front(ML);
David Blaikieceec2bd2014-04-11 01:50:01 +00001018
Eugene Zelenko66203762017-01-21 00:53:49 +00001019 if (NestedLoops.empty())
Tom Stellard75aadc22012-12-11 21:25:42 +00001020 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001021
Jan Vesely18b289f2015-03-13 17:32:43 +00001022 // Process nested loop outside->inside (we did push_front),
1023 // so "continue" to a outside loop won't be mistaken as "break"
1024 // of the current loop.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001025 int Num = 0;
Jan Vesely18b289f2015-03-13 17:32:43 +00001026 for (MachineLoop *ExaminedLoop : NestedLoops) {
Vincent Lejeune960a6222013-07-19 21:45:06 +00001027 if (ExaminedLoop->getNumBlocks() == 0 || Visited[ExaminedLoop])
Tom Stellard75aadc22012-12-11 21:25:42 +00001028 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001029 LLVM_DEBUG(dbgs() << "Processing:\n"; ExaminedLoop->dump(););
Vincent Lejeune960a6222013-07-19 21:45:06 +00001030 int NumBreak = mergeLoop(ExaminedLoop);
1031 if (NumBreak == -1)
Tom Stellard75aadc22012-12-11 21:25:42 +00001032 break;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001033 Num += NumBreak;
1034 }
1035 return Num;
1036}
Tom Stellard75aadc22012-12-11 21:25:42 +00001037
Vincent Lejeune960a6222013-07-19 21:45:06 +00001038int AMDGPUCFGStructurizer::mergeLoop(MachineLoop *LoopRep) {
1039 MachineBasicBlock *LoopHeader = LoopRep->getHeader();
1040 MBBVector ExitingMBBs;
1041 LoopRep->getExitingBlocks(ExitingMBBs);
1042 assert(!ExitingMBBs.empty() && "Infinite Loop not supported");
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001043 LLVM_DEBUG(dbgs() << "Loop has " << ExitingMBBs.size()
1044 << " exiting blocks\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001045 // We assume a single ExitBlk
1046 MBBVector ExitBlks;
1047 LoopRep->getExitBlocks(ExitBlks);
1048 SmallPtrSet<MachineBasicBlock *, 2> ExitBlkSet;
1049 for (unsigned i = 0, e = ExitBlks.size(); i < e; ++i)
1050 ExitBlkSet.insert(ExitBlks[i]);
1051 assert(ExitBlkSet.size() == 1);
1052 MachineBasicBlock *ExitBlk = *ExitBlks.begin();
1053 assert(ExitBlk && "Loop has several exit block");
1054 MBBVector LatchBlks;
Daniel Berlin73ad5cb2017-02-09 20:37:46 +00001055 for (auto *LB : inverse_children<MachineBasicBlock*>(LoopHeader))
Daniel Berlin58a6e572017-02-09 20:37:24 +00001056 if (LoopRep->contains(LB))
1057 LatchBlks.push_back(LB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001058
Vincent Lejeune960a6222013-07-19 21:45:06 +00001059 for (unsigned i = 0, e = ExitingMBBs.size(); i < e; ++i)
1060 mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk);
1061 for (unsigned i = 0, e = LatchBlks.size(); i < e; ++i)
1062 settleLoopcontBlock(LatchBlks[i], LoopHeader);
1063 int Match = 0;
1064 do {
1065 Match = 0;
1066 Match += serialPatternMatch(LoopHeader);
1067 Match += ifPatternMatch(LoopHeader);
1068 } while (Match > 0);
1069 mergeLooplandBlock(LoopHeader, ExitBlk);
1070 MachineLoop *ParentLoop = LoopRep->getParentLoop();
1071 if (ParentLoop)
1072 MLI->changeLoopFor(LoopHeader, ParentLoop);
1073 else
1074 MLI->removeBlock(LoopHeader);
1075 Visited[LoopRep] = true;
1076 return 1;
1077}
Tom Stellard75aadc22012-12-11 21:25:42 +00001078
Vincent Lejeune960a6222013-07-19 21:45:06 +00001079bool AMDGPUCFGStructurizer::isSameloopDetachedContbreak(
1080 MachineBasicBlock *Src1MBB, MachineBasicBlock *Src2MBB) {
1081 if (Src1MBB->succ_size() == 0) {
1082 MachineLoop *LoopRep = MLI->getLoopFor(Src1MBB);
1083 if (LoopRep&& LoopRep == MLI->getLoopFor(Src2MBB)) {
1084 MachineBasicBlock *&TheEntry = LLInfoMap[LoopRep];
1085 if (TheEntry) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001086 LLVM_DEBUG(dbgs() << "isLoopContBreakBlock yes src1 = BB"
1087 << Src1MBB->getNumber() << " src2 = BB"
1088 << Src2MBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001089 return true;
1090 }
1091 }
1092 }
1093 return false;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001094}
Tom Stellard75aadc22012-12-11 21:25:42 +00001095
Vincent Lejeune960a6222013-07-19 21:45:06 +00001096int AMDGPUCFGStructurizer::handleJumpintoIf(MachineBasicBlock *HeadMBB,
1097 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
1098 int Num = handleJumpintoIfImp(HeadMBB, TrueMBB, FalseMBB);
1099 if (Num == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001100 LLVM_DEBUG(dbgs() << "handleJumpintoIf swap trueBlk and FalseBlk"
1101 << "\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001102 Num = handleJumpintoIfImp(HeadMBB, FalseMBB, TrueMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001103 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001104 return Num;
Tom Stellard75aadc22012-12-11 21:25:42 +00001105}
1106
Vincent Lejeune960a6222013-07-19 21:45:06 +00001107int AMDGPUCFGStructurizer::handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
1108 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
1109 int Num = 0;
1110 MachineBasicBlock *DownBlk;
Tom Stellard75aadc22012-12-11 21:25:42 +00001111
1112 //trueBlk could be the common post dominator
Vincent Lejeune960a6222013-07-19 21:45:06 +00001113 DownBlk = TrueMBB;
Tom Stellard75aadc22012-12-11 21:25:42 +00001114
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001115 LLVM_DEBUG(dbgs() << "handleJumpintoIfImp head = BB" << HeadMBB->getNumber()
1116 << " true = BB" << TrueMBB->getNumber()
1117 << ", numSucc=" << TrueMBB->succ_size() << " false = BB"
1118 << FalseMBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001119
Vincent Lejeune960a6222013-07-19 21:45:06 +00001120 while (DownBlk) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001121 LLVM_DEBUG(dbgs() << "check down = BB" << DownBlk->getNumber(););
Tom Stellard75aadc22012-12-11 21:25:42 +00001122
Vincent Lejeune960a6222013-07-19 21:45:06 +00001123 if (singlePathTo(FalseMBB, DownBlk) == SinglePath_InPath) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001124 LLVM_DEBUG(dbgs() << " working\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001125
Vincent Lejeune960a6222013-07-19 21:45:06 +00001126 Num += cloneOnSideEntryTo(HeadMBB, TrueMBB, DownBlk);
1127 Num += cloneOnSideEntryTo(HeadMBB, FalseMBB, DownBlk);
Tom Stellard75aadc22012-12-11 21:25:42 +00001128
Vincent Lejeune960a6222013-07-19 21:45:06 +00001129 numClonedBlock += Num;
1130 Num += serialPatternMatch(*HeadMBB->succ_begin());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001131 Num += serialPatternMatch(*std::next(HeadMBB->succ_begin()));
Vincent Lejeune960a6222013-07-19 21:45:06 +00001132 Num += ifPatternMatch(HeadMBB);
1133 assert(Num > 0);
Tom Stellard75aadc22012-12-11 21:25:42 +00001134
1135 break;
1136 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001137 LLVM_DEBUG(dbgs() << " not working\n";);
Craig Topper062a2ba2014-04-25 05:30:21 +00001138 DownBlk = (DownBlk->succ_size() == 1) ? (*DownBlk->succ_begin()) : nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001139 } // walk down the postDomTree
1140
Vincent Lejeune960a6222013-07-19 21:45:06 +00001141 return Num;
1142}
Tom Stellard75aadc22012-12-11 21:25:42 +00001143
Florian Hahn6b3216a2017-07-31 10:07:49 +00001144#ifndef NDEBUG
Vincent Lejeune960a6222013-07-19 21:45:06 +00001145void AMDGPUCFGStructurizer::showImproveSimpleJumpintoIf(
1146 MachineBasicBlock *HeadMBB, MachineBasicBlock *TrueMBB,
1147 MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB, bool Detail) {
1148 dbgs() << "head = BB" << HeadMBB->getNumber()
1149 << " size = " << HeadMBB->size();
1150 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001151 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001152 HeadMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001153 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001154 }
1155
Vincent Lejeune960a6222013-07-19 21:45:06 +00001156 if (TrueMBB) {
1157 dbgs() << ", true = BB" << TrueMBB->getNumber() << " size = "
1158 << TrueMBB->size() << " numPred = " << TrueMBB->pred_size();
1159 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001160 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001161 TrueMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001162 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001163 }
1164 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001165 if (FalseMBB) {
1166 dbgs() << ", false = BB" << FalseMBB->getNumber() << " size = "
1167 << FalseMBB->size() << " numPred = " << FalseMBB->pred_size();
1168 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001169 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001170 FalseMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001171 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001172 }
1173 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001174 if (LandMBB) {
1175 dbgs() << ", land = BB" << LandMBB->getNumber() << " size = "
1176 << LandMBB->size() << " numPred = " << LandMBB->pred_size();
1177 if (Detail) {
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001178 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001179 LandMBB->print(dbgs());
Vincent Lejeunea8c38fe2013-07-19 21:44:56 +00001180 dbgs() << "\n";
Tom Stellard75aadc22012-12-11 21:25:42 +00001181 }
1182 }
1183
Eugene Zelenko66203762017-01-21 00:53:49 +00001184 dbgs() << "\n";
Vincent Lejeune960a6222013-07-19 21:45:06 +00001185}
Florian Hahn6b3216a2017-07-31 10:07:49 +00001186#endif
Tom Stellard75aadc22012-12-11 21:25:42 +00001187
Vincent Lejeune960a6222013-07-19 21:45:06 +00001188int AMDGPUCFGStructurizer::improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
1189 MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
1190 MachineBasicBlock **LandMBBPtr) {
1191 bool MigrateTrue = false;
1192 bool MigrateFalse = false;
Tom Stellard75aadc22012-12-11 21:25:42 +00001193
Vincent Lejeune960a6222013-07-19 21:45:06 +00001194 MachineBasicBlock *LandBlk = *LandMBBPtr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001195
Vincent Lejeune960a6222013-07-19 21:45:06 +00001196 assert((!TrueMBB || TrueMBB->succ_size() <= 1)
1197 && (!FalseMBB || FalseMBB->succ_size() <= 1));
Tom Stellard75aadc22012-12-11 21:25:42 +00001198
Vincent Lejeune960a6222013-07-19 21:45:06 +00001199 if (TrueMBB == FalseMBB)
Tom Stellard75aadc22012-12-11 21:25:42 +00001200 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001201
Vincent Lejeune960a6222013-07-19 21:45:06 +00001202 MigrateTrue = needMigrateBlock(TrueMBB);
1203 MigrateFalse = needMigrateBlock(FalseMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001204
Vincent Lejeune960a6222013-07-19 21:45:06 +00001205 if (!MigrateTrue && !MigrateFalse)
Tom Stellard75aadc22012-12-11 21:25:42 +00001206 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001207
1208 // If we need to migrate either trueBlk and falseBlk, migrate the rest that
1209 // have more than one predecessors. without doing this, its predecessor
1210 // rather than headBlk will have undefined value in initReg.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001211 if (!MigrateTrue && TrueMBB && TrueMBB->pred_size() > 1)
1212 MigrateTrue = true;
1213 if (!MigrateFalse && FalseMBB && FalseMBB->pred_size() > 1)
1214 MigrateFalse = true;
Tom Stellard75aadc22012-12-11 21:25:42 +00001215
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001216 LLVM_DEBUG(
1217 dbgs() << "before improveSimpleJumpintoIf: ";
1218 showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0););
Tom Stellard75aadc22012-12-11 21:25:42 +00001219
1220 // org: headBlk => if () {trueBlk} else {falseBlk} => landBlk
1221 //
1222 // new: headBlk => if () {initReg = 1; org trueBlk branch} else
1223 // {initReg = 0; org falseBlk branch }
1224 // => landBlk => if (initReg) {org trueBlk} else {org falseBlk}
1225 // => org landBlk
1226 // if landBlk->pred_size() > 2, put the about if-else inside
1227 // if (initReg !=2) {...}
1228 //
1229 // add initReg = initVal to headBlk
1230
1231 const TargetRegisterClass * I32RC = TRI->getCFGStructurizerRegClass(MVT::i32);
Tom Stellardb34186a2013-10-16 17:06:02 +00001232 if (!MigrateTrue || !MigrateFalse) {
1233 // XXX: We have an opportunity here to optimize the "branch into if" case
1234 // here. Branch into if looks like this:
1235 // entry
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001236 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001237 // diamond_head branch_from
1238 // / \ |
1239 // diamond_false diamond_true
1240 // \ /
1241 // done
1242 //
1243 // The diamond_head block begins the "if" and the diamond_true block
1244 // is the block being "branched into".
1245 //
1246 // If MigrateTrue is true, then TrueBB is the block being "branched into"
1247 // and if MigrateFalse is true, then FalseBB is the block being
1248 // "branched into"
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001249 //
Tom Stellardb34186a2013-10-16 17:06:02 +00001250 // Here is the pseudo code for how I think the optimization should work:
1251 // 1. Insert MOV GPR0, 0 before the branch instruction in diamond_head.
1252 // 2. Insert MOV GPR0, 1 before the branch instruction in branch_from.
1253 // 3. Move the branch instruction from diamond_head into its own basic
1254 // block (new_block).
1255 // 4. Add an unconditional branch from diamond_head to new_block
1256 // 5. Replace the branch instruction in branch_from with an unconditional
1257 // branch to new_block. If branch_from has multiple predecessors, then
1258 // we need to replace the True/False block in the branch
1259 // instruction instead of replacing it.
1260 // 6. Change the condition of the branch instruction in new_block from
1261 // COND to (COND || GPR0)
1262 //
1263 // In order insert these MOV instruction, we will need to use the
1264 // RegisterScavenger. Usually liveness stops being tracked during
1265 // the late machine optimization passes, however if we implement
1266 // bool TargetRegisterInfo::requiresRegisterScavenging(
1267 // const MachineFunction &MF)
Matt Arsenaulte0b44042015-09-10 21:51:19 +00001268 // and have it return true, liveness will be tracked correctly
Tom Stellardb34186a2013-10-16 17:06:02 +00001269 // by generic optimization passes. We will also need to make sure that
1270 // all of our target-specific passes that run after regalloc and before
1271 // the CFGStructurizer track liveness and we will need to modify this pass
1272 // to correctly track liveness.
1273 //
1274 // After the above changes, the new CFG should look like this:
1275 // entry
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001276 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001277 // diamond_head branch_from
1278 // \ /
1279 // new_block
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001280 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001281 // diamond_false diamond_true
1282 // \ /
1283 // done
1284 //
1285 // Without this optimization, we are forced to duplicate the diamond_true
1286 // block and we will end up with a CFG like this:
1287 //
1288 // entry
Benjamin Kramera9fe95b2013-10-18 14:12:50 +00001289 // / |
Tom Stellardb34186a2013-10-16 17:06:02 +00001290 // diamond_head branch_from
1291 // / \ |
1292 // diamond_false diamond_true diamond_true (duplicate)
1293 // \ / |
1294 // done --------------------|
1295 //
1296 // Duplicating diamond_true can be very costly especially if it has a
1297 // lot of instructions.
1298 return 0;
1299 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001300
Vincent Lejeune960a6222013-07-19 21:45:06 +00001301 int NumNewBlk = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +00001302
Vincent Lejeune960a6222013-07-19 21:45:06 +00001303 bool LandBlkHasOtherPred = (LandBlk->pred_size() > 2);
Tom Stellard75aadc22012-12-11 21:25:42 +00001304
Tom Stellardc5a154d2018-06-28 23:47:12 +00001305 //insert R600::ENDIF to avoid special case "input landBlk == NULL"
1306 MachineBasicBlock::iterator I = insertInstrBefore(LandBlk, R600::ENDIF);
Tom Stellard75aadc22012-12-11 21:25:42 +00001307
Vincent Lejeune960a6222013-07-19 21:45:06 +00001308 if (LandBlkHasOtherPred) {
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001309 report_fatal_error("Extra register needed to handle CFG");
Vincent Lejeune960a6222013-07-19 21:45:06 +00001310 unsigned CmpResReg =
1311 HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001312 report_fatal_error("Extra compare instruction needed to handle CFG");
Tom Stellardc5a154d2018-06-28 23:47:12 +00001313 insertCondBranchBefore(LandBlk, I, R600::IF_PREDICATE_SET,
Vincent Lejeune960a6222013-07-19 21:45:06 +00001314 CmpResReg, DebugLoc());
Tom Stellard75aadc22012-12-11 21:25:42 +00001315 }
1316
Tom Stellard69f86d12013-10-16 17:05:56 +00001317 // XXX: We are running this after RA, so creating virtual registers will
1318 // cause an assertion failure in the PostRA scheduling pass.
1319 unsigned InitReg =
1320 HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
Tom Stellardc5a154d2018-06-28 23:47:12 +00001321 insertCondBranchBefore(LandBlk, I, R600::IF_PREDICATE_SET, InitReg,
Vincent Lejeune960a6222013-07-19 21:45:06 +00001322 DebugLoc());
Tom Stellard75aadc22012-12-11 21:25:42 +00001323
Vincent Lejeune960a6222013-07-19 21:45:06 +00001324 if (MigrateTrue) {
1325 migrateInstruction(TrueMBB, LandBlk, I);
Tom Stellard75aadc22012-12-11 21:25:42 +00001326 // need to uncondionally insert the assignment to ensure a path from its
1327 // predecessor rather than headBlk has valid value in initReg if
1328 // (initVal != 1).
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001329 report_fatal_error("Extra register needed to handle CFG");
Tom Stellard75aadc22012-12-11 21:25:42 +00001330 }
Tom Stellardc5a154d2018-06-28 23:47:12 +00001331 insertInstrBefore(I, R600::ELSE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001332
Vincent Lejeune960a6222013-07-19 21:45:06 +00001333 if (MigrateFalse) {
1334 migrateInstruction(FalseMBB, LandBlk, I);
Tom Stellard75aadc22012-12-11 21:25:42 +00001335 // need to uncondionally insert the assignment to ensure a path from its
1336 // predecessor rather than headBlk has valid value in initReg if
1337 // (initVal != 0)
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001338 report_fatal_error("Extra register needed to handle CFG");
Tom Stellard75aadc22012-12-11 21:25:42 +00001339 }
1340
Vincent Lejeune960a6222013-07-19 21:45:06 +00001341 if (LandBlkHasOtherPred) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001342 // add endif
Tom Stellardc5a154d2018-06-28 23:47:12 +00001343 insertInstrBefore(I, R600::ENDIF);
Tom Stellard75aadc22012-12-11 21:25:42 +00001344
1345 // put initReg = 2 to other predecessors of landBlk
Vincent Lejeune960a6222013-07-19 21:45:06 +00001346 for (MachineBasicBlock::pred_iterator PI = LandBlk->pred_begin(),
1347 PE = LandBlk->pred_end(); PI != PE; ++PI) {
1348 MachineBasicBlock *MBB = *PI;
1349 if (MBB != TrueMBB && MBB != FalseMBB)
Matt Arsenault5de68cb2016-03-02 03:33:55 +00001350 report_fatal_error("Extra register needed to handle CFG");
Vincent Lejeune960a6222013-07-19 21:45:06 +00001351 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001352 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001353 LLVM_DEBUG(
1354 dbgs() << "result from improveSimpleJumpintoIf: ";
1355 showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0););
Tom Stellard75aadc22012-12-11 21:25:42 +00001356
1357 // update landBlk
Vincent Lejeune960a6222013-07-19 21:45:06 +00001358 *LandMBBPtr = LandBlk;
Tom Stellard75aadc22012-12-11 21:25:42 +00001359
Vincent Lejeune960a6222013-07-19 21:45:06 +00001360 return NumNewBlk;
1361}
Tom Stellard75aadc22012-12-11 21:25:42 +00001362
Vincent Lejeune960a6222013-07-19 21:45:06 +00001363void AMDGPUCFGStructurizer::mergeSerialBlock(MachineBasicBlock *DstMBB,
1364 MachineBasicBlock *SrcMBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001365 LLVM_DEBUG(dbgs() << "serialPattern BB" << DstMBB->getNumber() << " <= BB"
1366 << SrcMBB->getNumber() << "\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001367 DstMBB->splice(DstMBB->end(), SrcMBB, SrcMBB->begin(), SrcMBB->end());
Tom Stellard75aadc22012-12-11 21:25:42 +00001368
Cong Houc1069892015-12-13 09:26:17 +00001369 DstMBB->removeSuccessor(SrcMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001370 cloneSuccessorList(DstMBB, SrcMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001371
Vincent Lejeune960a6222013-07-19 21:45:06 +00001372 removeSuccessor(SrcMBB);
1373 MLI->removeBlock(SrcMBB);
1374 retireBlock(SrcMBB);
1375}
Tom Stellard75aadc22012-12-11 21:25:42 +00001376
Vincent Lejeune960a6222013-07-19 21:45:06 +00001377void AMDGPUCFGStructurizer::mergeIfthenelseBlock(MachineInstr *BranchMI,
1378 MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
1379 MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB) {
Vincent Lejeune8b8a7b52013-07-19 21:45:15 +00001380 assert (TrueMBB);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001381 LLVM_DEBUG(dbgs() << "ifPattern BB" << MBB->getNumber(); dbgs() << "{ ";
1382 if (TrueMBB) { dbgs() << "BB" << TrueMBB->getNumber(); } dbgs()
1383 << " } else ";
1384 dbgs() << "{ "; if (FalseMBB) {
1385 dbgs() << "BB" << FalseMBB->getNumber();
1386 } dbgs() << " }\n ";
1387 dbgs() << "landBlock: "; if (!LandMBB) { dbgs() << "NULL"; } else {
1388 dbgs() << "BB" << LandMBB->getNumber();
1389 } dbgs() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001390
Vincent Lejeune960a6222013-07-19 21:45:06 +00001391 int OldOpcode = BranchMI->getOpcode();
1392 DebugLoc BranchDL = BranchMI->getDebugLoc();
Tom Stellard75aadc22012-12-11 21:25:42 +00001393
1394// transform to
1395// if cond
1396// trueBlk
1397// else
1398// falseBlk
1399// endif
1400// landBlk
1401
Vincent Lejeune960a6222013-07-19 21:45:06 +00001402 MachineBasicBlock::iterator I = BranchMI;
1403 insertCondBranchBefore(I, getBranchNzeroOpcode(OldOpcode),
1404 BranchDL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001405
Vincent Lejeune960a6222013-07-19 21:45:06 +00001406 if (TrueMBB) {
1407 MBB->splice(I, TrueMBB, TrueMBB->begin(), TrueMBB->end());
Cong Houc1069892015-12-13 09:26:17 +00001408 MBB->removeSuccessor(TrueMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001409 if (LandMBB && TrueMBB->succ_size()!=0)
Cong Houc1069892015-12-13 09:26:17 +00001410 TrueMBB->removeSuccessor(LandMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001411 retireBlock(TrueMBB);
1412 MLI->removeBlock(TrueMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001413 }
1414
Vincent Lejeune960a6222013-07-19 21:45:06 +00001415 if (FalseMBB) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00001416 insertInstrBefore(I, R600::ELSE);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001417 MBB->splice(I, FalseMBB, FalseMBB->begin(),
1418 FalseMBB->end());
Cong Houc1069892015-12-13 09:26:17 +00001419 MBB->removeSuccessor(FalseMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001420 if (LandMBB && FalseMBB->succ_size() != 0)
Cong Houc1069892015-12-13 09:26:17 +00001421 FalseMBB->removeSuccessor(LandMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001422 retireBlock(FalseMBB);
1423 MLI->removeBlock(FalseMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001424 }
Tom Stellardc5a154d2018-06-28 23:47:12 +00001425 insertInstrBefore(I, R600::ENDIF);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001426
1427 BranchMI->eraseFromParent();
1428
1429 if (LandMBB && TrueMBB && FalseMBB)
1430 MBB->addSuccessor(LandMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001431}
1432
1433void AMDGPUCFGStructurizer::mergeLooplandBlock(MachineBasicBlock *DstBlk,
1434 MachineBasicBlock *LandMBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001435 LLVM_DEBUG(dbgs() << "loopPattern header = BB" << DstBlk->getNumber()
1436 << " land = BB" << LandMBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001437
Tom Stellardc5a154d2018-06-28 23:47:12 +00001438 insertInstrBefore(DstBlk, R600::WHILELOOP, DebugLoc());
1439 insertInstrEnd(DstBlk, R600::ENDLOOP, DebugLoc());
Cong Houd97c1002015-12-01 05:29:22 +00001440 DstBlk->replaceSuccessor(DstBlk, LandMBB);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001441}
Tom Stellard75aadc22012-12-11 21:25:42 +00001442
Vincent Lejeune960a6222013-07-19 21:45:06 +00001443void AMDGPUCFGStructurizer::mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
1444 MachineBasicBlock *LandMBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001445 LLVM_DEBUG(dbgs() << "loopbreakPattern exiting = BB"
1446 << ExitingMBB->getNumber() << " land = BB"
1447 << LandMBB->getNumber() << "\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001448 MachineInstr *BranchMI = getLoopendBlockBranchInstr(ExitingMBB);
1449 assert(BranchMI && isCondBranch(BranchMI));
1450 DebugLoc DL = BranchMI->getDebugLoc();
1451 MachineBasicBlock *TrueBranch = getTrueBranch(BranchMI);
1452 MachineBasicBlock::iterator I = BranchMI;
1453 if (TrueBranch != LandMBB)
Hans Wennborg0dd9ed12016-08-13 01:12:49 +00001454 reversePredicateSetter(I, *I->getParent());
Tom Stellardc5a154d2018-06-28 23:47:12 +00001455 insertCondBranchBefore(ExitingMBB, I, R600::IF_PREDICATE_SET, R600::PREDICATE_BIT, DL);
1456 insertInstrBefore(I, R600::BREAK);
1457 insertInstrBefore(I, R600::ENDIF);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001458 //now branchInst can be erase safely
1459 BranchMI->eraseFromParent();
1460 //now take care of successors, retire blocks
Cong Houc1069892015-12-13 09:26:17 +00001461 ExitingMBB->removeSuccessor(LandMBB, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001462}
Tom Stellard75aadc22012-12-11 21:25:42 +00001463
Vincent Lejeune960a6222013-07-19 21:45:06 +00001464void AMDGPUCFGStructurizer::settleLoopcontBlock(MachineBasicBlock *ContingMBB,
1465 MachineBasicBlock *ContMBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001466 LLVM_DEBUG(dbgs() << "settleLoopcontBlock conting = BB"
1467 << ContingMBB->getNumber() << ", cont = BB"
1468 << ContMBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001469
Vincent Lejeune960a6222013-07-19 21:45:06 +00001470 MachineInstr *MI = getLoopendBlockBranchInstr(ContingMBB);
1471 if (MI) {
1472 assert(isCondBranch(MI));
1473 MachineBasicBlock::iterator I = MI;
1474 MachineBasicBlock *TrueBranch = getTrueBranch(MI);
1475 int OldOpcode = MI->getOpcode();
1476 DebugLoc DL = MI->getDebugLoc();
Tom Stellard75aadc22012-12-11 21:25:42 +00001477
Vincent Lejeune960a6222013-07-19 21:45:06 +00001478 bool UseContinueLogical = ((&*ContingMBB->rbegin()) == MI);
1479
David Blaikie4eaa79c2015-03-23 20:56:44 +00001480 if (!UseContinueLogical) {
Vincent Lejeune960a6222013-07-19 21:45:06 +00001481 int BranchOpcode =
1482 TrueBranch == ContMBB ? getBranchNzeroOpcode(OldOpcode) :
1483 getBranchZeroOpcode(OldOpcode);
1484 insertCondBranchBefore(I, BranchOpcode, DL);
1485 // insertEnd to ensure phi-moves, if exist, go before the continue-instr.
Tom Stellardc5a154d2018-06-28 23:47:12 +00001486 insertInstrEnd(ContingMBB, R600::CONTINUE, DL);
1487 insertInstrEnd(ContingMBB, R600::ENDIF, DL);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001488 } else {
1489 int BranchOpcode =
1490 TrueBranch == ContMBB ? getContinueNzeroOpcode(OldOpcode) :
1491 getContinueZeroOpcode(OldOpcode);
1492 insertCondBranchBefore(I, BranchOpcode, DL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001493 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001494
1495 MI->eraseFromParent();
1496 } else {
1497 // if we've arrived here then we've already erased the branch instruction
1498 // travel back up the basic block to see the last reference of our debug
1499 // location we've just inserted that reference here so it should be
1500 // representative insertEnd to ensure phi-moves, if exist, go before the
1501 // continue-instr.
Tom Stellardc5a154d2018-06-28 23:47:12 +00001502 insertInstrEnd(ContingMBB, R600::CONTINUE,
Vincent Lejeune960a6222013-07-19 21:45:06 +00001503 getLastDebugLocInBB(ContingMBB));
Tom Stellard75aadc22012-12-11 21:25:42 +00001504 }
1505}
1506
Vincent Lejeune960a6222013-07-19 21:45:06 +00001507int AMDGPUCFGStructurizer::cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
1508 MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB) {
1509 int Cloned = 0;
1510 assert(PreMBB->isSuccessor(SrcMBB));
1511 while (SrcMBB && SrcMBB != DstMBB) {
1512 assert(SrcMBB->succ_size() == 1);
1513 if (SrcMBB->pred_size() > 1) {
1514 SrcMBB = cloneBlockForPredecessor(SrcMBB, PreMBB);
1515 ++Cloned;
Tom Stellard75aadc22012-12-11 21:25:42 +00001516 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001517
Vincent Lejeune960a6222013-07-19 21:45:06 +00001518 PreMBB = SrcMBB;
1519 SrcMBB = *SrcMBB->succ_begin();
Tom Stellard75aadc22012-12-11 21:25:42 +00001520 }
1521
Vincent Lejeune960a6222013-07-19 21:45:06 +00001522 return Cloned;
1523}
Tom Stellard75aadc22012-12-11 21:25:42 +00001524
Vincent Lejeune960a6222013-07-19 21:45:06 +00001525MachineBasicBlock *
1526AMDGPUCFGStructurizer::cloneBlockForPredecessor(MachineBasicBlock *MBB,
1527 MachineBasicBlock *PredMBB) {
1528 assert(PredMBB->isSuccessor(MBB) &&
Tom Stellard75aadc22012-12-11 21:25:42 +00001529 "succBlk is not a prececessor of curBlk");
1530
Vincent Lejeune960a6222013-07-19 21:45:06 +00001531 MachineBasicBlock *CloneMBB = clone(MBB); //clone instructions
1532 replaceInstrUseOfBlockWith(PredMBB, MBB, CloneMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001533 //srcBlk, oldBlk, newBlk
1534
Cong Houd97c1002015-12-01 05:29:22 +00001535 PredMBB->replaceSuccessor(MBB, CloneMBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001536
1537 // add all successor to cloneBlk
Vincent Lejeune960a6222013-07-19 21:45:06 +00001538 cloneSuccessorList(CloneMBB, MBB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001539
Vincent Lejeune960a6222013-07-19 21:45:06 +00001540 numClonedInstr += MBB->size();
Tom Stellard75aadc22012-12-11 21:25:42 +00001541
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001542 LLVM_DEBUG(dbgs() << "Cloned block: "
1543 << "BB" << MBB->getNumber() << "size " << MBB->size()
1544 << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001545
Vincent Lejeune960a6222013-07-19 21:45:06 +00001546 SHOWNEWBLK(CloneMBB, "result of Cloned block: ");
Tom Stellard75aadc22012-12-11 21:25:42 +00001547
Vincent Lejeune960a6222013-07-19 21:45:06 +00001548 return CloneMBB;
1549}
Tom Stellard75aadc22012-12-11 21:25:42 +00001550
Vincent Lejeune960a6222013-07-19 21:45:06 +00001551void AMDGPUCFGStructurizer::migrateInstruction(MachineBasicBlock *SrcMBB,
1552 MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I) {
1553 MachineBasicBlock::iterator SpliceEnd;
Tom Stellard75aadc22012-12-11 21:25:42 +00001554 //look for the input branchinstr, not the AMDGPU branchinstr
Vincent Lejeune960a6222013-07-19 21:45:06 +00001555 MachineInstr *BranchMI = getNormalBlockBranchInstr(SrcMBB);
1556 if (!BranchMI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001557 LLVM_DEBUG(dbgs() << "migrateInstruction don't see branch instr\n";);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001558 SpliceEnd = SrcMBB->end();
Tom Stellard75aadc22012-12-11 21:25:42 +00001559 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001560 LLVM_DEBUG(dbgs() << "migrateInstruction see branch instr: " << *BranchMI);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001561 SpliceEnd = BranchMI;
Tom Stellard75aadc22012-12-11 21:25:42 +00001562 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001563 LLVM_DEBUG(dbgs() << "migrateInstruction before splice dstSize = "
1564 << DstMBB->size() << "srcSize = " << SrcMBB->size()
1565 << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001566
1567 //splice insert before insertPos
Vincent Lejeune960a6222013-07-19 21:45:06 +00001568 DstMBB->splice(I, SrcMBB, SrcMBB->begin(), SpliceEnd);
Tom Stellard75aadc22012-12-11 21:25:42 +00001569
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001570 LLVM_DEBUG(dbgs() << "migrateInstruction after splice dstSize = "
1571 << DstMBB->size() << "srcSize = " << SrcMBB->size()
1572 << '\n';);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001573}
Tom Stellard75aadc22012-12-11 21:25:42 +00001574
Vincent Lejeune960a6222013-07-19 21:45:06 +00001575MachineBasicBlock *
1576AMDGPUCFGStructurizer::normalizeInfiniteLoopExit(MachineLoop* LoopRep) {
1577 MachineBasicBlock *LoopHeader = LoopRep->getHeader();
1578 MachineBasicBlock *LoopLatch = LoopRep->getLoopLatch();
Tom Stellard75aadc22012-12-11 21:25:42 +00001579
Vincent Lejeune960a6222013-07-19 21:45:06 +00001580 if (!LoopHeader || !LoopLatch)
Craig Topper062a2ba2014-04-25 05:30:21 +00001581 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001582 MachineInstr *BranchMI = getLoopendBlockBranchInstr(LoopLatch);
1583 // Is LoopRep an infinite loop ?
1584 if (!BranchMI || !isUncondBranch(BranchMI))
Craig Topper062a2ba2014-04-25 05:30:21 +00001585 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001586
Vincent Lejeune960a6222013-07-19 21:45:06 +00001587 MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
1588 FuncRep->push_back(DummyExitBlk); //insert to function
1589 SHOWNEWBLK(DummyExitBlk, "DummyExitBlock to normalize infiniteLoop: ");
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001590 LLVM_DEBUG(dbgs() << "Old branch instr: " << *BranchMI << "\n";);
Matthias Braunf1caa282017-12-15 22:22:58 +00001591 LLVMContext &Ctx = LoopHeader->getParent()->getFunction().getContext();
Tom Stellard1d46fb22015-07-16 15:38:29 +00001592 Ctx.emitError("Extra register needed to handle CFG");
1593 return nullptr;
Vincent Lejeune960a6222013-07-19 21:45:06 +00001594}
Tom Stellard75aadc22012-12-11 21:25:42 +00001595
Vincent Lejeune960a6222013-07-19 21:45:06 +00001596void AMDGPUCFGStructurizer::removeUnconditionalBranch(MachineBasicBlock *MBB) {
1597 MachineInstr *BranchMI;
Tom Stellard75aadc22012-12-11 21:25:42 +00001598
1599 // I saw two unconditional branch in one basic block in example
1600 // test_fc_do_while_or.c need to fix the upstream on this to remove the loop.
Vincent Lejeune960a6222013-07-19 21:45:06 +00001601 while ((BranchMI = getLoopendBlockBranchInstr(MBB))
1602 && isUncondBranch(BranchMI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001603 LLVM_DEBUG(dbgs() << "Removing uncond branch instr: " << *BranchMI);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001604 BranchMI->eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +00001605 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001606}
Tom Stellard75aadc22012-12-11 21:25:42 +00001607
Vincent Lejeune960a6222013-07-19 21:45:06 +00001608void AMDGPUCFGStructurizer::removeRedundantConditionalBranch(
1609 MachineBasicBlock *MBB) {
1610 if (MBB->succ_size() != 2)
1611 return;
1612 MachineBasicBlock *MBB1 = *MBB->succ_begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001613 MachineBasicBlock *MBB2 = *std::next(MBB->succ_begin());
Vincent Lejeune960a6222013-07-19 21:45:06 +00001614 if (MBB1 != MBB2)
1615 return;
Tom Stellard75aadc22012-12-11 21:25:42 +00001616
Vincent Lejeune960a6222013-07-19 21:45:06 +00001617 MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
1618 assert(BranchMI && isCondBranch(BranchMI));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001619 LLVM_DEBUG(dbgs() << "Removing unneeded cond branch instr: " << *BranchMI);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001620 BranchMI->eraseFromParent();
1621 SHOWNEWBLK(MBB1, "Removing redundant successor");
Cong Houc1069892015-12-13 09:26:17 +00001622 MBB->removeSuccessor(MBB1, true);
Vincent Lejeune960a6222013-07-19 21:45:06 +00001623}
Tom Stellard75aadc22012-12-11 21:25:42 +00001624
Vincent Lejeune960a6222013-07-19 21:45:06 +00001625void AMDGPUCFGStructurizer::addDummyExitBlock(
1626 SmallVectorImpl<MachineBasicBlock*> &RetMBB) {
1627 MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
1628 FuncRep->push_back(DummyExitBlk); //insert to function
Tom Stellardc5a154d2018-06-28 23:47:12 +00001629 insertInstrEnd(DummyExitBlk, R600::RETURN);
Tom Stellard75aadc22012-12-11 21:25:42 +00001630
Vincent Lejeune960a6222013-07-19 21:45:06 +00001631 for (SmallVectorImpl<MachineBasicBlock *>::iterator It = RetMBB.begin(),
1632 E = RetMBB.end(); It != E; ++It) {
1633 MachineBasicBlock *MBB = *It;
1634 MachineInstr *MI = getReturnInstr(MBB);
1635 if (MI)
1636 MI->eraseFromParent();
1637 MBB->addSuccessor(DummyExitBlk);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001638 LLVM_DEBUG(dbgs() << "Add dummyExitBlock to BB" << MBB->getNumber()
1639 << " successors\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001640 }
Vincent Lejeune960a6222013-07-19 21:45:06 +00001641 SHOWNEWBLK(DummyExitBlk, "DummyExitBlock: ");
Tom Stellard75aadc22012-12-11 21:25:42 +00001642}
1643
Vincent Lejeune960a6222013-07-19 21:45:06 +00001644void AMDGPUCFGStructurizer::removeSuccessor(MachineBasicBlock *MBB) {
1645 while (MBB->succ_size())
1646 MBB->removeSuccessor(*MBB->succ_begin());
Tom Stellard75aadc22012-12-11 21:25:42 +00001647}
1648
Vincent Lejeune960a6222013-07-19 21:45:06 +00001649void AMDGPUCFGStructurizer::recordSccnum(MachineBasicBlock *MBB,
1650 int SccNum) {
1651 BlockInformation *&srcBlkInfo = BlockInfoMap[MBB];
1652 if (!srcBlkInfo)
1653 srcBlkInfo = new BlockInformation();
1654 srcBlkInfo->SccNum = SccNum;
Tom Stellard75aadc22012-12-11 21:25:42 +00001655}
1656
Vincent Lejeune960a6222013-07-19 21:45:06 +00001657void AMDGPUCFGStructurizer::retireBlock(MachineBasicBlock *MBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001658 LLVM_DEBUG(dbgs() << "Retiring BB" << MBB->getNumber() << "\n";);
Tom Stellard75aadc22012-12-11 21:25:42 +00001659
Vincent Lejeune960a6222013-07-19 21:45:06 +00001660 BlockInformation *&SrcBlkInfo = BlockInfoMap[MBB];
Tom Stellard75aadc22012-12-11 21:25:42 +00001661
Vincent Lejeune960a6222013-07-19 21:45:06 +00001662 if (!SrcBlkInfo)
1663 SrcBlkInfo = new BlockInformation();
Tom Stellard75aadc22012-12-11 21:25:42 +00001664
Vincent Lejeune960a6222013-07-19 21:45:06 +00001665 SrcBlkInfo->IsRetired = true;
1666 assert(MBB->succ_size() == 0 && MBB->pred_size() == 0
Tom Stellard75aadc22012-12-11 21:25:42 +00001667 && "can't retire block yet");
1668}
1669
Tom Stellardf2ba9722013-12-11 17:51:47 +00001670INITIALIZE_PASS_BEGIN(AMDGPUCFGStructurizer, "amdgpustructurizer",
1671 "AMDGPU CFG Structurizer", false, false)
1672INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
1673INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
1674INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
1675INITIALIZE_PASS_END(AMDGPUCFGStructurizer, "amdgpustructurizer",
1676 "AMDGPU CFG Structurizer", false, false)
1677
1678FunctionPass *llvm::createAMDGPUCFGStructurizerPass() {
1679 return new AMDGPUCFGStructurizer();
Tom Stellard75aadc22012-12-11 21:25:42 +00001680}