blob: fb2340b722190f77919f4e1cfdc492a962dac93a [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Wilson0827e042010-02-12 01:30:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass optimizes machine instruction PHIs to take advantage of
10// opportunities created during DAG legalization.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/SmallPtrSet.h"
15#include "llvm/ADT/Statistic.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000016#include "llvm/CodeGen/MachineBasicBlock.h"
17#include "llvm/CodeGen/MachineFunction.h"
Bob Wilson0827e042010-02-12 01:30:21 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000020#include "llvm/CodeGen/MachineOperand.h"
Bob Wilson0827e042010-02-12 01:30:21 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetRegisterInfo.h"
23#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000024#include "llvm/Pass.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000025#include <cassert>
26
Bob Wilson0827e042010-02-12 01:30:21 +000027using namespace llvm;
28
Matthias Braun1527baa2017-05-25 21:26:32 +000029#define DEBUG_TYPE "opt-phis"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030
Bob Wilson0827e042010-02-12 01:30:21 +000031STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
Bob Wilson01abf8f2010-02-13 00:31:44 +000032STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
Bob Wilson0827e042010-02-12 01:30:21 +000033
34namespace {
Eugene Zelenko32a40562017-09-11 23:00:48 +000035
Bob Wilson0827e042010-02-12 01:30:21 +000036 class OptimizePHIs : public MachineFunctionPass {
37 MachineRegisterInfo *MRI;
38 const TargetInstrInfo *TII;
39
40 public:
41 static char ID; // Pass identification
Eugene Zelenko32a40562017-09-11 23:00:48 +000042
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 OptimizePHIs() : MachineFunctionPass(ID) {
44 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
45 }
Bob Wilson0827e042010-02-12 01:30:21 +000046
Fangrui Songcb0bab82018-07-16 18:51:40 +000047 bool runOnMachineFunction(MachineFunction &Fn) override;
Bob Wilson0827e042010-02-12 01:30:21 +000048
Craig Topper4584cd52014-03-07 09:26:03 +000049 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bob Wilson0827e042010-02-12 01:30:21 +000050 AU.setPreservesCFG();
51 MachineFunctionPass::getAnalysisUsage(AU);
52 }
53
54 private:
Eugene Zelenko32a40562017-09-11 23:00:48 +000055 using InstrSet = SmallPtrSet<MachineInstr *, 16>;
56 using InstrSetIterator = SmallPtrSetIterator<MachineInstr *>;
Bob Wilson01abf8f2010-02-13 00:31:44 +000057
58 bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
59 InstrSet &PHIsInCycle);
60 bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
61 bool OptimizeBB(MachineBasicBlock &MBB);
Bob Wilson0827e042010-02-12 01:30:21 +000062 };
Eugene Zelenko32a40562017-09-11 23:00:48 +000063
64} // end anonymous namespace
Bob Wilson0827e042010-02-12 01:30:21 +000065
66char OptimizePHIs::ID = 0;
Eugene Zelenko32a40562017-09-11 23:00:48 +000067
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000068char &llvm::OptimizePHIsID = OptimizePHIs::ID;
Eugene Zelenko32a40562017-09-11 23:00:48 +000069
Matthias Braun1527baa2017-05-25 21:26:32 +000070INITIALIZE_PASS(OptimizePHIs, DEBUG_TYPE,
Owen Andersondf7a4f22010-10-07 22:25:06 +000071 "Optimize machine instruction PHIs", false, false)
Bob Wilson0827e042010-02-12 01:30:21 +000072
Bob Wilson0827e042010-02-12 01:30:21 +000073bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
Matthias Braunf1caa282017-12-15 22:22:58 +000074 if (skipFunction(Fn.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +000075 return false;
76
Bob Wilson0827e042010-02-12 01:30:21 +000077 MRI = &Fn.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000078 TII = Fn.getSubtarget().getInstrInfo();
Bob Wilson0827e042010-02-12 01:30:21 +000079
Bob Wilson01abf8f2010-02-13 00:31:44 +000080 // 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 Wilson0827e042010-02-12 01:30:21 +000084 bool Changed = false;
85 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Bob Wilson01abf8f2010-02-13 00:31:44 +000086 Changed |= OptimizeBB(*I);
Bob Wilson0827e042010-02-12 01:30:21 +000087
88 return Changed;
89}
90
91/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +000092/// are copies of SingleValReg, possibly via copies through other PHIs. If
Bob Wilson0827e042010-02-12 01:30:21 +000093/// SingleValReg is zero on entry, it is set to the register with the single
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +000094/// 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 Wilson01abf8f2010-02-13 00:31:44 +000096bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
Bob Wilson0827e042010-02-12 01:30:21 +000097 unsigned &SingleValReg,
Bob Wilson01abf8f2010-02-13 00:31:44 +000098 InstrSet &PHIsInCycle) {
Bob Wilson0827e042010-02-12 01:30:21 +000099 assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
100 unsigned DstReg = MI->getOperand(0).getReg();
101
102 // See if we already saw this register.
David Blaikie70573dc2014-11-19 07:49:26 +0000103 if (!PHIsInCycle.insert(MI).second)
Bob Wilson0827e042010-02-12 01:30:21 +0000104 return true;
105
106 // Don't scan crazily complex things.
Bob Wilson01abf8f2010-02-13 00:31:44 +0000107 if (PHIsInCycle.size() == 16)
Bob Wilson0827e042010-02-12 01:30:21 +0000108 return false;
109
110 // Scan the PHI operands.
111 for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
112 unsigned SrcReg = MI->getOperand(i).getReg();
113 if (SrcReg == DstReg)
114 continue;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000115 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
Bob Wilson0827e042010-02-12 01:30:21 +0000116
117 // Skip over register-to-register moves.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000118 if (SrcMI && SrcMI->isCopy() &&
119 !SrcMI->getOperand(0).getSubReg() &&
120 !SrcMI->getOperand(1).getSubReg() &&
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +0000121 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg())) {
122 SrcReg = SrcMI->getOperand(1).getReg();
123 SrcMI = MRI->getVRegDef(SrcReg);
124 }
Bob Wilson0827e042010-02-12 01:30:21 +0000125 if (!SrcMI)
126 return false;
127
128 if (SrcMI->isPHI()) {
Bob Wilson01abf8f2010-02-13 00:31:44 +0000129 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
Bob Wilson0827e042010-02-12 01:30:21 +0000130 return false;
131 } else {
132 // Fail if there is more than one non-phi/non-move register.
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +0000133 if (SingleValReg != 0 && SingleValReg != SrcReg)
Bob Wilson0827e042010-02-12 01:30:21 +0000134 return false;
135 SingleValReg = SrcReg;
136 }
137 }
138 return true;
139}
140
Bob Wilson01abf8f2010-02-13 00:31:44 +0000141/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
142/// other PHIs in a cycle.
143bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
144 assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
145 unsigned DstReg = MI->getOperand(0).getReg();
146 assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
147 "PHI destination is not a virtual register");
148
149 // See if we already saw this register.
David Blaikie70573dc2014-11-19 07:49:26 +0000150 if (!PHIsInCycle.insert(MI).second)
Bob Wilson01abf8f2010-02-13 00:31:44 +0000151 return true;
152
153 // Don't scan crazily complex things.
154 if (PHIsInCycle.size() == 16)
155 return false;
156
Mikael Holmenb5deac42017-12-07 07:01:21 +0000157 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DstReg)) {
Owen Andersonb36376e2014-03-17 19:36:09 +0000158 if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
Bob Wilson01abf8f2010-02-13 00:31:44 +0000159 return false;
160 }
161
162 return true;
163}
164
165/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
166/// a single value.
167bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
Bob Wilson0827e042010-02-12 01:30:21 +0000168 bool Changed = false;
169 for (MachineBasicBlock::iterator
170 MII = MBB.begin(), E = MBB.end(); MII != E; ) {
171 MachineInstr *MI = &*MII++;
172 if (!MI->isPHI())
173 break;
174
Bob Wilson01abf8f2010-02-13 00:31:44 +0000175 // Check for single-value PHI cycles.
Bob Wilson0827e042010-02-12 01:30:21 +0000176 unsigned SingleValReg = 0;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000177 InstrSet PHIsInCycle;
178 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
Bob Wilson0827e042010-02-12 01:30:21 +0000179 SingleValReg != 0) {
Cameron Zwarichd85bc102011-10-17 21:54:46 +0000180 unsigned OldReg = MI->getOperand(0).getReg();
181 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
182 continue;
183
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +0000184 // for the case SingleValReg taken from copy instr
185 MRI->clearKillFlags(SingleValReg);
186
Cameron Zwarichd85bc102011-10-17 21:54:46 +0000187 MRI->replaceRegWith(OldReg, SingleValReg);
Bob Wilson0827e042010-02-12 01:30:21 +0000188 MI->eraseFromParent();
189 ++NumPHICycles;
190 Changed = true;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000191 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 Smith00ec93d2016-08-17 00:43:59 +0000200 if (MII == PhiMI)
Bob Wilson01abf8f2010-02-13 00:31:44 +0000201 ++MII;
202 PhiMI->eraseFromParent();
203 }
204 ++NumDeadPHICycles;
205 Changed = true;
Bob Wilson0827e042010-02-12 01:30:21 +0000206 }
207 }
208 return Changed;
209}