1 //===- OptimizePHIs.cpp - Optimize machine instruction PHIs ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass optimizes machine instruction PHIs to take advantage of
10 // opportunities created during DAG legalization.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/CodeGen/MachineBasicBlock.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineOperand.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/TargetSubtargetInfo.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Pass.h"
29 #define DEBUG_TYPE "opt-phis"
31 STATISTIC(NumPHICycles
, "Number of PHI cycles replaced");
32 STATISTIC(NumDeadPHICycles
, "Number of dead PHI cycles");
36 class OptimizePHIs
: public MachineFunctionPass
{
37 MachineRegisterInfo
*MRI
;
38 const TargetInstrInfo
*TII
;
41 static char ID
; // Pass identification
43 OptimizePHIs() : MachineFunctionPass(ID
) {
44 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
47 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
49 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
51 MachineFunctionPass::getAnalysisUsage(AU
);
55 using InstrSet
= SmallPtrSet
<MachineInstr
*, 16>;
56 using InstrSetIterator
= SmallPtrSetIterator
<MachineInstr
*>;
58 bool IsSingleValuePHICycle(MachineInstr
*MI
, unsigned &SingleValReg
,
59 InstrSet
&PHIsInCycle
);
60 bool IsDeadPHICycle(MachineInstr
*MI
, InstrSet
&PHIsInCycle
);
61 bool OptimizeBB(MachineBasicBlock
&MBB
);
64 } // end anonymous namespace
66 char OptimizePHIs::ID
= 0;
68 char &llvm::OptimizePHIsID
= OptimizePHIs::ID
;
70 INITIALIZE_PASS(OptimizePHIs
, DEBUG_TYPE
,
71 "Optimize machine instruction PHIs", false, false)
73 bool OptimizePHIs::runOnMachineFunction(MachineFunction
&Fn
) {
74 if (skipFunction(Fn
.getFunction()))
77 MRI
= &Fn
.getRegInfo();
78 TII
= Fn
.getSubtarget().getInstrInfo();
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
85 for (MachineBasicBlock
&MBB
: Fn
)
86 Changed
|= OptimizeBB(MBB
);
91 /// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
92 /// are copies of SingleValReg, possibly via copies through other PHIs. If
93 /// SingleValReg is zero on entry, it is set to the register with the single
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.
96 bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr
*MI
,
97 unsigned &SingleValReg
,
98 InstrSet
&PHIsInCycle
) {
99 assert(MI
->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
100 Register DstReg
= MI
->getOperand(0).getReg();
102 // See if we already saw this register.
103 if (!PHIsInCycle
.insert(MI
).second
)
106 // Don't scan crazily complex things.
107 if (PHIsInCycle
.size() == 16)
110 // Scan the PHI operands.
111 for (unsigned i
= 1; i
!= MI
->getNumOperands(); i
+= 2) {
112 Register SrcReg
= MI
->getOperand(i
).getReg();
113 if (SrcReg
== DstReg
)
115 MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
117 // Skip over register-to-register moves.
118 if (SrcMI
&& SrcMI
->isCopy() && !SrcMI
->getOperand(0).getSubReg() &&
119 !SrcMI
->getOperand(1).getSubReg() &&
120 Register::isVirtualRegister(SrcMI
->getOperand(1).getReg())) {
121 SrcReg
= SrcMI
->getOperand(1).getReg();
122 SrcMI
= MRI
->getVRegDef(SrcReg
);
127 if (SrcMI
->isPHI()) {
128 if (!IsSingleValuePHICycle(SrcMI
, SingleValReg
, PHIsInCycle
))
131 // Fail if there is more than one non-phi/non-move register.
132 if (SingleValReg
!= 0 && SingleValReg
!= SrcReg
)
134 SingleValReg
= SrcReg
;
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");
144 Register DstReg
= MI
->getOperand(0).getReg();
145 assert(Register::isVirtualRegister(DstReg
) &&
146 "PHI destination is not a virtual register");
148 // See if we already saw this register.
149 if (!PHIsInCycle
.insert(MI
).second
)
152 // Don't scan crazily complex things.
153 if (PHIsInCycle
.size() == 16)
156 for (MachineInstr
&UseMI
: MRI
->use_nodbg_instructions(DstReg
)) {
157 if (!UseMI
.isPHI() || !IsDeadPHICycle(&UseMI
, PHIsInCycle
))
164 /// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
166 bool OptimizePHIs::OptimizeBB(MachineBasicBlock
&MBB
) {
167 bool Changed
= false;
168 for (MachineBasicBlock::iterator
169 MII
= MBB
.begin(), E
= MBB
.end(); MII
!= E
; ) {
170 MachineInstr
*MI
= &*MII
++;
174 // Check for single-value PHI cycles.
175 unsigned SingleValReg
= 0;
176 InstrSet PHIsInCycle
;
177 if (IsSingleValuePHICycle(MI
, SingleValReg
, PHIsInCycle
) &&
179 Register OldReg
= MI
->getOperand(0).getReg();
180 if (!MRI
->constrainRegClass(SingleValReg
, MRI
->getRegClass(OldReg
)))
183 MRI
->replaceRegWith(OldReg
, SingleValReg
);
184 MI
->eraseFromParent();
186 // The kill flags on OldReg and SingleValReg may no longer be correct.
187 MRI
->clearKillFlags(SingleValReg
);
194 // Check for dead PHI cycles.
196 if (IsDeadPHICycle(MI
, PHIsInCycle
)) {
197 for (MachineInstr
*PhiMI
: PHIsInCycle
) {
200 PhiMI
->eraseFromParent();