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