blob: 8972867ba083ff243bb31aa5d3201fb25ca646e8 [file] [log] [blame]
Eugene Zelenko32a40562017-09-11 23:00:48 +00001//===- OptimizePHIs.cpp - Optimize machine instruction PHIs ---------------===//
Bob Wilson0827e042010-02-12 01:30:21 +00002//
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 Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/Statistic.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000017#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineFunction.h"
Bob Wilson0827e042010-02-12 01:30:21 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000021#include "llvm/CodeGen/MachineOperand.h"
Bob Wilson0827e042010-02-12 01:30:21 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetRegisterInfo.h"
24#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000025#include "llvm/Pass.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000026#include <cassert>
27
Bob Wilson0827e042010-02-12 01:30:21 +000028using namespace llvm;
29
Matthias Braun1527baa2017-05-25 21:26:32 +000030#define DEBUG_TYPE "opt-phis"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000031
Bob Wilson0827e042010-02-12 01:30:21 +000032STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
Bob Wilson01abf8f2010-02-13 00:31:44 +000033STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
Bob Wilson0827e042010-02-12 01:30:21 +000034
35namespace {
Eugene Zelenko32a40562017-09-11 23:00:48 +000036
Bob Wilson0827e042010-02-12 01:30:21 +000037 class OptimizePHIs : public MachineFunctionPass {
38 MachineRegisterInfo *MRI;
39 const TargetInstrInfo *TII;
40
41 public:
42 static char ID; // Pass identification
Eugene Zelenko32a40562017-09-11 23:00:48 +000043
Owen Anderson6c18d1a2010-10-19 17:21:58 +000044 OptimizePHIs() : MachineFunctionPass(ID) {
45 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
46 }
Bob Wilson0827e042010-02-12 01:30:21 +000047
Craig Topper4584cd52014-03-07 09:26:03 +000048 bool runOnMachineFunction(MachineFunction &MF) override;
Bob Wilson0827e042010-02-12 01:30:21 +000049
Craig Topper4584cd52014-03-07 09:26:03 +000050 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bob Wilson0827e042010-02-12 01:30:21 +000051 AU.setPreservesCFG();
52 MachineFunctionPass::getAnalysisUsage(AU);
53 }
54
55 private:
Eugene Zelenko32a40562017-09-11 23:00:48 +000056 using InstrSet = SmallPtrSet<MachineInstr *, 16>;
57 using InstrSetIterator = SmallPtrSetIterator<MachineInstr *>;
Bob Wilson01abf8f2010-02-13 00:31:44 +000058
59 bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
60 InstrSet &PHIsInCycle);
61 bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
62 bool OptimizeBB(MachineBasicBlock &MBB);
Bob Wilson0827e042010-02-12 01:30:21 +000063 };
Eugene Zelenko32a40562017-09-11 23:00:48 +000064
65} // end anonymous namespace
Bob Wilson0827e042010-02-12 01:30:21 +000066
67char OptimizePHIs::ID = 0;
Eugene Zelenko32a40562017-09-11 23:00:48 +000068
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000069char &llvm::OptimizePHIsID = OptimizePHIs::ID;
Eugene Zelenko32a40562017-09-11 23:00:48 +000070
Matthias Braun1527baa2017-05-25 21:26:32 +000071INITIALIZE_PASS(OptimizePHIs, DEBUG_TYPE,
Owen Andersondf7a4f22010-10-07 22:25:06 +000072 "Optimize machine instruction PHIs", false, false)
Bob Wilson0827e042010-02-12 01:30:21 +000073
Bob Wilson0827e042010-02-12 01:30:21 +000074bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
Matthias Braunf1caa282017-12-15 22:22:58 +000075 if (skipFunction(Fn.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +000076 return false;
77
Bob Wilson0827e042010-02-12 01:30:21 +000078 MRI = &Fn.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000079 TII = Fn.getSubtarget().getInstrInfo();
Bob Wilson0827e042010-02-12 01:30:21 +000080
Bob Wilson01abf8f2010-02-13 00:31:44 +000081 // 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 Wilson0827e042010-02-12 01:30:21 +000085 bool Changed = false;
86 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Bob Wilson01abf8f2010-02-13 00:31:44 +000087 Changed |= OptimizeBB(*I);
Bob Wilson0827e042010-02-12 01:30:21 +000088
89 return Changed;
90}
91
92/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
93/// are copies of SingleValReg, possibly via copies through other PHIs. If
94/// SingleValReg is zero on entry, it is set to the register with the single
Bob Wilson01abf8f2010-02-13 00:31:44 +000095/// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
Bob Wilson0827e042010-02-12 01:30:21 +000096/// have been scanned.
Bob Wilson01abf8f2010-02-13 00:31:44 +000097bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
Bob Wilson0827e042010-02-12 01:30:21 +000098 unsigned &SingleValReg,
Bob Wilson01abf8f2010-02-13 00:31:44 +000099 InstrSet &PHIsInCycle) {
Bob Wilson0827e042010-02-12 01:30:21 +0000100 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 Blaikie70573dc2014-11-19 07:49:26 +0000104 if (!PHIsInCycle.insert(MI).second)
Bob Wilson0827e042010-02-12 01:30:21 +0000105 return true;
106
107 // Don't scan crazily complex things.
Bob Wilson01abf8f2010-02-13 00:31:44 +0000108 if (PHIsInCycle.size() == 16)
Bob Wilson0827e042010-02-12 01:30:21 +0000109 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 Wilson01abf8f2010-02-13 00:31:44 +0000116 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
Bob Wilson0827e042010-02-12 01:30:21 +0000117
118 // Skip over register-to-register moves.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000119 if (SrcMI && SrcMI->isCopy() &&
120 !SrcMI->getOperand(0).getSubReg() &&
121 !SrcMI->getOperand(1).getSubReg() &&
122 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000123 SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
Bob Wilson0827e042010-02-12 01:30:21 +0000124 if (!SrcMI)
125 return false;
126
127 if (SrcMI->isPHI()) {
Bob Wilson01abf8f2010-02-13 00:31:44 +0000128 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
Bob Wilson0827e042010-02-12 01:30:21 +0000129 return false;
130 } else {
131 // Fail if there is more than one non-phi/non-move register.
132 if (SingleValReg != 0)
133 return false;
134 SingleValReg = SrcReg;
135 }
136 }
137 return true;
138}
139
Bob Wilson01abf8f2010-02-13 00:31:44 +0000140/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
141/// other PHIs in a cycle.
142bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
143 assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
144 unsigned DstReg = MI->getOperand(0).getReg();
145 assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
146 "PHI destination is not a virtual register");
147
148 // See if we already saw this register.
David Blaikie70573dc2014-11-19 07:49:26 +0000149 if (!PHIsInCycle.insert(MI).second)
Bob Wilson01abf8f2010-02-13 00:31:44 +0000150 return true;
151
152 // Don't scan crazily complex things.
153 if (PHIsInCycle.size() == 16)
154 return false;
155
Mikael Holmenb5deac42017-12-07 07:01:21 +0000156 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DstReg)) {
Owen Andersonb36376e2014-03-17 19:36:09 +0000157 if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
Bob Wilson01abf8f2010-02-13 00:31:44 +0000158 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.
166bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
Bob Wilson0827e042010-02-12 01:30:21 +0000167 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 Wilson01abf8f2010-02-13 00:31:44 +0000174 // Check for single-value PHI cycles.
Bob Wilson0827e042010-02-12 01:30:21 +0000175 unsigned SingleValReg = 0;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000176 InstrSet PHIsInCycle;
177 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
Bob Wilson0827e042010-02-12 01:30:21 +0000178 SingleValReg != 0) {
Cameron Zwarichd85bc102011-10-17 21:54:46 +0000179 unsigned OldReg = MI->getOperand(0).getReg();
180 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
181 continue;
182
183 MRI->replaceRegWith(OldReg, SingleValReg);
Bob Wilson0827e042010-02-12 01:30:21 +0000184 MI->eraseFromParent();
185 ++NumPHICycles;
186 Changed = true;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000187 continue;
188 }
189
190 // Check for dead PHI cycles.
191 PHIsInCycle.clear();
192 if (IsDeadPHICycle(MI, PHIsInCycle)) {
193 for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
194 PI != PE; ++PI) {
195 MachineInstr *PhiMI = *PI;
Duncan P. N. Exon Smith00ec93d2016-08-17 00:43:59 +0000196 if (MII == PhiMI)
Bob Wilson01abf8f2010-02-13 00:31:44 +0000197 ++MII;
198 PhiMI->eraseFromParent();
199 }
200 ++NumDeadPHICycles;
201 Changed = true;
Bob Wilson0827e042010-02-12 01:30:21 +0000202 }
203 }
204 return Changed;
205}