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