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