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