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