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