blob: a1042e720c37585c1299abe47c14d751a25091cd [file] [log] [blame]
Bob Wilson0827e042010-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 Wilson0827e042010-02-12 01:30:21 +000015#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
17#include "llvm/ADT/Statistic.h"
Bob Wilson0827e042010-02-12 01:30:21 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000023#include "llvm/Target/TargetSubtargetInfo.h"
Bob Wilson0827e042010-02-12 01:30:21 +000024using namespace llvm;
25
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026#define DEBUG_TYPE "phi-opt"
27
Bob Wilson0827e042010-02-12 01:30:21 +000028STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
Bob Wilson01abf8f2010-02-13 00:31:44 +000029STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
Bob Wilson0827e042010-02-12 01:30:21 +000030
31namespace {
32 class OptimizePHIs : public MachineFunctionPass {
33 MachineRegisterInfo *MRI;
34 const TargetInstrInfo *TII;
35
36 public:
37 static char ID; // Pass identification
Owen Anderson6c18d1a2010-10-19 17:21:58 +000038 OptimizePHIs() : MachineFunctionPass(ID) {
39 initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
40 }
Bob Wilson0827e042010-02-12 01:30:21 +000041
Craig Topper4584cd52014-03-07 09:26:03 +000042 bool runOnMachineFunction(MachineFunction &MF) override;
Bob Wilson0827e042010-02-12 01:30:21 +000043
Craig Topper4584cd52014-03-07 09:26:03 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bob Wilson0827e042010-02-12 01:30:21 +000045 AU.setPreservesCFG();
46 MachineFunctionPass::getAnalysisUsage(AU);
47 }
48
49 private:
Bob Wilson01abf8f2010-02-13 00:31:44 +000050 typedef SmallPtrSet<MachineInstr*, 16> InstrSet;
51 typedef SmallPtrSetIterator<MachineInstr*> InstrSetIterator;
52
53 bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
54 InstrSet &PHIsInCycle);
55 bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
56 bool OptimizeBB(MachineBasicBlock &MBB);
Bob Wilson0827e042010-02-12 01:30:21 +000057 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000058}
Bob Wilson0827e042010-02-12 01:30:21 +000059
60char OptimizePHIs::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000061char &llvm::OptimizePHIsID = OptimizePHIs::ID;
Owen Andersona57b97e2010-07-21 22:09:45 +000062INITIALIZE_PASS(OptimizePHIs, "opt-phis",
Owen Andersondf7a4f22010-10-07 22:25:06 +000063 "Optimize machine instruction PHIs", false, false)
Bob Wilson0827e042010-02-12 01:30:21 +000064
Bob Wilson0827e042010-02-12 01:30:21 +000065bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
Paul Robinson7c99ec52014-03-31 17:43:35 +000066 if (skipOptnoneFunction(*Fn.getFunction()))
67 return false;
68
Bob Wilson0827e042010-02-12 01:30:21 +000069 MRI = &Fn.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000070 TII = Fn.getSubtarget().getInstrInfo();
Bob Wilson0827e042010-02-12 01:30:21 +000071
Bob Wilson01abf8f2010-02-13 00:31:44 +000072 // Find dead PHI cycles and PHI cycles that can be replaced by a single
73 // value. InstCombine does these optimizations, but DAG legalization may
74 // introduce new opportunities, e.g., when i64 values are split up for
75 // 32-bit targets.
Bob Wilson0827e042010-02-12 01:30:21 +000076 bool Changed = false;
77 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Bob Wilson01abf8f2010-02-13 00:31:44 +000078 Changed |= OptimizeBB(*I);
Bob Wilson0827e042010-02-12 01:30:21 +000079
80 return Changed;
81}
82
83/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
84/// are copies of SingleValReg, possibly via copies through other PHIs. If
85/// SingleValReg is zero on entry, it is set to the register with the single
Bob Wilson01abf8f2010-02-13 00:31:44 +000086/// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
Bob Wilson0827e042010-02-12 01:30:21 +000087/// have been scanned.
Bob Wilson01abf8f2010-02-13 00:31:44 +000088bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
Bob Wilson0827e042010-02-12 01:30:21 +000089 unsigned &SingleValReg,
Bob Wilson01abf8f2010-02-13 00:31:44 +000090 InstrSet &PHIsInCycle) {
Bob Wilson0827e042010-02-12 01:30:21 +000091 assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
92 unsigned DstReg = MI->getOperand(0).getReg();
93
94 // See if we already saw this register.
David Blaikie70573dc2014-11-19 07:49:26 +000095 if (!PHIsInCycle.insert(MI).second)
Bob Wilson0827e042010-02-12 01:30:21 +000096 return true;
97
98 // Don't scan crazily complex things.
Bob Wilson01abf8f2010-02-13 00:31:44 +000099 if (PHIsInCycle.size() == 16)
Bob Wilson0827e042010-02-12 01:30:21 +0000100 return false;
101
102 // Scan the PHI operands.
103 for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
104 unsigned SrcReg = MI->getOperand(i).getReg();
105 if (SrcReg == DstReg)
106 continue;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000107 MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
Bob Wilson0827e042010-02-12 01:30:21 +0000108
109 // Skip over register-to-register moves.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000110 if (SrcMI && SrcMI->isCopy() &&
111 !SrcMI->getOperand(0).getSubReg() &&
112 !SrcMI->getOperand(1).getSubReg() &&
113 TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000114 SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
Bob Wilson0827e042010-02-12 01:30:21 +0000115 if (!SrcMI)
116 return false;
117
118 if (SrcMI->isPHI()) {
Bob Wilson01abf8f2010-02-13 00:31:44 +0000119 if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
Bob Wilson0827e042010-02-12 01:30:21 +0000120 return false;
121 } else {
122 // Fail if there is more than one non-phi/non-move register.
123 if (SingleValReg != 0)
124 return false;
125 SingleValReg = SrcReg;
126 }
127 }
128 return true;
129}
130
Bob Wilson01abf8f2010-02-13 00:31:44 +0000131/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
132/// other PHIs in a cycle.
133bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
134 assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
135 unsigned DstReg = MI->getOperand(0).getReg();
136 assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
137 "PHI destination is not a virtual register");
138
139 // See if we already saw this register.
David Blaikie70573dc2014-11-19 07:49:26 +0000140 if (!PHIsInCycle.insert(MI).second)
Bob Wilson01abf8f2010-02-13 00:31:44 +0000141 return true;
142
143 // Don't scan crazily complex things.
144 if (PHIsInCycle.size() == 16)
145 return false;
146
Owen Andersonb36376e2014-03-17 19:36:09 +0000147 for (MachineInstr &UseMI : MRI->use_instructions(DstReg)) {
148 if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
Bob Wilson01abf8f2010-02-13 00:31:44 +0000149 return false;
150 }
151
152 return true;
153}
154
155/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
156/// a single value.
157bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
Bob Wilson0827e042010-02-12 01:30:21 +0000158 bool Changed = false;
159 for (MachineBasicBlock::iterator
160 MII = MBB.begin(), E = MBB.end(); MII != E; ) {
161 MachineInstr *MI = &*MII++;
162 if (!MI->isPHI())
163 break;
164
Bob Wilson01abf8f2010-02-13 00:31:44 +0000165 // Check for single-value PHI cycles.
Bob Wilson0827e042010-02-12 01:30:21 +0000166 unsigned SingleValReg = 0;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000167 InstrSet PHIsInCycle;
168 if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
Bob Wilson0827e042010-02-12 01:30:21 +0000169 SingleValReg != 0) {
Cameron Zwarichd85bc102011-10-17 21:54:46 +0000170 unsigned OldReg = MI->getOperand(0).getReg();
171 if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
172 continue;
173
174 MRI->replaceRegWith(OldReg, SingleValReg);
Bob Wilson0827e042010-02-12 01:30:21 +0000175 MI->eraseFromParent();
176 ++NumPHICycles;
177 Changed = true;
Bob Wilson01abf8f2010-02-13 00:31:44 +0000178 continue;
179 }
180
181 // Check for dead PHI cycles.
182 PHIsInCycle.clear();
183 if (IsDeadPHICycle(MI, PHIsInCycle)) {
184 for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
185 PI != PE; ++PI) {
186 MachineInstr *PhiMI = *PI;
187 if (&*MII == PhiMI)
188 ++MII;
189 PhiMI->eraseFromParent();
190 }
191 ++NumDeadPHICycles;
192 Changed = true;
Bob Wilson0827e042010-02-12 01:30:21 +0000193 }
194 }
195 return Changed;
196}