Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 1 | //===-- EarlyIfConversion.cpp - If-conversion on SSA form machine code ----===// |
| 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 |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Early if-conversion is for out-of-order CPUs that don't have a lot of |
| 10 | // predicable instructions. The goal is to eliminate conditional branches that |
| 11 | // may mispredict. |
| 12 | // |
| 13 | // Instructions from both sides of the branch are executed specutatively, and a |
| 14 | // cmov instruction selects the result. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/BitVector.h" |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/PostOrderIterator.h" |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SetVector.h" |
| 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | #include "llvm/ADT/SparseSet.h" |
Jakob Stoklund Olesen | d0af1d9 | 2012-08-13 21:03:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 965665b | 2013-01-17 01:06:04 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineTraceMetrics.h" |
| 32 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 35 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 36 | #include "llvm/InitializePasses.h" |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
| 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "early-ifcvt" |
| 44 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 45 | // Absolute maximum number of instructions allowed per speculated block. |
| 46 | // This bypasses all other heuristics, so it should be set fairly high. |
| 47 | static cl::opt<unsigned> |
| 48 | BlockInstrLimit("early-ifcvt-limit", cl::init(30), cl::Hidden, |
| 49 | cl::desc("Maximum number of instructions per speculated block.")); |
| 50 | |
| 51 | // Stress testing mode - disable heuristics. |
| 52 | static cl::opt<bool> Stress("stress-early-ifcvt", cl::Hidden, |
| 53 | cl::desc("Turn all knobs to 11")); |
| 54 | |
Jakob Stoklund Olesen | d0af1d9 | 2012-08-13 21:03:27 +0000 | [diff] [blame] | 55 | STATISTIC(NumDiamondsSeen, "Number of diamonds"); |
| 56 | STATISTIC(NumDiamondsConv, "Number of diamonds converted"); |
| 57 | STATISTIC(NumTrianglesSeen, "Number of triangles"); |
| 58 | STATISTIC(NumTrianglesConv, "Number of triangles converted"); |
| 59 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | // SSAIfConv |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // |
| 64 | // The SSAIfConv class performs if-conversion on SSA form machine code after |
Matt Beaumont-Gay | 11d08b2 | 2012-07-04 01:09:45 +0000 | [diff] [blame] | 65 | // determining if it is possible. The class contains no heuristics; external |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 66 | // code should be used to determine when if-conversion is a good idea. |
| 67 | // |
Matt Beaumont-Gay | 11d08b2 | 2012-07-04 01:09:45 +0000 | [diff] [blame] | 68 | // SSAIfConv can convert both triangles and diamonds: |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 69 | // |
| 70 | // Triangle: Head Diamond: Head |
Matt Beaumont-Gay | 11d08b2 | 2012-07-04 01:09:45 +0000 | [diff] [blame] | 71 | // | \ / \_ |
| 72 | // | \ / | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 73 | // | [TF]BB FBB TBB |
| 74 | // | / \ / |
| 75 | // | / \ / |
| 76 | // Tail Tail |
| 77 | // |
| 78 | // Instructions in the conditional blocks TBB and/or FBB are spliced into the |
Matt Beaumont-Gay | 11d08b2 | 2012-07-04 01:09:45 +0000 | [diff] [blame] | 79 | // Head block, and phis in the Tail block are converted to select instructions. |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 80 | // |
| 81 | namespace { |
| 82 | class SSAIfConv { |
| 83 | const TargetInstrInfo *TII; |
| 84 | const TargetRegisterInfo *TRI; |
| 85 | MachineRegisterInfo *MRI; |
| 86 | |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 87 | public: |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 88 | /// The block containing the conditional branch. |
| 89 | MachineBasicBlock *Head; |
| 90 | |
| 91 | /// The block containing phis after the if-then-else. |
| 92 | MachineBasicBlock *Tail; |
| 93 | |
Krzysztof Parzyszek | 020041d | 2020-01-21 09:47:35 -0600 | [diff] [blame] | 94 | /// The 'true' conditional block as determined by analyzeBranch. |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 95 | MachineBasicBlock *TBB; |
| 96 | |
Krzysztof Parzyszek | 020041d | 2020-01-21 09:47:35 -0600 | [diff] [blame] | 97 | /// The 'false' conditional block as determined by analyzeBranch. |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 98 | MachineBasicBlock *FBB; |
| 99 | |
| 100 | /// isTriangle - When there is no 'else' block, either TBB or FBB will be |
| 101 | /// equal to Tail. |
| 102 | bool isTriangle() const { return TBB == Tail || FBB == Tail; } |
| 103 | |
Jakob Stoklund Olesen | 0a99062 | 2012-08-10 20:19:17 +0000 | [diff] [blame] | 104 | /// Returns the Tail predecessor for the True side. |
| 105 | MachineBasicBlock *getTPred() const { return TBB == Tail ? Head : TBB; } |
| 106 | |
| 107 | /// Returns the Tail predecessor for the False side. |
| 108 | MachineBasicBlock *getFPred() const { return FBB == Tail ? Head : FBB; } |
| 109 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 110 | /// Information about each phi in the Tail block. |
| 111 | struct PHIInfo { |
| 112 | MachineInstr *PHI; |
| 113 | unsigned TReg, FReg; |
| 114 | // Latencies from Cond+Branch, TReg, and FReg to DstReg. |
| 115 | int CondCycles, TCycles, FCycles; |
| 116 | |
| 117 | PHIInfo(MachineInstr *phi) |
| 118 | : PHI(phi), TReg(0), FReg(0), CondCycles(0), TCycles(0), FCycles(0) {} |
| 119 | }; |
| 120 | |
| 121 | SmallVector<PHIInfo, 8> PHIs; |
| 122 | |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 123 | private: |
Krzysztof Parzyszek | 020041d | 2020-01-21 09:47:35 -0600 | [diff] [blame] | 124 | /// The branch condition determined by analyzeBranch. |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 125 | SmallVector<MachineOperand, 4> Cond; |
| 126 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 127 | /// Instructions in Head that define values used by the conditional blocks. |
| 128 | /// The hoisted instructions must be inserted after these instructions. |
| 129 | SmallPtrSet<MachineInstr*, 8> InsertAfter; |
| 130 | |
| 131 | /// Register units clobbered by the conditional blocks. |
| 132 | BitVector ClobberedRegUnits; |
| 133 | |
| 134 | // Scratch pad for findInsertionPoint. |
| 135 | SparseSet<unsigned> LiveRegUnits; |
| 136 | |
| 137 | /// Insertion point in Head for speculatively executed instructions form TBB |
| 138 | /// and FBB. |
| 139 | MachineBasicBlock::iterator InsertionPoint; |
| 140 | |
| 141 | /// Return true if all non-terminator instructions in MBB can be safely |
| 142 | /// speculated. |
| 143 | bool canSpeculateInstrs(MachineBasicBlock *MBB); |
| 144 | |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 145 | /// Return true if all non-terminator instructions in MBB can be safely |
| 146 | /// predicated. |
| 147 | bool canPredicateInstrs(MachineBasicBlock *MBB); |
| 148 | |
| 149 | /// Scan through instruction dependencies and update InsertAfter array. |
| 150 | /// Return false if any dependency is incompatible with if conversion. |
| 151 | bool InstrDependenciesAllowIfConv(MachineInstr *I); |
| 152 | |
| 153 | /// Predicate all instructions of the basic block with current condition |
| 154 | /// except for terminators. Reverse the condition if ReversePredicate is set. |
| 155 | void PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate); |
| 156 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 157 | /// Find a valid insertion point in Head. |
| 158 | bool findInsertionPoint(); |
| 159 | |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 160 | /// Replace PHI instructions in Tail with selects. |
| 161 | void replacePHIInstrs(); |
| 162 | |
| 163 | /// Insert selects and rewrite PHI operands to use them. |
| 164 | void rewritePHIOperands(); |
| 165 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 166 | public: |
| 167 | /// runOnMachineFunction - Initialize per-function data structures. |
| 168 | void runOnMachineFunction(MachineFunction &MF) { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 169 | TII = MF.getSubtarget().getInstrInfo(); |
| 170 | TRI = MF.getSubtarget().getRegisterInfo(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 171 | MRI = &MF.getRegInfo(); |
| 172 | LiveRegUnits.clear(); |
| 173 | LiveRegUnits.setUniverse(TRI->getNumRegUnits()); |
| 174 | ClobberedRegUnits.clear(); |
| 175 | ClobberedRegUnits.resize(TRI->getNumRegUnits()); |
| 176 | } |
| 177 | |
| 178 | /// canConvertIf - If the sub-CFG headed by MBB can be if-converted, |
| 179 | /// initialize the internal state, and return true. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 180 | /// If predicate is set try to predicate the block otherwise try to |
| 181 | /// speculatively execute it. |
| 182 | bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 183 | |
| 184 | /// convertIf - If-convert the last block passed to canConvertIf(), assuming |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 185 | /// it is possible. Add any erased blocks to RemovedBlocks. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 186 | void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks, |
| 187 | bool Predicate = false); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 188 | }; |
| 189 | } // end anonymous namespace |
| 190 | |
| 191 | |
| 192 | /// canSpeculateInstrs - Returns true if all the instructions in MBB can safely |
| 193 | /// be speculated. The terminators are not considered. |
| 194 | /// |
| 195 | /// If instructions use any values that are defined in the head basic block, |
| 196 | /// the defining instructions are added to InsertAfter. |
| 197 | /// |
| 198 | /// Any clobbered regunits are added to ClobberedRegUnits. |
| 199 | /// |
| 200 | bool SSAIfConv::canSpeculateInstrs(MachineBasicBlock *MBB) { |
| 201 | // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to |
| 202 | // get right. |
| 203 | if (!MBB->livein_empty()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 204 | LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
| 208 | unsigned InstrCount = 0; |
Jakob Stoklund Olesen | 3f1bb93 | 2012-07-06 02:31:22 +0000 | [diff] [blame] | 209 | |
| 210 | // Check all instructions, except the terminators. It is assumed that |
| 211 | // terminators never have side effects or define any used register values. |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 212 | for (MachineBasicBlock::iterator I = MBB->begin(), |
| 213 | E = MBB->getFirstTerminator(); I != E; ++I) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 214 | if (I->isDebugInstr()) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 215 | continue; |
| 216 | |
| 217 | if (++InstrCount > BlockInstrLimit && !Stress) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 218 | LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than " |
| 219 | << BlockInstrLimit << " instructions.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // There shouldn't normally be any phis in a single-predecessor block. |
| 224 | if (I->isPHI()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 225 | LLVM_DEBUG(dbgs() << "Can't hoist: " << *I); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | // Don't speculate loads. Note that it may be possible and desirable to |
| 230 | // speculate GOT or constant pool loads that are guaranteed not to trap, |
| 231 | // but we don't support that for now. |
| 232 | if (I->mayLoad()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 233 | LLVM_DEBUG(dbgs() << "Won't speculate load: " << *I); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | |
| 237 | // We never speculate stores, so an AA pointer isn't necessary. |
| 238 | bool DontMoveAcrossStore = true; |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 239 | if (!I->isSafeToMove(nullptr, DontMoveAcrossStore)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 240 | LLVM_DEBUG(dbgs() << "Can't speculate: " << *I); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
| 244 | // Check for any dependencies on Head instructions. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 245 | if (!InstrDependenciesAllowIfConv(&(*I))) |
| 246 | return false; |
| 247 | } |
| 248 | return true; |
| 249 | } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 250 | |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 251 | /// Check that there is no dependencies preventing if conversion. |
| 252 | /// |
| 253 | /// If instruction uses any values that are defined in the head basic block, |
| 254 | /// the defining instructions are added to InsertAfter. |
| 255 | bool SSAIfConv::InstrDependenciesAllowIfConv(MachineInstr *I) { |
| 256 | for (const MachineOperand &MO : I->operands()) { |
| 257 | if (MO.isRegMask()) { |
| 258 | LLVM_DEBUG(dbgs() << "Won't speculate regmask: " << *I); |
| 259 | return false; |
| 260 | } |
| 261 | if (!MO.isReg()) |
| 262 | continue; |
| 263 | Register Reg = MO.getReg(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 264 | |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 265 | // Remember clobbered regunits. |
| 266 | if (MO.isDef() && Register::isPhysicalRegister(Reg)) |
| 267 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) |
| 268 | ClobberedRegUnits.set(*Units); |
| 269 | |
| 270 | if (!MO.readsReg() || !Register::isVirtualRegister(Reg)) |
| 271 | continue; |
| 272 | MachineInstr *DefMI = MRI->getVRegDef(Reg); |
| 273 | if (!DefMI || DefMI->getParent() != Head) |
| 274 | continue; |
| 275 | if (InsertAfter.insert(DefMI).second) |
| 276 | LLVM_DEBUG(dbgs() << printMBBReference(*I->getParent()) << " depends on " |
| 277 | << *DefMI); |
| 278 | if (DefMI->isTerminator()) { |
| 279 | LLVM_DEBUG(dbgs() << "Can't insert instructions below terminator.\n"); |
| 280 | return false; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 | |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 286 | /// canPredicateInstrs - Returns true if all the instructions in MBB can safely |
| 287 | /// be predicates. The terminators are not considered. |
| 288 | /// |
| 289 | /// If instructions use any values that are defined in the head basic block, |
| 290 | /// the defining instructions are added to InsertAfter. |
| 291 | /// |
| 292 | /// Any clobbered regunits are added to ClobberedRegUnits. |
| 293 | /// |
| 294 | bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) { |
| 295 | // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to |
| 296 | // get right. |
| 297 | if (!MBB->livein_empty()) { |
| 298 | LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n"); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | unsigned InstrCount = 0; |
| 303 | |
| 304 | // Check all instructions, except the terminators. It is assumed that |
| 305 | // terminators never have side effects or define any used register values. |
| 306 | for (MachineBasicBlock::iterator I = MBB->begin(), |
| 307 | E = MBB->getFirstTerminator(); |
| 308 | I != E; ++I) { |
| 309 | if (I->isDebugInstr()) |
| 310 | continue; |
| 311 | |
| 312 | if (++InstrCount > BlockInstrLimit && !Stress) { |
| 313 | LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than " |
| 314 | << BlockInstrLimit << " instructions.\n"); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | // There shouldn't normally be any phis in a single-predecessor block. |
| 319 | if (I->isPHI()) { |
| 320 | LLVM_DEBUG(dbgs() << "Can't predicate: " << *I); |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | // Check that instruction is predicable and that it is not already |
| 325 | // predicated. |
| 326 | if (!TII->isPredicable(*I) || TII->isPredicated(*I)) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Check for any dependencies on Head instructions. |
| 331 | if (!InstrDependenciesAllowIfConv(&(*I))) |
| 332 | return false; |
| 333 | } |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | // Apply predicate to all instructions in the machine block. |
| 338 | void SSAIfConv::PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate) { |
| 339 | auto Condition = Cond; |
| 340 | if (ReversePredicate) |
| 341 | TII->reverseBranchCondition(Condition); |
| 342 | // Terminators don't need to be predicated as they will be removed. |
| 343 | for (MachineBasicBlock::iterator I = MBB->begin(), |
| 344 | E = MBB->getFirstTerminator(); |
| 345 | I != E; ++I) { |
| 346 | if (I->isDebugInstr()) |
| 347 | continue; |
| 348 | TII->PredicateInstruction(*I, Condition); |
| 349 | } |
| 350 | } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 351 | |
| 352 | /// Find an insertion point in Head for the speculated instructions. The |
| 353 | /// insertion point must be: |
| 354 | /// |
| 355 | /// 1. Before any terminators. |
| 356 | /// 2. After any instructions in InsertAfter. |
| 357 | /// 3. Not have any clobbered regunits live. |
| 358 | /// |
| 359 | /// This function sets InsertionPoint and returns true when successful, it |
| 360 | /// returns false if no valid insertion point could be found. |
| 361 | /// |
| 362 | bool SSAIfConv::findInsertionPoint() { |
| 363 | // Keep track of live regunits before the current position. |
| 364 | // Only track RegUnits that are also in ClobberedRegUnits. |
| 365 | LiveRegUnits.clear(); |
| 366 | SmallVector<unsigned, 8> Reads; |
| 367 | MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator(); |
| 368 | MachineBasicBlock::iterator I = Head->end(); |
| 369 | MachineBasicBlock::iterator B = Head->begin(); |
| 370 | while (I != B) { |
| 371 | --I; |
| 372 | // Some of the conditional code depends in I. |
Duncan P. N. Exon Smith | 395bd9c | 2016-02-22 02:53:42 +0000 | [diff] [blame] | 373 | if (InsertAfter.count(&*I)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 374 | LLVM_DEBUG(dbgs() << "Can't insert code after " << *I); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
| 378 | // Update live regunits. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 379 | for (const MachineOperand &MO : I->operands()) { |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 380 | // We're ignoring regmask operands. That is conservatively correct. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 381 | if (!MO.isReg()) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 382 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 383 | Register Reg = MO.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 384 | if (!Register::isPhysicalRegister(Reg)) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 385 | continue; |
| 386 | // I clobbers Reg, so it isn't live before I. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 387 | if (MO.isDef()) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 388 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) |
| 389 | LiveRegUnits.erase(*Units); |
| 390 | // Unless I reads Reg. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 391 | if (MO.readsReg()) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 392 | Reads.push_back(Reg); |
| 393 | } |
| 394 | // Anything read by I is live before I. |
| 395 | while (!Reads.empty()) |
| 396 | for (MCRegUnitIterator Units(Reads.pop_back_val(), TRI); Units.isValid(); |
| 397 | ++Units) |
| 398 | if (ClobberedRegUnits.test(*Units)) |
| 399 | LiveRegUnits.insert(*Units); |
| 400 | |
| 401 | // We can't insert before a terminator. |
| 402 | if (I != FirstTerm && I->isTerminator()) |
| 403 | continue; |
| 404 | |
| 405 | // Some of the clobbered registers are live before I, not a valid insertion |
| 406 | // point. |
| 407 | if (!LiveRegUnits.empty()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 408 | LLVM_DEBUG({ |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 409 | dbgs() << "Would clobber"; |
| 410 | for (SparseSet<unsigned>::const_iterator |
| 411 | i = LiveRegUnits.begin(), e = LiveRegUnits.end(); i != e; ++i) |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 412 | dbgs() << ' ' << printRegUnit(*i, TRI); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 413 | dbgs() << " live before " << *I; |
| 414 | }); |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | // This is a valid insertion point. |
| 419 | InsertionPoint = I; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 420 | LLVM_DEBUG(dbgs() << "Can insert before " << *I); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 421 | return true; |
| 422 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 423 | LLVM_DEBUG(dbgs() << "No legal insertion point found.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 424 | return false; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | |
| 429 | /// canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is |
| 430 | /// a potential candidate for if-conversion. Fill out the internal state. |
| 431 | /// |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 432 | bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) { |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 433 | Head = MBB; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 434 | TBB = FBB = Tail = nullptr; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 435 | |
| 436 | if (Head->succ_size() != 2) |
| 437 | return false; |
| 438 | MachineBasicBlock *Succ0 = Head->succ_begin()[0]; |
| 439 | MachineBasicBlock *Succ1 = Head->succ_begin()[1]; |
| 440 | |
| 441 | // Canonicalize so Succ0 has MBB as its single predecessor. |
| 442 | if (Succ0->pred_size() != 1) |
| 443 | std::swap(Succ0, Succ1); |
| 444 | |
| 445 | if (Succ0->pred_size() != 1 || Succ0->succ_size() != 1) |
| 446 | return false; |
| 447 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 448 | Tail = Succ0->succ_begin()[0]; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 449 | |
| 450 | // This is not a triangle. |
| 451 | if (Tail != Succ1) { |
| 452 | // Check for a diamond. We won't deal with any critical edges. |
| 453 | if (Succ1->pred_size() != 1 || Succ1->succ_size() != 1 || |
| 454 | Succ1->succ_begin()[0] != Tail) |
| 455 | return false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 456 | LLVM_DEBUG(dbgs() << "\nDiamond: " << printMBBReference(*Head) << " -> " |
| 457 | << printMBBReference(*Succ0) << "/" |
| 458 | << printMBBReference(*Succ1) << " -> " |
| 459 | << printMBBReference(*Tail) << '\n'); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 460 | |
| 461 | // Live-in physregs are tricky to get right when speculating code. |
| 462 | if (!Tail->livein_empty()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 463 | LLVM_DEBUG(dbgs() << "Tail has live-ins.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 467 | LLVM_DEBUG(dbgs() << "\nTriangle: " << printMBBReference(*Head) << " -> " |
| 468 | << printMBBReference(*Succ0) << " -> " |
| 469 | << printMBBReference(*Tail) << '\n'); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // This is a triangle or a diamond. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 473 | // Skip if we cannot predicate and there are no phis skip as there must be |
| 474 | // side effects that can only be handled with predication. |
| 475 | if (!Predicate && (Tail->empty() || !Tail->front().isPHI())) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 476 | LLVM_DEBUG(dbgs() << "No phis in tail.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 477 | return false; |
| 478 | } |
| 479 | |
| 480 | // The branch we're looking to eliminate must be analyzable. |
| 481 | Cond.clear(); |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 482 | if (TII->analyzeBranch(*Head, TBB, FBB, Cond)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 483 | LLVM_DEBUG(dbgs() << "Branch not analyzable.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | |
| 487 | // This is weird, probably some sort of degenerate CFG. |
| 488 | if (!TBB) { |
Krzysztof Parzyszek | 020041d | 2020-01-21 09:47:35 -0600 | [diff] [blame] | 489 | LLVM_DEBUG(dbgs() << "analyzeBranch didn't find conditional branch.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | |
Eli Friedman | 295e346 | 2019-01-15 00:19:46 +0000 | [diff] [blame] | 493 | // Make sure the analyzed branch is conditional; one of the successors |
| 494 | // could be a landing pad. (Empty landing pads can be generated on Windows.) |
| 495 | if (Cond.empty()) { |
Krzysztof Parzyszek | 020041d | 2020-01-21 09:47:35 -0600 | [diff] [blame] | 496 | LLVM_DEBUG(dbgs() << "analyzeBranch found an unconditional branch.\n"); |
Eli Friedman | 295e346 | 2019-01-15 00:19:46 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | |
Krzysztof Parzyszek | 020041d | 2020-01-21 09:47:35 -0600 | [diff] [blame] | 500 | // analyzeBranch doesn't set FBB on a fall-through branch. |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 501 | // Make sure it is always set. |
| 502 | FBB = TBB == Succ0 ? Succ1 : Succ0; |
| 503 | |
| 504 | // Any phis in the tail block must be convertible to selects. |
| 505 | PHIs.clear(); |
Jakob Stoklund Olesen | 0a99062 | 2012-08-10 20:19:17 +0000 | [diff] [blame] | 506 | MachineBasicBlock *TPred = getTPred(); |
| 507 | MachineBasicBlock *FPred = getFPred(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 508 | for (MachineBasicBlock::iterator I = Tail->begin(), E = Tail->end(); |
| 509 | I != E && I->isPHI(); ++I) { |
| 510 | PHIs.push_back(&*I); |
| 511 | PHIInfo &PI = PHIs.back(); |
| 512 | // Find PHI operands corresponding to TPred and FPred. |
| 513 | for (unsigned i = 1; i != PI.PHI->getNumOperands(); i += 2) { |
| 514 | if (PI.PHI->getOperand(i+1).getMBB() == TPred) |
| 515 | PI.TReg = PI.PHI->getOperand(i).getReg(); |
| 516 | if (PI.PHI->getOperand(i+1).getMBB() == FPred) |
| 517 | PI.FReg = PI.PHI->getOperand(i).getReg(); |
| 518 | } |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 519 | assert(Register::isVirtualRegister(PI.TReg) && "Bad PHI"); |
| 520 | assert(Register::isVirtualRegister(PI.FReg) && "Bad PHI"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 521 | |
| 522 | // Get target information. |
Amara Emerson | 67a8775 | 2020-01-17 14:34:26 -0800 | [diff] [blame] | 523 | if (!TII->canInsertSelect(*Head, Cond, PI.PHI->getOperand(0).getReg(), |
| 524 | PI.TReg, PI.FReg, PI.CondCycles, PI.TCycles, |
| 525 | PI.FCycles)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 526 | LLVM_DEBUG(dbgs() << "Can't convert: " << *PI.PHI); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 527 | return false; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // Check that the conditional instructions can be speculated. |
| 532 | InsertAfter.clear(); |
| 533 | ClobberedRegUnits.reset(); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 534 | if (Predicate) { |
| 535 | if (TBB != Tail && !canPredicateInstrs(TBB)) |
| 536 | return false; |
| 537 | if (FBB != Tail && !canPredicateInstrs(FBB)) |
| 538 | return false; |
| 539 | } else { |
| 540 | if (TBB != Tail && !canSpeculateInstrs(TBB)) |
| 541 | return false; |
| 542 | if (FBB != Tail && !canSpeculateInstrs(FBB)) |
| 543 | return false; |
| 544 | } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 545 | |
| 546 | // Try to find a valid insertion point for the speculated instructions in the |
| 547 | // head basic block. |
| 548 | if (!findInsertionPoint()) |
| 549 | return false; |
| 550 | |
Jakob Stoklund Olesen | d0af1d9 | 2012-08-13 21:03:27 +0000 | [diff] [blame] | 551 | if (isTriangle()) |
| 552 | ++NumTrianglesSeen; |
| 553 | else |
| 554 | ++NumDiamondsSeen; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 555 | return true; |
| 556 | } |
| 557 | |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 558 | /// replacePHIInstrs - Completely replace PHI instructions with selects. |
| 559 | /// This is possible when the only Tail predecessors are the if-converted |
| 560 | /// blocks. |
| 561 | void SSAIfConv::replacePHIInstrs() { |
| 562 | assert(Tail->pred_size() == 2 && "Cannot replace PHIs"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 563 | MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator(); |
| 564 | assert(FirstTerm != Head->end() && "No terminators"); |
| 565 | DebugLoc HeadDL = FirstTerm->getDebugLoc(); |
| 566 | |
| 567 | // Convert all PHIs to select instructions inserted before FirstTerm. |
| 568 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) { |
| 569 | PHIInfo &PI = PHIs[i]; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 570 | LLVM_DEBUG(dbgs() << "If-converting " << *PI.PHI); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 571 | Register DstReg = PI.PHI->getOperand(0).getReg(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 572 | TII->insertSelect(*Head, FirstTerm, HeadDL, DstReg, Cond, PI.TReg, PI.FReg); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 573 | LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm)); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 574 | PI.PHI->eraseFromParent(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 575 | PI.PHI = nullptr; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 576 | } |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /// rewritePHIOperands - When there are additional Tail predecessors, insert |
| 580 | /// select instructions in Head and rewrite PHI operands to use the selects. |
| 581 | /// Keep the PHI instructions in Tail to handle the other predecessors. |
| 582 | void SSAIfConv::rewritePHIOperands() { |
| 583 | MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator(); |
| 584 | assert(FirstTerm != Head->end() && "No terminators"); |
| 585 | DebugLoc HeadDL = FirstTerm->getDebugLoc(); |
| 586 | |
| 587 | // Convert all PHIs to select instructions inserted before FirstTerm. |
| 588 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) { |
| 589 | PHIInfo &PI = PHIs[i]; |
Yi Jiang | e0b3499 | 2015-06-18 22:34:09 +0000 | [diff] [blame] | 590 | unsigned DstReg = 0; |
Junmo Park | 67bb3f1 | 2016-01-29 01:39:39 +0000 | [diff] [blame] | 591 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 592 | LLVM_DEBUG(dbgs() << "If-converting " << *PI.PHI); |
Yi Jiang | e0b3499 | 2015-06-18 22:34:09 +0000 | [diff] [blame] | 593 | if (PI.TReg == PI.FReg) { |
| 594 | // We do not need the select instruction if both incoming values are |
| 595 | // equal. |
| 596 | DstReg = PI.TReg; |
| 597 | } else { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 598 | Register PHIDst = PI.PHI->getOperand(0).getReg(); |
Yi Jiang | e0b3499 | 2015-06-18 22:34:09 +0000 | [diff] [blame] | 599 | DstReg = MRI->createVirtualRegister(MRI->getRegClass(PHIDst)); |
| 600 | TII->insertSelect(*Head, FirstTerm, HeadDL, |
| 601 | DstReg, Cond, PI.TReg, PI.FReg); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 602 | LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm)); |
Yi Jiang | e0b3499 | 2015-06-18 22:34:09 +0000 | [diff] [blame] | 603 | } |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 604 | |
| 605 | // Rewrite PHI operands TPred -> (DstReg, Head), remove FPred. |
| 606 | for (unsigned i = PI.PHI->getNumOperands(); i != 1; i -= 2) { |
| 607 | MachineBasicBlock *MBB = PI.PHI->getOperand(i-1).getMBB(); |
| 608 | if (MBB == getTPred()) { |
| 609 | PI.PHI->getOperand(i-1).setMBB(Head); |
| 610 | PI.PHI->getOperand(i-2).setReg(DstReg); |
| 611 | } else if (MBB == getFPred()) { |
| 612 | PI.PHI->RemoveOperand(i-1); |
| 613 | PI.PHI->RemoveOperand(i-2); |
| 614 | } |
| 615 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 616 | LLVM_DEBUG(dbgs() << " --> " << *PI.PHI); |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | /// convertIf - Execute the if conversion after canConvertIf has determined the |
| 621 | /// feasibility. |
| 622 | /// |
| 623 | /// Any basic blocks erased will be added to RemovedBlocks. |
| 624 | /// |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 625 | void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks, |
| 626 | bool Predicate) { |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 627 | assert(Head && Tail && TBB && FBB && "Call canConvertIf first."); |
| 628 | |
Jakob Stoklund Olesen | d0af1d9 | 2012-08-13 21:03:27 +0000 | [diff] [blame] | 629 | // Update statistics. |
| 630 | if (isTriangle()) |
| 631 | ++NumTrianglesConv; |
| 632 | else |
| 633 | ++NumDiamondsConv; |
| 634 | |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 635 | // Move all instructions into Head, except for the terminators. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 636 | if (TBB != Tail) { |
| 637 | if (Predicate) |
| 638 | PredicateBlock(TBB, /*ReversePredicate=*/false); |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 639 | Head->splice(InsertionPoint, TBB, TBB->begin(), TBB->getFirstTerminator()); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 640 | } |
| 641 | if (FBB != Tail) { |
| 642 | if (Predicate) |
| 643 | PredicateBlock(FBB, /*ReversePredicate=*/true); |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 644 | Head->splice(InsertionPoint, FBB, FBB->begin(), FBB->getFirstTerminator()); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 645 | } |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 646 | // Are there extra Tail predecessors? |
| 647 | bool ExtraPreds = Tail->pred_size() != 2; |
| 648 | if (ExtraPreds) |
| 649 | rewritePHIOperands(); |
| 650 | else |
| 651 | replacePHIInstrs(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 652 | |
| 653 | // Fix up the CFG, temporarily leave Head without any successors. |
| 654 | Head->removeSuccessor(TBB); |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 655 | Head->removeSuccessor(FBB, true); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 656 | if (TBB != Tail) |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 657 | TBB->removeSuccessor(Tail, true); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 658 | if (FBB != Tail) |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 659 | FBB->removeSuccessor(Tail, true); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 660 | |
| 661 | // Fix up Head's terminators. |
| 662 | // It should become a single branch or a fallthrough. |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 663 | DebugLoc HeadDL = Head->getFirstTerminator()->getDebugLoc(); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 664 | TII->removeBranch(*Head); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 665 | |
| 666 | // Erase the now empty conditional blocks. It is likely that Head can fall |
| 667 | // through to Tail, and we can join the two blocks. |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 668 | if (TBB != Tail) { |
| 669 | RemovedBlocks.push_back(TBB); |
| 670 | TBB->eraseFromParent(); |
| 671 | } |
| 672 | if (FBB != Tail) { |
| 673 | RemovedBlocks.push_back(FBB); |
| 674 | FBB->eraseFromParent(); |
| 675 | } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 676 | |
| 677 | assert(Head->succ_empty() && "Additional head successors?"); |
Jakob Stoklund Olesen | 83a927d | 2012-08-13 20:49:04 +0000 | [diff] [blame] | 678 | if (!ExtraPreds && Head->isLayoutSuccessor(Tail)) { |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 679 | // Splice Tail onto the end of Head. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 680 | LLVM_DEBUG(dbgs() << "Joining tail " << printMBBReference(*Tail) |
| 681 | << " into head " << printMBBReference(*Head) << '\n'); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 682 | Head->splice(Head->end(), Tail, |
| 683 | Tail->begin(), Tail->end()); |
| 684 | Head->transferSuccessorsAndUpdatePHIs(Tail); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 685 | RemovedBlocks.push_back(Tail); |
| 686 | Tail->eraseFromParent(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 687 | } else { |
| 688 | // We need a branch to Tail, let code placement work it out later. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 689 | LLVM_DEBUG(dbgs() << "Converting to unconditional branch.\n"); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 690 | SmallVector<MachineOperand, 0> EmptyCond; |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 691 | TII->insertBranch(*Head, Tail, nullptr, EmptyCond, HeadDL); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 692 | Head->addSuccessor(Tail); |
| 693 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 694 | LLVM_DEBUG(dbgs() << *Head); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 697 | //===----------------------------------------------------------------------===// |
| 698 | // EarlyIfConverter Pass |
| 699 | //===----------------------------------------------------------------------===// |
| 700 | |
| 701 | namespace { |
| 702 | class EarlyIfConverter : public MachineFunctionPass { |
| 703 | const TargetInstrInfo *TII; |
| 704 | const TargetRegisterInfo *TRI; |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 705 | MCSchedModel SchedModel; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 706 | MachineRegisterInfo *MRI; |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 707 | MachineDominatorTree *DomTree; |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 708 | MachineLoopInfo *Loops; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 709 | MachineTraceMetrics *Traces; |
| 710 | MachineTraceMetrics::Ensemble *MinInstr; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 711 | SSAIfConv IfConv; |
| 712 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 713 | public: |
| 714 | static char ID; |
| 715 | EarlyIfConverter() : MachineFunctionPass(ID) {} |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 716 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 717 | bool runOnMachineFunction(MachineFunction &MF) override; |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 718 | StringRef getPassName() const override { return "Early If-Conversion"; } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 719 | |
| 720 | private: |
| 721 | bool tryConvertIf(MachineBasicBlock*); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 722 | void invalidateTraces(); |
| 723 | bool shouldConvertIf(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 724 | }; |
| 725 | } // end anonymous namespace |
| 726 | |
| 727 | char EarlyIfConverter::ID = 0; |
| 728 | char &llvm::EarlyIfConverterID = EarlyIfConverter::ID; |
| 729 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 730 | INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE, |
| 731 | "Early If Converter", false, false) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 732 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 733 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 734 | INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics) |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 735 | INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE, |
| 736 | "Early If Converter", false, false) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 737 | |
| 738 | void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const { |
| 739 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 740 | AU.addRequired<MachineDominatorTree>(); |
| 741 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 742 | AU.addRequired<MachineLoopInfo>(); |
| 743 | AU.addPreserved<MachineLoopInfo>(); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 744 | AU.addRequired<MachineTraceMetrics>(); |
| 745 | AU.addPreserved<MachineTraceMetrics>(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 746 | MachineFunctionPass::getAnalysisUsage(AU); |
| 747 | } |
| 748 | |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 749 | namespace { |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 750 | /// Update the dominator tree after if-conversion erased some blocks. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 751 | void updateDomTree(MachineDominatorTree *DomTree, const SSAIfConv &IfConv, |
| 752 | ArrayRef<MachineBasicBlock *> Removed) { |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 753 | // convertIf can remove TBB, FBB, and Tail can be merged into Head. |
| 754 | // TBB and FBB should not dominate any blocks. |
| 755 | // Tail children should be transferred to Head. |
| 756 | MachineDomTreeNode *HeadNode = DomTree->getNode(IfConv.Head); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 757 | for (auto B : Removed) { |
| 758 | MachineDomTreeNode *Node = DomTree->getNode(B); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 759 | assert(Node != HeadNode && "Cannot erase the head node"); |
| 760 | while (Node->getNumChildren()) { |
| 761 | assert(Node->getBlock() == IfConv.Tail && "Unexpected children"); |
| 762 | DomTree->changeImmediateDominator(Node->getChildren().back(), HeadNode); |
| 763 | } |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 764 | DomTree->eraseNode(B); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 765 | } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 768 | /// Update LoopInfo after if-conversion. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 769 | void updateLoops(MachineLoopInfo *Loops, |
| 770 | ArrayRef<MachineBasicBlock *> Removed) { |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 771 | if (!Loops) |
| 772 | return; |
| 773 | // If-conversion doesn't change loop structure, and it doesn't mess with back |
| 774 | // edges, so updating LoopInfo is simply removing the dead blocks. |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 775 | for (auto B : Removed) |
| 776 | Loops->removeBlock(B); |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 777 | } |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 778 | } // namespace |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 779 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 780 | /// Invalidate MachineTraceMetrics before if-conversion. |
| 781 | void EarlyIfConverter::invalidateTraces() { |
Jakob Stoklund Olesen | a12a7d5 | 2012-07-30 20:57:50 +0000 | [diff] [blame] | 782 | Traces->verifyAnalysis(); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 783 | Traces->invalidate(IfConv.Head); |
| 784 | Traces->invalidate(IfConv.Tail); |
| 785 | Traces->invalidate(IfConv.TBB); |
| 786 | Traces->invalidate(IfConv.FBB); |
Jakob Stoklund Olesen | a12a7d5 | 2012-07-30 20:57:50 +0000 | [diff] [blame] | 787 | Traces->verifyAnalysis(); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 790 | // Adjust cycles with downward saturation. |
| 791 | static unsigned adjCycles(unsigned Cyc, int Delta) { |
| 792 | if (Delta < 0 && Cyc + Delta > Cyc) |
| 793 | return 0; |
| 794 | return Cyc + Delta; |
| 795 | } |
| 796 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 797 | /// Apply cost model and heuristics to the if-conversion in IfConv. |
| 798 | /// Return true if the conversion is a good idea. |
| 799 | /// |
| 800 | bool EarlyIfConverter::shouldConvertIf() { |
Jakob Stoklund Olesen | fa8a26f | 2012-08-08 18:24:23 +0000 | [diff] [blame] | 801 | // Stress testing mode disables all cost considerations. |
| 802 | if (Stress) |
| 803 | return true; |
| 804 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 805 | if (!MinInstr) |
| 806 | MinInstr = Traces->getEnsemble(MachineTraceMetrics::TS_MinInstrCount); |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 807 | |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 808 | MachineTraceMetrics::Trace TBBTrace = MinInstr->getTrace(IfConv.getTPred()); |
| 809 | MachineTraceMetrics::Trace FBBTrace = MinInstr->getTrace(IfConv.getFPred()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 810 | LLVM_DEBUG(dbgs() << "TBB: " << TBBTrace << "FBB: " << FBBTrace); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 811 | unsigned MinCrit = std::min(TBBTrace.getCriticalPath(), |
| 812 | FBBTrace.getCriticalPath()); |
| 813 | |
| 814 | // Set a somewhat arbitrary limit on the critical path extension we accept. |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 815 | unsigned CritLimit = SchedModel.MispredictPenalty/2; |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 816 | |
| 817 | // If-conversion only makes sense when there is unexploited ILP. Compute the |
| 818 | // maximum-ILP resource length of the trace after if-conversion. Compare it |
| 819 | // to the shortest critical path. |
| 820 | SmallVector<const MachineBasicBlock*, 1> ExtraBlocks; |
| 821 | if (IfConv.TBB != IfConv.Tail) |
| 822 | ExtraBlocks.push_back(IfConv.TBB); |
| 823 | unsigned ResLength = FBBTrace.getResourceLength(ExtraBlocks); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 824 | LLVM_DEBUG(dbgs() << "Resource length " << ResLength |
| 825 | << ", minimal critical path " << MinCrit << '\n'); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 826 | if (ResLength > MinCrit + CritLimit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 827 | LLVM_DEBUG(dbgs() << "Not enough available ILP.\n"); |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 830 | |
| 831 | // Assume that the depth of the first head terminator will also be the depth |
| 832 | // of the select instruction inserted, as determined by the flag dependency. |
| 833 | // TBB / FBB data dependencies may delay the select even more. |
| 834 | MachineTraceMetrics::Trace HeadTrace = MinInstr->getTrace(IfConv.Head); |
| 835 | unsigned BranchDepth = |
Duncan P. N. Exon Smith | e59c8af | 2016-02-22 03:33:28 +0000 | [diff] [blame] | 836 | HeadTrace.getInstrCycles(*IfConv.Head->getFirstTerminator()).Depth; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 837 | LLVM_DEBUG(dbgs() << "Branch depth: " << BranchDepth << '\n'); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 838 | |
| 839 | // Look at all the tail phis, and compute the critical path extension caused |
| 840 | // by inserting select instructions. |
| 841 | MachineTraceMetrics::Trace TailTrace = MinInstr->getTrace(IfConv.Tail); |
| 842 | for (unsigned i = 0, e = IfConv.PHIs.size(); i != e; ++i) { |
| 843 | SSAIfConv::PHIInfo &PI = IfConv.PHIs[i]; |
Duncan P. N. Exon Smith | e59c8af | 2016-02-22 03:33:28 +0000 | [diff] [blame] | 844 | unsigned Slack = TailTrace.getInstrSlack(*PI.PHI); |
| 845 | unsigned MaxDepth = Slack + TailTrace.getInstrCycles(*PI.PHI).Depth; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 846 | LLVM_DEBUG(dbgs() << "Slack " << Slack << ":\t" << *PI.PHI); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 847 | |
| 848 | // The condition is pulled into the critical path. |
| 849 | unsigned CondDepth = adjCycles(BranchDepth, PI.CondCycles); |
| 850 | if (CondDepth > MaxDepth) { |
| 851 | unsigned Extra = CondDepth - MaxDepth; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 852 | LLVM_DEBUG(dbgs() << "Condition adds " << Extra << " cycles.\n"); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 853 | if (Extra > CritLimit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 854 | LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n'); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 855 | return false; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | // The TBB value is pulled into the critical path. |
Duncan P. N. Exon Smith | e59c8af | 2016-02-22 03:33:28 +0000 | [diff] [blame] | 860 | unsigned TDepth = adjCycles(TBBTrace.getPHIDepth(*PI.PHI), PI.TCycles); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 861 | if (TDepth > MaxDepth) { |
| 862 | unsigned Extra = TDepth - MaxDepth; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 863 | LLVM_DEBUG(dbgs() << "TBB data adds " << Extra << " cycles.\n"); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 864 | if (Extra > CritLimit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 865 | LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n'); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 866 | return false; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | // The FBB value is pulled into the critical path. |
Duncan P. N. Exon Smith | e59c8af | 2016-02-22 03:33:28 +0000 | [diff] [blame] | 871 | unsigned FDepth = adjCycles(FBBTrace.getPHIDepth(*PI.PHI), PI.FCycles); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 872 | if (FDepth > MaxDepth) { |
| 873 | unsigned Extra = FDepth - MaxDepth; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 874 | LLVM_DEBUG(dbgs() << "FBB data adds " << Extra << " cycles.\n"); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 875 | if (Extra > CritLimit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 876 | LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n'); |
Jakob Stoklund Olesen | bc55bfd | 2012-08-10 22:27:31 +0000 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | } |
| 880 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 881 | return true; |
| 882 | } |
| 883 | |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 884 | /// Attempt repeated if-conversion on MBB, return true if successful. |
| 885 | /// |
| 886 | bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) { |
| 887 | bool Changed = false; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 888 | while (IfConv.canConvertIf(MBB) && shouldConvertIf()) { |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 889 | // If-convert MBB and update analyses. |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 890 | invalidateTraces(); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 891 | SmallVector<MachineBasicBlock*, 4> RemovedBlocks; |
| 892 | IfConv.convertIf(RemovedBlocks); |
| 893 | Changed = true; |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 894 | updateDomTree(DomTree, IfConv, RemovedBlocks); |
| 895 | updateLoops(Loops, RemovedBlocks); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 896 | } |
| 897 | return Changed; |
| 898 | } |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 899 | |
| 900 | bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 901 | LLVM_DEBUG(dbgs() << "********** EARLY IF-CONVERSION **********\n" |
| 902 | << "********** Function: " << MF.getName() << '\n'); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 903 | if (skipFunction(MF.getFunction())) |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 904 | return false; |
| 905 | |
Eric Christopher | 6b0fcfe | 2014-05-21 23:40:26 +0000 | [diff] [blame] | 906 | // Only run if conversion if the target wants it. |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 907 | const TargetSubtargetInfo &STI = MF.getSubtarget(); |
| 908 | if (!STI.enableEarlyIfConversion()) |
Eric Christopher | 9eff5178f | 2014-05-22 17:49:33 +0000 | [diff] [blame] | 909 | return false; |
Eric Christopher | 6b0fcfe | 2014-05-21 23:40:26 +0000 | [diff] [blame] | 910 | |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 911 | TII = STI.getInstrInfo(); |
| 912 | TRI = STI.getRegisterInfo(); |
| 913 | SchedModel = STI.getSchedModel(); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 914 | MRI = &MF.getRegInfo(); |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 915 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Jakob Stoklund Olesen | bc90a4e | 2012-07-10 22:39:56 +0000 | [diff] [blame] | 916 | Loops = getAnalysisIfAvailable<MachineLoopInfo>(); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 917 | Traces = &getAnalysis<MachineTraceMetrics>(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 918 | MinInstr = nullptr; |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 919 | |
| 920 | bool Changed = false; |
| 921 | IfConv.runOnMachineFunction(MF); |
| 922 | |
Jakob Stoklund Olesen | 0263839 | 2012-07-10 22:18:23 +0000 | [diff] [blame] | 923 | // Visit blocks in dominator tree post-order. The post-order enables nested |
| 924 | // if-conversion in a single pass. The tryConvertIf() function may erase |
| 925 | // blocks, but only blocks dominated by the head block. This makes it safe to |
| 926 | // update the dominator tree while the post-order iterator is still active. |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 927 | for (auto DomNode : post_order(DomTree)) |
| 928 | if (tryConvertIf(DomNode->getBlock())) |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 929 | Changed = true; |
| 930 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 931 | return Changed; |
| 932 | } |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 933 | |
| 934 | //===----------------------------------------------------------------------===// |
| 935 | // EarlyIfPredicator Pass |
| 936 | //===----------------------------------------------------------------------===// |
| 937 | |
| 938 | namespace { |
| 939 | class EarlyIfPredicator : public MachineFunctionPass { |
| 940 | const TargetInstrInfo *TII; |
| 941 | const TargetRegisterInfo *TRI; |
| 942 | TargetSchedModel SchedModel; |
| 943 | MachineRegisterInfo *MRI; |
| 944 | MachineDominatorTree *DomTree; |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 945 | MachineBranchProbabilityInfo *MBPI; |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 946 | MachineLoopInfo *Loops; |
| 947 | SSAIfConv IfConv; |
| 948 | |
| 949 | public: |
| 950 | static char ID; |
| 951 | EarlyIfPredicator() : MachineFunctionPass(ID) {} |
| 952 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 953 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 954 | StringRef getPassName() const override { return "Early If-predicator"; } |
| 955 | |
| 956 | protected: |
| 957 | bool tryConvertIf(MachineBasicBlock *); |
| 958 | bool shouldConvertIf(); |
| 959 | }; |
| 960 | } // end anonymous namespace |
| 961 | |
| 962 | #undef DEBUG_TYPE |
| 963 | #define DEBUG_TYPE "early-if-predicator" |
| 964 | |
| 965 | char EarlyIfPredicator::ID = 0; |
| 966 | char &llvm::EarlyIfPredicatorID = EarlyIfPredicator::ID; |
| 967 | |
| 968 | INITIALIZE_PASS_BEGIN(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", |
| 969 | false, false) |
| 970 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 971 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 972 | INITIALIZE_PASS_END(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false, |
| 973 | false) |
| 974 | |
| 975 | void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const { |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 976 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 977 | AU.addRequired<MachineDominatorTree>(); |
| 978 | AU.addPreserved<MachineDominatorTree>(); |
| 979 | AU.addRequired<MachineLoopInfo>(); |
| 980 | AU.addPreserved<MachineLoopInfo>(); |
| 981 | MachineFunctionPass::getAnalysisUsage(AU); |
| 982 | } |
| 983 | |
| 984 | /// Apply the target heuristic to decide if the transformation is profitable. |
| 985 | bool EarlyIfPredicator::shouldConvertIf() { |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 986 | auto TrueProbability = MBPI->getEdgeProbability(IfConv.Head, IfConv.TBB); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 987 | if (IfConv.isTriangle()) { |
| 988 | MachineBasicBlock &IfBlock = |
| 989 | (IfConv.TBB == IfConv.Tail) ? *IfConv.FBB : *IfConv.TBB; |
| 990 | |
| 991 | unsigned ExtraPredCost = 0; |
| 992 | unsigned Cycles = 0; |
| 993 | for (MachineInstr &I : IfBlock) { |
| 994 | unsigned NumCycles = SchedModel.computeInstrLatency(&I, false); |
| 995 | if (NumCycles > 1) |
| 996 | Cycles += NumCycles - 1; |
| 997 | ExtraPredCost += TII->getPredicationCost(I); |
| 998 | } |
| 999 | |
| 1000 | return TII->isProfitableToIfCvt(IfBlock, Cycles, ExtraPredCost, |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 1001 | TrueProbability); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 1002 | } |
| 1003 | unsigned TExtra = 0; |
| 1004 | unsigned FExtra = 0; |
| 1005 | unsigned TCycle = 0; |
| 1006 | unsigned FCycle = 0; |
| 1007 | for (MachineInstr &I : *IfConv.TBB) { |
| 1008 | unsigned NumCycles = SchedModel.computeInstrLatency(&I, false); |
| 1009 | if (NumCycles > 1) |
| 1010 | TCycle += NumCycles - 1; |
| 1011 | TExtra += TII->getPredicationCost(I); |
| 1012 | } |
| 1013 | for (MachineInstr &I : *IfConv.FBB) { |
| 1014 | unsigned NumCycles = SchedModel.computeInstrLatency(&I, false); |
| 1015 | if (NumCycles > 1) |
| 1016 | FCycle += NumCycles - 1; |
| 1017 | FExtra += TII->getPredicationCost(I); |
| 1018 | } |
| 1019 | return TII->isProfitableToIfCvt(*IfConv.TBB, TCycle, TExtra, *IfConv.FBB, |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 1020 | FCycle, FExtra, TrueProbability); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /// Attempt repeated if-conversion on MBB, return true if successful. |
| 1024 | /// |
| 1025 | bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) { |
| 1026 | bool Changed = false; |
| 1027 | while (IfConv.canConvertIf(MBB, /*Predicate*/ true) && shouldConvertIf()) { |
| 1028 | // If-convert MBB and update analyses. |
| 1029 | SmallVector<MachineBasicBlock *, 4> RemovedBlocks; |
| 1030 | IfConv.convertIf(RemovedBlocks, /*Predicate*/ true); |
| 1031 | Changed = true; |
| 1032 | updateDomTree(DomTree, IfConv, RemovedBlocks); |
| 1033 | updateLoops(Loops, RemovedBlocks); |
| 1034 | } |
| 1035 | return Changed; |
| 1036 | } |
| 1037 | |
| 1038 | bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) { |
| 1039 | LLVM_DEBUG(dbgs() << "********** EARLY IF-PREDICATOR **********\n" |
| 1040 | << "********** Function: " << MF.getName() << '\n'); |
| 1041 | if (skipFunction(MF.getFunction())) |
| 1042 | return false; |
| 1043 | |
| 1044 | const TargetSubtargetInfo &STI = MF.getSubtarget(); |
| 1045 | TII = STI.getInstrInfo(); |
| 1046 | TRI = STI.getRegisterInfo(); |
| 1047 | MRI = &MF.getRegInfo(); |
| 1048 | SchedModel.init(&STI); |
| 1049 | DomTree = &getAnalysis<MachineDominatorTree>(); |
| 1050 | Loops = getAnalysisIfAvailable<MachineLoopInfo>(); |
shkzhang | 1408e7e | 2019-12-11 04:46:00 -0500 | [diff] [blame] | 1051 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
Thomas Raoux | be699bf | 2019-08-20 15:54:59 +0000 | [diff] [blame] | 1052 | |
| 1053 | bool Changed = false; |
| 1054 | IfConv.runOnMachineFunction(MF); |
| 1055 | |
| 1056 | // Visit blocks in dominator tree post-order. The post-order enables nested |
| 1057 | // if-conversion in a single pass. The tryConvertIf() function may erase |
| 1058 | // blocks, but only blocks dominated by the head block. This makes it safe to |
| 1059 | // update the dominator tree while the post-order iterator is still active. |
| 1060 | for (auto DomNode : post_order(DomTree)) |
| 1061 | if (tryConvertIf(DomNode->getBlock())) |
| 1062 | Changed = true; |
| 1063 | |
| 1064 | return Changed; |
| 1065 | } |