blob: 56cb6735272b888fe5b9584e29137ba44f0adf40 [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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/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
Stephen Hines36b56882014-04-23 16:57:46 -070040 bool runOnMachineFunction(MachineFunction &MF) override;
Bob Wilsonfe61fb12010-02-12 01:30:21 +000041
Stephen Hines36b56882014-04-23 16:57:46 -070042 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bob Wilsonfe61fb12010-02-12 01:30:21 +000043 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) {
Stephen Hines36b56882014-04-23 16:57:46 -070064 if (skipOptnoneFunction(*Fn.getFunction()))
65 return false;
66
Bob Wilsonfe61fb12010-02-12 01:30:21 +000067 MRI = &Fn.getRegInfo();
68 TII = Fn.getTarget().getInstrInfo();
69
Bob Wilsonbf9b2212010-02-13 00:31:44 +000070 // Find dead PHI cycles and PHI cycles that can be replaced by a single
71 // value. InstCombine does these optimizations, but DAG legalization may
72 // introduce new opportunities, e.g., when i64 values are split up for
73 // 32-bit targets.
Bob Wilsonfe61fb12010-02-12 01:30:21 +000074 bool Changed = false;
75 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Bob Wilsonbf9b2212010-02-13 00:31:44 +000076 Changed |= OptimizeBB(*I);
Bob Wilsonfe61fb12010-02-12 01:30:21 +000077
78 return Changed;
79}
80
81/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
82/// are copies of SingleValReg, possibly via copies through other PHIs. If
83/// SingleValReg is zero on entry, it is set to the register with the single
Bob Wilsonbf9b2212010-02-13 00:31:44 +000084/// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
Bob Wilsonfe61fb12010-02-12 01:30:21 +000085/// have been scanned.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000086bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
Bob Wilsonfe61fb12010-02-12 01:30:21 +000087 unsigned &SingleValReg,
Bob Wilsonbf9b2212010-02-13 00:31:44 +000088 InstrSet &PHIsInCycle) {
Bob Wilsonfe61fb12010-02-12 01:30:21 +000089 assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
90 unsigned DstReg = MI->getOperand(0).getReg();
91
92 // See if we already saw this register.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000093 if (!PHIsInCycle.insert(MI))
Bob Wilsonfe61fb12010-02-12 01:30:21 +000094 return true;
95
96 // Don't scan crazily complex things.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000097 if (PHIsInCycle.size() == 16)
Bob Wilsonfe61fb12010-02-12 01:30:21 +000098 return false;
99
100 // Scan the PHI operands.
101 for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
102 unsigned SrcReg = MI->getOperand(i).getReg();
103 if (SrcReg == DstReg)
104 continue;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000105 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000106
107 // Skip over register-to-register moves.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000108 if (SrcMI && SrcMI->isCopy() &&
109 !SrcMI->getOperand(0).getSubReg() &&
110 !SrcMI->getOperand(1).getSubReg() &&
111 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000112 SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000113 if (!SrcMI)
114 return false;
115
116 if (SrcMI->isPHI()) {
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000117 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000118 return false;
119 } else {
120 // Fail if there is more than one non-phi/non-move register.
121 if (SingleValReg != 0)
122 return false;
123 SingleValReg = SrcReg;
124 }
125 }
126 return true;
127}
128
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000129/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
130/// other PHIs in a cycle.
131bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
132 assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
133 unsigned DstReg = MI->getOperand(0).getReg();
134 assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
135 "PHI destination is not a virtual register");
136
137 // See if we already saw this register.
138 if (!PHIsInCycle.insert(MI))
139 return true;
140
141 // Don't scan crazily complex things.
142 if (PHIsInCycle.size() == 16)
143 return false;
144
Stephen Hines36b56882014-04-23 16:57:46 -0700145 for (MachineInstr &UseMI : MRI->use_instructions(DstReg)) {
146 if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000147 return false;
148 }
149
150 return true;
151}
152
153/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
154/// a single value.
155bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000156 bool Changed = false;
157 for (MachineBasicBlock::iterator
158 MII = MBB.begin(), E = MBB.end(); MII != E; ) {
159 MachineInstr *MI = &*MII++;
160 if (!MI->isPHI())
161 break;
162
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000163 // Check for single-value PHI cycles.
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000164 unsigned SingleValReg = 0;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000165 InstrSet PHIsInCycle;
166 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000167 SingleValReg != 0) {
Cameron Zwarich419eb362011-10-17 21:54:46 +0000168 unsigned OldReg = MI->getOperand(0).getReg();
169 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
170 continue;
171
172 MRI->replaceRegWith(OldReg, SingleValReg);
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000173 MI->eraseFromParent();
174 ++NumPHICycles;
175 Changed = true;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000176 continue;
177 }
178
179 // Check for dead PHI cycles.
180 PHIsInCycle.clear();
181 if (IsDeadPHICycle(MI, PHIsInCycle)) {
182 for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
183 PI != PE; ++PI) {
184 MachineInstr *PhiMI = *PI;
185 if (&*MII == PhiMI)
186 ++MII;
187 PhiMI->eraseFromParent();
188 }
189 ++NumDeadPHICycles;
190 Changed = true;
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000191 }
192 }
193 return Changed;
194}