blob: 95a2934afb40d42afeefa235a217e549a4161517 [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
Bob Wilsonfe61fb12010-02-12 01:30:21 +000015#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
17#include "llvm/ADT/Statistic.h"
Bob Wilsonfe61fb12010-02-12 01:30:21 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Function.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsonfe61fb12010-02-12 01:30:21 +000023using namespace llvm;
24
Stephen Hinesdce4a402014-05-29 02:49:00 -070025#define DEBUG_TYPE "phi-opt"
26
Bob Wilsonfe61fb12010-02-12 01:30:21 +000027STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
Bob Wilsonbf9b2212010-02-13 00:31:44 +000028STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
Bob Wilsonfe61fb12010-02-12 01:30:21 +000029
30namespace {
31 class OptimizePHIs : public MachineFunctionPass {
32 MachineRegisterInfo *MRI;
33 const TargetInstrInfo *TII;
34
35 public:
36 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000037 OptimizePHIs() : MachineFunctionPass(ID) {
38 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
39 }
Bob Wilsonfe61fb12010-02-12 01:30:21 +000040
Stephen Hines36b56882014-04-23 16:57:46 -070041 bool runOnMachineFunction(MachineFunction &MF) override;
Bob Wilsonfe61fb12010-02-12 01:30:21 +000042
Stephen Hines36b56882014-04-23 16:57:46 -070043 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bob Wilsonfe61fb12010-02-12 01:30:21 +000044 AU.setPreservesCFG();
45 MachineFunctionPass::getAnalysisUsage(AU);
46 }
47
48 private:
Bob Wilsonbf9b2212010-02-13 00:31:44 +000049 typedef SmallPtrSet<MachineInstr*, 16> InstrSet;
50 typedef SmallPtrSetIterator<MachineInstr*> InstrSetIterator;
51
52 bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
53 InstrSet &PHIsInCycle);
54 bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
55 bool OptimizeBB(MachineBasicBlock &MBB);
Bob Wilsonfe61fb12010-02-12 01:30:21 +000056 };
57}
58
59char OptimizePHIs::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000060char &llvm::OptimizePHIsID = OptimizePHIs::ID;
Owen Andersond13db2c2010-07-21 22:09:45 +000061INITIALIZE_PASS(OptimizePHIs, "opt-phis",
Owen Andersonce665bd2010-10-07 22:25:06 +000062 "Optimize machine instruction PHIs", false, false)
Bob Wilsonfe61fb12010-02-12 01:30:21 +000063
Bob Wilsonfe61fb12010-02-12 01:30:21 +000064bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
Stephen Hines36b56882014-04-23 16:57:46 -070065 if (skipOptnoneFunction(*Fn.getFunction()))
66 return false;
67
Bob Wilsonfe61fb12010-02-12 01:30:21 +000068 MRI = &Fn.getRegInfo();
69 TII = Fn.getTarget().getInstrInfo();
70
Bob Wilsonbf9b2212010-02-13 00:31:44 +000071 // Find dead PHI cycles and PHI cycles that can be replaced by a single
72 // value. InstCombine does these optimizations, but DAG legalization may
73 // introduce new opportunities, e.g., when i64 values are split up for
74 // 32-bit targets.
Bob Wilsonfe61fb12010-02-12 01:30:21 +000075 bool Changed = false;
76 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Bob Wilsonbf9b2212010-02-13 00:31:44 +000077 Changed |= OptimizeBB(*I);
Bob Wilsonfe61fb12010-02-12 01:30:21 +000078
79 return Changed;
80}
81
82/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
83/// are copies of SingleValReg, possibly via copies through other PHIs. If
84/// SingleValReg is zero on entry, it is set to the register with the single
Bob Wilsonbf9b2212010-02-13 00:31:44 +000085/// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
Bob Wilsonfe61fb12010-02-12 01:30:21 +000086/// have been scanned.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000087bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
Bob Wilsonfe61fb12010-02-12 01:30:21 +000088 unsigned &SingleValReg,
Bob Wilsonbf9b2212010-02-13 00:31:44 +000089 InstrSet &PHIsInCycle) {
Bob Wilsonfe61fb12010-02-12 01:30:21 +000090 assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
91 unsigned DstReg = MI->getOperand(0).getReg();
92
93 // See if we already saw this register.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000094 if (!PHIsInCycle.insert(MI))
Bob Wilsonfe61fb12010-02-12 01:30:21 +000095 return true;
96
97 // Don't scan crazily complex things.
Bob Wilsonbf9b2212010-02-13 00:31:44 +000098 if (PHIsInCycle.size() == 16)
Bob Wilsonfe61fb12010-02-12 01:30:21 +000099 return false;
100
101 // Scan the PHI operands.
102 for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
103 unsigned SrcReg = MI->getOperand(i).getReg();
104 if (SrcReg == DstReg)
105 continue;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000106 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000107
108 // Skip over register-to-register moves.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000109 if (SrcMI && SrcMI->isCopy() &&
110 !SrcMI->getOperand(0).getSubReg() &&
111 !SrcMI->getOperand(1).getSubReg() &&
112 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000113 SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000114 if (!SrcMI)
115 return false;
116
117 if (SrcMI->isPHI()) {
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000118 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000119 return false;
120 } else {
121 // Fail if there is more than one non-phi/non-move register.
122 if (SingleValReg != 0)
123 return false;
124 SingleValReg = SrcReg;
125 }
126 }
127 return true;
128}
129
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000130/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
131/// other PHIs in a cycle.
132bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
133 assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
134 unsigned DstReg = MI->getOperand(0).getReg();
135 assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
136 "PHI destination is not a virtual register");
137
138 // See if we already saw this register.
139 if (!PHIsInCycle.insert(MI))
140 return true;
141
142 // Don't scan crazily complex things.
143 if (PHIsInCycle.size() == 16)
144 return false;
145
Stephen Hines36b56882014-04-23 16:57:46 -0700146 for (MachineInstr &UseMI : MRI->use_instructions(DstReg)) {
147 if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000148 return false;
149 }
150
151 return true;
152}
153
154/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
155/// a single value.
156bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000157 bool Changed = false;
158 for (MachineBasicBlock::iterator
159 MII = MBB.begin(), E = MBB.end(); MII != E; ) {
160 MachineInstr *MI = &*MII++;
161 if (!MI->isPHI())
162 break;
163
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000164 // Check for single-value PHI cycles.
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000165 unsigned SingleValReg = 0;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000166 InstrSet PHIsInCycle;
167 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000168 SingleValReg != 0) {
Cameron Zwarich419eb362011-10-17 21:54:46 +0000169 unsigned OldReg = MI->getOperand(0).getReg();
170 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
171 continue;
172
173 MRI->replaceRegWith(OldReg, SingleValReg);
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000174 MI->eraseFromParent();
175 ++NumPHICycles;
176 Changed = true;
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000177 continue;
178 }
179
180 // Check for dead PHI cycles.
181 PHIsInCycle.clear();
182 if (IsDeadPHICycle(MI, PHIsInCycle)) {
183 for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
184 PI != PE; ++PI) {
185 MachineInstr *PhiMI = *PI;
186 if (&*MII == PhiMI)
187 ++MII;
188 PhiMI->eraseFromParent();
189 }
190 ++NumDeadPHICycles;
191 Changed = true;
Bob Wilsonfe61fb12010-02-12 01:30:21 +0000192 }
193 }
194 return Changed;
195}