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