Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1 | //===- OptimizePHIs.cpp - Optimize machine instruction PHIs ---------------===// |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass optimizes machine instruction PHIs to take advantage of |
| 10 | // opportunities created during DAG legalization. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallPtrSet.h" |
| 15 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineOperand.h" |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 25 | #include <cassert> |
| 26 | |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "opt-phis" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 31 | STATISTIC(NumPHICycles, "Number of PHI cycles replaced"); |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 32 | STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles"); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 35 | |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 36 | class OptimizePHIs : public MachineFunctionPass { |
| 37 | MachineRegisterInfo *MRI; |
| 38 | const TargetInstrInfo *TII; |
| 39 | |
| 40 | public: |
| 41 | static char ID; // Pass identification |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 42 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 43 | OptimizePHIs() : MachineFunctionPass(ID) { |
| 44 | initializeOptimizePHIsPass(*PassRegistry::getPassRegistry()); |
| 45 | } |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 46 | |
Fangrui Song | cb0bab8 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 47 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 48 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 50 | AU.setPreservesCFG(); |
| 51 | MachineFunctionPass::getAnalysisUsage(AU); |
| 52 | } |
| 53 | |
| 54 | private: |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 55 | using InstrSet = SmallPtrSet<MachineInstr *, 16>; |
| 56 | using InstrSetIterator = SmallPtrSetIterator<MachineInstr *>; |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 57 | |
| 58 | bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg, |
| 59 | InstrSet &PHIsInCycle); |
| 60 | bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle); |
| 61 | bool OptimizeBB(MachineBasicBlock &MBB); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 62 | }; |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 63 | |
| 64 | } // end anonymous namespace |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 65 | |
| 66 | char OptimizePHIs::ID = 0; |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 67 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 68 | char &llvm::OptimizePHIsID = OptimizePHIs::ID; |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 69 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 70 | INITIALIZE_PASS(OptimizePHIs, DEBUG_TYPE, |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 71 | "Optimize machine instruction PHIs", false, false) |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 72 | |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 73 | bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 74 | if (skipFunction(Fn.getFunction())) |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 75 | return false; |
| 76 | |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 77 | MRI = &Fn.getRegInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 78 | TII = Fn.getSubtarget().getInstrInfo(); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 79 | |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 80 | // Find dead PHI cycles and PHI cycles that can be replaced by a single |
| 81 | // value. InstCombine does these optimizations, but DAG legalization may |
| 82 | // introduce new opportunities, e.g., when i64 values are split up for |
| 83 | // 32-bit targets. |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 84 | bool Changed = false; |
| 85 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 86 | Changed |= OptimizeBB(*I); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 87 | |
| 88 | return Changed; |
| 89 | } |
| 90 | |
| 91 | /// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands |
Dinar Temirbulatov | 8c8724d | 2018-12-15 14:37:01 +0000 | [diff] [blame] | 92 | /// are copies of SingleValReg, possibly via copies through other PHIs. If |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 93 | /// SingleValReg is zero on entry, it is set to the register with the single |
Dinar Temirbulatov | 8c8724d | 2018-12-15 14:37:01 +0000 | [diff] [blame] | 94 | /// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that |
| 95 | /// have been scanned. PHIs may be grouped by cycle, several cycles or chains. |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 96 | bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI, |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 97 | unsigned &SingleValReg, |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 98 | InstrSet &PHIsInCycle) { |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 99 | assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction"); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 100 | Register DstReg = MI->getOperand(0).getReg(); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 101 | |
| 102 | // See if we already saw this register. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 103 | if (!PHIsInCycle.insert(MI).second) |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 104 | return true; |
| 105 | |
| 106 | // Don't scan crazily complex things. |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 107 | if (PHIsInCycle.size() == 16) |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 108 | return false; |
| 109 | |
| 110 | // Scan the PHI operands. |
| 111 | for (unsigned i = 1; i != MI->getNumOperands(); i += 2) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 112 | Register SrcReg = MI->getOperand(i).getReg(); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 113 | if (SrcReg == DstReg) |
| 114 | continue; |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 115 | MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 116 | |
| 117 | // Skip over register-to-register moves. |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 118 | if (SrcMI && SrcMI->isCopy() && !SrcMI->getOperand(0).getSubReg() && |
Jakob Stoklund Olesen | 37c42a3 | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 119 | !SrcMI->getOperand(1).getSubReg() && |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 120 | Register::isVirtualRegister(SrcMI->getOperand(1).getReg())) { |
Dinar Temirbulatov | 8c8724d | 2018-12-15 14:37:01 +0000 | [diff] [blame] | 121 | SrcReg = SrcMI->getOperand(1).getReg(); |
| 122 | SrcMI = MRI->getVRegDef(SrcReg); |
| 123 | } |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 124 | if (!SrcMI) |
| 125 | return false; |
| 126 | |
| 127 | if (SrcMI->isPHI()) { |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 128 | if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle)) |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } else { |
| 131 | // Fail if there is more than one non-phi/non-move register. |
Dinar Temirbulatov | 8c8724d | 2018-12-15 14:37:01 +0000 | [diff] [blame] | 132 | if (SingleValReg != 0 && SingleValReg != SrcReg) |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 133 | return false; |
| 134 | SingleValReg = SrcReg; |
| 135 | } |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 140 | /// IsDeadPHICycle - Check if the register defined by a PHI is only used by |
| 141 | /// other PHIs in a cycle. |
| 142 | bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) { |
| 143 | assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction"); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 144 | Register DstReg = MI->getOperand(0).getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 145 | assert(Register::isVirtualRegister(DstReg) && |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 146 | "PHI destination is not a virtual register"); |
| 147 | |
| 148 | // See if we already saw this register. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 149 | if (!PHIsInCycle.insert(MI).second) |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 150 | return true; |
| 151 | |
| 152 | // Don't scan crazily complex things. |
| 153 | if (PHIsInCycle.size() == 16) |
| 154 | return false; |
| 155 | |
Mikael Holmen | b5deac4 | 2017-12-07 07:01:21 +0000 | [diff] [blame] | 156 | for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DstReg)) { |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 157 | if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle)) |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | /// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by |
| 165 | /// a single value. |
| 166 | bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) { |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 167 | bool Changed = false; |
| 168 | for (MachineBasicBlock::iterator |
| 169 | MII = MBB.begin(), E = MBB.end(); MII != E; ) { |
| 170 | MachineInstr *MI = &*MII++; |
| 171 | if (!MI->isPHI()) |
| 172 | break; |
| 173 | |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 174 | // Check for single-value PHI cycles. |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 175 | unsigned SingleValReg = 0; |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 176 | InstrSet PHIsInCycle; |
| 177 | if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) && |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 178 | SingleValReg != 0) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 179 | Register OldReg = MI->getOperand(0).getReg(); |
Cameron Zwarich | d85bc10 | 2011-10-17 21:54:46 +0000 | [diff] [blame] | 180 | if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg))) |
| 181 | continue; |
| 182 | |
| 183 | MRI->replaceRegWith(OldReg, SingleValReg); |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 184 | MI->eraseFromParent(); |
David Green | c93c6f3 | 2019-02-12 15:02:57 +0000 | [diff] [blame] | 185 | |
| 186 | // The kill flags on OldReg and SingleValReg may no longer be correct. |
| 187 | MRI->clearKillFlags(SingleValReg); |
| 188 | |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 189 | ++NumPHICycles; |
| 190 | Changed = true; |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
| 194 | // Check for dead PHI cycles. |
| 195 | PHIsInCycle.clear(); |
| 196 | if (IsDeadPHICycle(MI, PHIsInCycle)) { |
| 197 | for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end(); |
| 198 | PI != PE; ++PI) { |
| 199 | MachineInstr *PhiMI = *PI; |
Duncan P. N. Exon Smith | 00ec93d | 2016-08-17 00:43:59 +0000 | [diff] [blame] | 200 | if (MII == PhiMI) |
Bob Wilson | 01abf8f | 2010-02-13 00:31:44 +0000 | [diff] [blame] | 201 | ++MII; |
| 202 | PhiMI->eraseFromParent(); |
| 203 | } |
| 204 | ++NumDeadPHICycles; |
| 205 | Changed = true; |
Bob Wilson | 0827e04 | 2010-02-12 01:30:21 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | return Changed; |
| 209 | } |