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