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