blob: bf01df905b5f2f43dcb48323bd0903135e120373 [file] [log] [blame]
Bob Wilsonfe61fb12010-02-12 01:30:21 +00001//===-- OptimizePHIs.cpp - Optimize machine instruction PHIs --------------===//
2//
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
15#define DEBUG_TYPE "phi-opt"
16#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/Statistic.h"
Bob Wilsonfe61fb12010-02-12 01:30:21 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilsonfe61fb12010-02-12 01:30:21 +000022#include "llvm/Function.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsonfe61fb12010-02-12 01:30:21 +000024using namespace llvm;
25
26STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
Bob Wilsonbf9b2212010-02-13 00:31:44 +000027STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
Bob Wilsonfe61fb12010-02-12 01:30:21 +000028
29namespace {
30 class OptimizePHIs : public MachineFunctionPass {
31 MachineRegisterInfo *MRI;
32 const TargetInstrInfo *TII;
33
34 public:
35 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000036 OptimizePHIs() : MachineFunctionPass(ID) {
37 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
38 }
Bob Wilsonfe61fb12010-02-12 01:30:21 +000039
40 virtual bool runOnMachineFunction(MachineFunction &MF);
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesCFG();
44 MachineFunctionPass::getAnalysisUsage(AU);
45 }
46
47 private:
Bob Wilsonbf9b2212010-02-13 00:31:44 +000048 typedef SmallPtrSet<MachineInstr*, 16> InstrSet;
49 typedef SmallPtrSetIterator<MachineInstr*> InstrSetIterator;
50
51 bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
52 InstrSet &PHIsInCycle);
53 bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
54 bool OptimizeBB(MachineBasicBlock &MBB);
Bob Wilsonfe61fb12010-02-12 01:30:21 +000055 };
56}
57
58char OptimizePHIs::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000059char &llvm::OptimizePHIsID = OptimizePHIs::ID;
Owen Andersond13db2c2010-07-21 22:09:45 +000060INITIALIZE_PASS(OptimizePHIs, "opt-phis",
Owen Andersonce665bd2010-10-07 22:25:06 +000061 "Optimize machine instruction PHIs", false, false)
Bob Wilsonfe61fb12010-02-12 01:30:21 +000062
Bob Wilsonfe61fb12010-02-12 01:30:21 +000063bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
64 MRI = &Fn.getRegInfo();
65 TII = Fn.getTarget().getInstrInfo();
66
Bob Wilsonbf9b2212010-02-13 00:31:44 +000067 // 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
70 // 32-bit targets.
Bob Wilsonfe61fb12010-02-12 01:30:21 +000071 bool Changed = false;
72 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Bob Wilsonbf9b2212010-02-13 00:31:44 +000073 Changed |= OptimizeBB(*I);
Bob Wilsonfe61fb12010-02-12 01:30:21 +000074
75 return Changed;
76}
77
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
Bob Wilsonbf9b2212010-02-13 00:31:44 +000081/// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
Bob Wilsonfe61fb12010-02-12 01:30:21 +000082/// have been scanned.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000083bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
Bob Wilsonfe61fb12010-02-12 01:30:21 +000084 unsigned &SingleValReg,
Bob Wilsonbf9b2212010-02-13 00:31:44 +000085 InstrSet &PHIsInCycle) {
Bob Wilsonfe61fb12010-02-12 01:30:21 +000086 assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
87 unsigned DstReg = MI->getOperand(0).getReg();
88
89 // See if we already saw this register.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000090 if (!PHIsInCycle.insert(MI))
Bob Wilsonfe61fb12010-02-12 01:30:21 +000091 return true;
92
93 // Don't scan crazily complex things.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000094 if (PHIsInCycle.size() == 16)
Bob Wilsonfe61fb12010-02-12 01:30:21 +000095 return false;
96
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)
101 continue;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000102 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000103
104 // Skip over register-to-register moves.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000105 if (SrcMI && SrcMI->isCopy() &&
106 !SrcMI->getOperand(0).getSubReg() &&
107 !SrcMI->getOperand(1).getSubReg() &&
108 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000109 SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000110 if (!SrcMI)
111 return false;
112
113 if (SrcMI->isPHI()) {
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000114 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000115 return false;
116 } else {
117 // Fail if there is more than one non-phi/non-move register.
118 if (SingleValReg != 0)
119 return false;
120 SingleValReg = SrcReg;
121 }
122 }
123 return true;
124}
125
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000126/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
127/// other PHIs in a cycle.
128bool 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");
133
134 // See if we already saw this register.
135 if (!PHIsInCycle.insert(MI))
136 return true;
137
138 // Don't scan crazily complex things.
139 if (PHIsInCycle.size() == 16)
140 return false;
141
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))
146 return false;
147 }
148
149 return true;
150}
151
152/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
153/// a single value.
154bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000155 bool Changed = false;
156 for (MachineBasicBlock::iterator
157 MII = MBB.begin(), E = MBB.end(); MII != E; ) {
158 MachineInstr *MI = &*MII++;
159 if (!MI->isPHI())
160 break;
161
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000162 // Check for single-value PHI cycles.
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000163 unsigned SingleValReg = 0;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000164 InstrSet PHIsInCycle;
165 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000166 SingleValReg != 0) {
Cameron Zwarich419eb362011-10-17 21:54:46 +0000167 unsigned OldReg = MI->getOperand(0).getReg();
168 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
169 continue;
170
171 MRI->replaceRegWith(OldReg, SingleValReg);
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000172 MI->eraseFromParent();
173 ++NumPHICycles;
174 Changed = true;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000175 continue;
176 }
177
178 // Check for dead PHI cycles.
179 PHIsInCycle.clear();
180 if (IsDeadPHICycle(MI, PHIsInCycle)) {
181 for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
182 PI != PE; ++PI) {
183 MachineInstr *PhiMI = *PI;
184 if (&*MII == PhiMI)
185 ++MII;
186 PhiMI->eraseFromParent();
187 }
188 ++NumDeadPHICycles;
189 Changed = true;
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000190 }
191 }
192 return Changed;
193}