blob: 1a493964e678938a86e58a2568bc0e15b13ea068 [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");
Daniel Sanders0c476112019-08-15 19:22:08 +0000100 Register DstReg = MI->getOperand(0).getReg();
Bob Wilson0827e042010-02-12 01:30:21 +0000101
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) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000112 Register SrcReg = MI->getOperand(i).getReg();
Bob Wilson0827e042010-02-12 01:30:21 +0000113 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.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000118 if (SrcMI && SrcMI->isCopy() && !SrcMI->getOperand(0).getSubReg() &&
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000119 !SrcMI->getOperand(1).getSubReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000120 Register::isVirtualRegister(SrcMI->getOperand(1).getReg())) {
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +0000121 SrcReg = SrcMI->getOperand(1).getReg();
122 SrcMI = MRI->getVRegDef(SrcReg);
123 }
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.
Dinar Temirbulatov8c8724d2018-12-15 14:37:01 +0000132 if (SingleValReg != 0 && SingleValReg != SrcReg)
Bob Wilson0827e042010-02-12 01:30:21 +0000133 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");
Daniel Sanders0c476112019-08-15 19:22:08 +0000144 Register DstReg = MI->getOperand(0).getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000145 assert(Register::isVirtualRegister(DstReg) &&
Bob Wilson01abf8f2010-02-13 00:31:44 +0000146 "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) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000179 Register OldReg = MI->getOperand(0).getReg();
Cameron Zwarichd85bc102011-10-17 21:54:46 +0000180 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();
David Greenc93c6f32019-02-12 15:02:57 +0000185
186 // The kill flags on OldReg and SingleValReg may no longer be correct.
187 MRI->clearKillFlags(SingleValReg);
188
Bob Wilson0827e042010-02-12 01:30:21 +0000189 ++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}