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