1 //===-- OptimizePHIs.cpp - Optimize machine instruction PHIs --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass optimizes machine instruction PHIs to take advantage of
11 // opportunities created during DAG legalization.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "phi-opt"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/Target/TargetInstrInfo.h"
26 STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
27 STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
30 class OptimizePHIs : public MachineFunctionPass {
31 MachineRegisterInfo *MRI;
32 const TargetInstrInfo *TII;
35 static char ID; // Pass identification
36 OptimizePHIs() : MachineFunctionPass(ID) {
37 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
40 virtual bool runOnMachineFunction(MachineFunction &MF);
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 MachineFunctionPass::getAnalysisUsage(AU);
48 typedef SmallPtrSet<MachineInstr*, 16> InstrSet;
49 typedef SmallPtrSetIterator<MachineInstr*> InstrSetIterator;
51 bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
52 InstrSet &PHIsInCycle);
53 bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
54 bool OptimizeBB(MachineBasicBlock &MBB);
58 char OptimizePHIs::ID = 0;
59 char &llvm::OptimizePHIsID = OptimizePHIs::ID;
60 INITIALIZE_PASS(OptimizePHIs, "opt-phis",
61 "Optimize machine instruction PHIs", false, false)
63 bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
64 MRI = &Fn.getRegInfo();
65 TII = Fn.getTarget().getInstrInfo();
67 // Find dead PHI cycles and PHI cycles that can be replaced by a single
68 // value. InstCombine does these optimizations, but DAG legalization may
69 // introduce new opportunities, e.g., when i64 values are split up for
72 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
73 Changed |= OptimizeBB(*I);
78 /// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
79 /// are copies of SingleValReg, possibly via copies through other PHIs. If
80 /// SingleValReg is zero on entry, it is set to the register with the single
81 /// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
82 /// have been scanned.
83 bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
84 unsigned &SingleValReg,
85 InstrSet &PHIsInCycle) {
86 assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
87 unsigned DstReg = MI->getOperand(0).getReg();
89 // See if we already saw this register.
90 if (!PHIsInCycle.insert(MI))
93 // Don't scan crazily complex things.
94 if (PHIsInCycle.size() == 16)
97 // Scan the PHI operands.
98 for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
99 unsigned SrcReg = MI->getOperand(i).getReg();
100 if (SrcReg == DstReg)
102 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
104 // Skip over register-to-register moves.
105 if (SrcMI && SrcMI->isCopy() &&
106 !SrcMI->getOperand(0).getSubReg() &&
107 !SrcMI->getOperand(1).getSubReg() &&
108 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
109 SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
113 if (SrcMI->isPHI()) {
114 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
117 // Fail if there is more than one non-phi/non-move register.
118 if (SingleValReg != 0)
120 SingleValReg = SrcReg;
126 /// IsDeadPHICycle - Check if the register defined by a PHI is only used by
127 /// other PHIs in a cycle.
128 bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
129 assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
130 unsigned DstReg = MI->getOperand(0).getReg();
131 assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
132 "PHI destination is not a virtual register");
134 // See if we already saw this register.
135 if (!PHIsInCycle.insert(MI))
138 // Don't scan crazily complex things.
139 if (PHIsInCycle.size() == 16)
142 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(DstReg),
143 E = MRI->use_end(); I != E; ++I) {
144 MachineInstr *UseMI = &*I;
145 if (!UseMI->isPHI() || !IsDeadPHICycle(UseMI, PHIsInCycle))
152 /// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
154 bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
155 bool Changed = false;
156 for (MachineBasicBlock::iterator
157 MII = MBB.begin(), E = MBB.end(); MII != E; ) {
158 MachineInstr *MI = &*MII++;
162 // Check for single-value PHI cycles.
163 unsigned SingleValReg = 0;
164 InstrSet PHIsInCycle;
165 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
167 unsigned OldReg = MI->getOperand(0).getReg();
168 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
171 MRI->replaceRegWith(OldReg, SingleValReg);
172 MI->eraseFromParent();
178 // Check for dead PHI cycles.
180 if (IsDeadPHICycle(MI, PHIsInCycle)) {
181 for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
183 MachineInstr *PhiMI = *PI;
186 PhiMI->eraseFromParent();