blob: 8441e49bbc66cfac1958cdd004140bfe6e51025f [file] [log] [blame]
Chris Lattnerbc40e892003-01-13 20:01:16 +00001//===-- PhiElimination.cpp - Eliminate PHI nodes by inserting copies ------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbc40e892003-01-13 20:01:16 +00009//
10// This pass eliminates machine instruction PHI nodes by inserting copy
11// instructions. This destroys SSA information, but is the desired input for
12// some register allocators.
13//
14//===----------------------------------------------------------------------===//
15
Misha Brukmand7a10c82005-05-05 23:45:17 +000016#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner0742b592004-02-23 18:38:20 +000017#include "llvm/CodeGen/Passes.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000021#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000022#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/STLExtras.h"
Chris Lattner6db07562005-10-03 07:22:07 +000025#include "llvm/ADT/Statistic.h"
Chris Lattner53a79aa2005-10-03 04:47:08 +000026#include <set>
Chris Lattner6db07562005-10-03 07:22:07 +000027#include <algorithm>
Chris Lattner0742b592004-02-23 18:38:20 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattnerbc40e892003-01-13 20:01:16 +000030namespace {
Chris Lattner6db07562005-10-03 07:22:07 +000031 Statistic<> NumAtomic("phielim", "Number of atomic phis lowered");
32 Statistic<> NumSimple("phielim", "Number of simple phis lowered");
33
Chris Lattnerbc40e892003-01-13 20:01:16 +000034 struct PNE : public MachineFunctionPass {
35 bool runOnMachineFunction(MachineFunction &Fn) {
36 bool Changed = false;
37
38 // Eliminate PHI instructions by inserting copies into predecessor blocks.
Chris Lattnerbc40e892003-01-13 20:01:16 +000039 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Misha Brukmandedf2bd2005-04-22 04:01:18 +000040 Changed |= EliminatePHINodes(Fn, *I);
Chris Lattnerbc40e892003-01-13 20:01:16 +000041
Chris Lattnerbc40e892003-01-13 20:01:16 +000042 return Changed;
43 }
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addPreserved<LiveVariables>();
47 MachineFunctionPass::getAnalysisUsage(AU);
48 }
49
50 private:
51 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
52 /// in predecessor basic blocks.
53 ///
54 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
Chris Lattner53a79aa2005-10-03 04:47:08 +000055 void LowerAtomicPHINode(MachineBasicBlock &MBB,
56 MachineBasicBlock::iterator AfterPHIsIt,
Chris Lattner6db07562005-10-03 07:22:07 +000057 DenseMap<unsigned, VirtReg2IndexFunctor> &VUC);
Chris Lattnerbc40e892003-01-13 20:01:16 +000058 };
59
60 RegisterPass<PNE> X("phi-node-elimination",
Misha Brukmandedf2bd2005-04-22 04:01:18 +000061 "Eliminate PHI nodes for register allocation");
Chris Lattnerbc40e892003-01-13 20:01:16 +000062}
63
Brian Gaeked0fde302003-11-11 22:41:34 +000064
Chris Lattner0742b592004-02-23 18:38:20 +000065const PassInfo *llvm::PHIEliminationID = X.getPassInfo();
Chris Lattnerbc40e892003-01-13 20:01:16 +000066
67/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
68/// predecessor basic blocks.
69///
70bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000071 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattner53a79aa2005-10-03 04:47:08 +000072 return false; // Quick exit for basic blocks without PHIs.
Chris Lattnerbc40e892003-01-13 20:01:16 +000073
Chris Lattner80e20eb2004-05-10 19:06:37 +000074 // VRegPHIUseCount - Keep track of the number of times each virtual register
Chris Lattnerbee88722004-05-12 21:47:57 +000075 // is used by PHI nodes in successors of this block.
Chris Lattner8abf6932004-05-10 19:17:36 +000076 DenseMap<unsigned, VirtReg2IndexFunctor> VRegPHIUseCount;
77 VRegPHIUseCount.grow(MF.getSSARegMap()->getLastVirtReg());
Chris Lattner80e20eb2004-05-10 19:06:37 +000078
Chris Lattnerbee88722004-05-12 21:47:57 +000079 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin(),
80 E = MBB.pred_end(); PI != E; ++PI)
81 for (MachineBasicBlock::succ_iterator SI = (*PI)->succ_begin(),
Chris Lattner6db07562005-10-03 07:22:07 +000082 E = (*PI)->succ_end(); SI != E; ++SI)
83 for (MachineBasicBlock::iterator BBI = (*SI)->begin(), E = (*SI)->end();
84 BBI != E && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
85 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
86 VRegPHIUseCount[BBI->getOperand(i).getReg()]++;
87
Chris Lattner791f8962004-05-10 18:47:18 +000088 // Get an iterator to the first instruction after the last PHI node (this may
Chris Lattner53a79aa2005-10-03 04:47:08 +000089 // also be the end of the basic block).
Chris Lattner791f8962004-05-10 18:47:18 +000090 MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
91 while (AfterPHIsIt != MBB.end() &&
Chris Lattnerbee88722004-05-12 21:47:57 +000092 AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
Chris Lattner791f8962004-05-10 18:47:18 +000093 ++AfterPHIsIt; // Skip over all of the PHI nodes...
94
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000095 while (MBB.front().getOpcode() == TargetInstrInfo::PHI) {
Chris Lattner6db07562005-10-03 07:22:07 +000096 LowerAtomicPHINode(MBB, AfterPHIsIt, VRegPHIUseCount);
Chris Lattner53a79aa2005-10-03 04:47:08 +000097 }
98 return true;
99}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000100
Chris Lattner53a79aa2005-10-03 04:47:08 +0000101/// LowerAtomicPHINode - Lower the PHI node at the top of the specified block,
102/// under the assuption that it needs to be lowered in a way that supports
103/// atomic execution of PHIs. This lowering method is always correct all of the
104/// time.
105void PNE::LowerAtomicPHINode(MachineBasicBlock &MBB,
106 MachineBasicBlock::iterator AfterPHIsIt,
Chris Lattner6db07562005-10-03 07:22:07 +0000107 DenseMap<unsigned, VirtReg2IndexFunctor> &VRegPHIUseCount) {
Chris Lattner53a79aa2005-10-03 04:47:08 +0000108 // Unlink the PHI node from the basic block, but don't delete the PHI yet.
109 MachineInstr *MPhi = MBB.remove(MBB.begin());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000110
Chris Lattner53a79aa2005-10-03 04:47:08 +0000111 unsigned DestReg = MPhi->getOperand(0).getReg();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000112
Chris Lattner53a79aa2005-10-03 04:47:08 +0000113 // Create a new register for the incoming PHI arguments/
114 MachineFunction &MF = *MBB.getParent();
115 const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(DestReg);
116 unsigned IncomingReg = MF.getSSARegMap()->createVirtualRegister(RC);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000117
Chris Lattner53a79aa2005-10-03 04:47:08 +0000118 // Insert a register to register copy in the top of the current block (but
119 // after any remaining phi nodes) which copies the new incoming register
120 // into the phi node destination.
121 //
122 const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
123 RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC);
124
125 // Update live variable information if there is any...
126 LiveVariables *LV = getAnalysisToUpdate<LiveVariables>();
127 if (LV) {
128 MachineInstr *PHICopy = prior(AfterPHIsIt);
129
130 // Add information to LiveVariables to know that the incoming value is
131 // killed. Note that because the value is defined in several places (once
132 // each for each incoming block), the "def" block and instruction fields
133 // for the VarInfo is not filled in.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000134 //
Chris Lattner53a79aa2005-10-03 04:47:08 +0000135 LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattner53a79aa2005-10-03 04:47:08 +0000137 // Since we are going to be deleting the PHI node, if it is the last use
138 // of any registers, or if the value itself is dead, we need to move this
139 // information over to the new copy we just inserted.
140 //
141 LV->removeVirtualRegistersKilled(MPhi);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000142
Chris Lattner6db07562005-10-03 07:22:07 +0000143 // If the result is dead, update LV.
144 if (LV->RegisterDefIsDead(MPhi, DestReg)) {
145 LV->addVirtualRegisterDead(DestReg, PHICopy);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000146 LV->removeVirtualRegistersDead(MPhi);
147 }
Chris Lattner172c3622006-01-04 06:47:48 +0000148
149 // Realize that the destination register is defined by the PHI copy now, not
150 // the PHI itself.
151 LV->getVarInfo(DestReg).DefInst = PHICopy;
Chris Lattner53a79aa2005-10-03 04:47:08 +0000152 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000153
Chris Lattner53a79aa2005-10-03 04:47:08 +0000154 // Adjust the VRegPHIUseCount map to account for the removal of this PHI
155 // node.
Chris Lattner6db07562005-10-03 07:22:07 +0000156 unsigned NumPreds = (MPhi->getNumOperands()-1)/2;
Chris Lattner53a79aa2005-10-03 04:47:08 +0000157 for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
Chris Lattner6db07562005-10-03 07:22:07 +0000158 VRegPHIUseCount[MPhi->getOperand(i).getReg()] -= NumPreds;
Chris Lattner572c7702003-05-12 14:28:28 +0000159
Chris Lattner53a79aa2005-10-03 04:47:08 +0000160 // Now loop over all of the incoming arguments, changing them to copy into
161 // the IncomingReg register in the corresponding predecessor basic block.
162 //
Chris Lattner6db07562005-10-03 07:22:07 +0000163 std::set<MachineBasicBlock*> MBBsInsertedInto;
Chris Lattner53a79aa2005-10-03 04:47:08 +0000164 for (int i = MPhi->getNumOperands() - 1; i >= 2; i-=2) {
Chris Lattner6db07562005-10-03 07:22:07 +0000165 unsigned SrcReg = MPhi->getOperand(i-1).getReg();
166 assert(MRegisterInfo::isVirtualRegister(SrcReg) &&
167 "Machine PHI Operands must all be virtual registers!");
Chris Lattner53a79aa2005-10-03 04:47:08 +0000168
169 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
170 // source path the PHI.
171 MachineBasicBlock &opBlock = *MPhi->getOperand(i).getMachineBasicBlock();
172
Chris Lattner53a79aa2005-10-03 04:47:08 +0000173 // Check to make sure we haven't already emitted the copy for this block.
174 // This can happen because PHI nodes may have multiple entries for the
Chris Lattner6db07562005-10-03 07:22:07 +0000175 // same basic block.
176 if (!MBBsInsertedInto.insert(&opBlock).second)
177 continue; // If the copy has already been emitted, we're done.
178
179 // Get an iterator pointing to the first terminator in the block (or end()).
180 // This is the point where we can insert a copy if we'd like to.
181 MachineBasicBlock::iterator I = opBlock.getFirstTerminator();
182
183 // Insert the copy.
184 RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000185
Chris Lattner6db07562005-10-03 07:22:07 +0000186 // Now update live variable information if we have it. Otherwise we're done
187 if (!LV) continue;
188
189 // We want to be able to insert a kill of the register if this PHI
190 // (aka, the copy we just inserted) is the last use of the source
191 // value. Live variable analysis conservatively handles this by
192 // saying that the value is live until the end of the block the PHI
193 // entry lives in. If the value really is dead at the PHI copy, there
194 // will be no successor blocks which have the value live-in.
195 //
196 // Check to see if the copy is the last use, and if so, update the
197 // live variables information so that it knows the copy source
198 // instruction kills the incoming value.
199 //
200 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
201
202 // Loop over all of the successors of the basic block, checking to see
203 // if the value is either live in the block, or if it is killed in the
204 // block. Also check to see if this register is in use by another PHI
205 // node which has not yet been eliminated. If so, it will be killed
206 // at an appropriate point later.
207 //
208
209 // Is it used by any PHI instructions in this block?
210 bool ValueIsLive = VRegPHIUseCount[SrcReg] != 0;
211
212 std::vector<MachineBasicBlock*> OpSuccBlocks;
213
214 // Otherwise, scan successors, including the BB the PHI node lives in.
215 for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
216 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
217 MachineBasicBlock *SuccMBB = *SI;
218
219 // Is it alive in this successor?
220 unsigned SuccIdx = SuccMBB->getNumber();
221 if (SuccIdx < InRegVI.AliveBlocks.size() &&
222 InRegVI.AliveBlocks[SuccIdx]) {
223 ValueIsLive = true;
224 break;
Chris Lattner927ce5d2003-05-12 03:55:21 +0000225 }
Chris Lattner6db07562005-10-03 07:22:07 +0000226
227 OpSuccBlocks.push_back(SuccMBB);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000228 }
229
Chris Lattner6db07562005-10-03 07:22:07 +0000230 // Check to see if this value is live because there is a use in a successor
231 // that kills it.
232 if (!ValueIsLive) {
233 switch (OpSuccBlocks.size()) {
234 case 1: {
235 MachineBasicBlock *MBB = OpSuccBlocks[0];
236 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
237 if (InRegVI.Kills[i]->getParent() == MBB) {
Chris Lattner53a79aa2005-10-03 04:47:08 +0000238 ValueIsLive = true;
239 break;
240 }
Chris Lattner6db07562005-10-03 07:22:07 +0000241 break;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000242 }
Chris Lattner6db07562005-10-03 07:22:07 +0000243 case 2: {
244 MachineBasicBlock *MBB1 = OpSuccBlocks[0], *MBB2 = OpSuccBlocks[1];
245 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
246 if (InRegVI.Kills[i]->getParent() == MBB1 ||
247 InRegVI.Kills[i]->getParent() == MBB2) {
248 ValueIsLive = true;
249 break;
250 }
251 break;
252 }
253 default:
254 std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
255 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
256 if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
257 InRegVI.Kills[i]->getParent())) {
258 ValueIsLive = true;
259 break;
260 }
261 }
262 }
263
264 // Okay, if we now know that the value is not live out of the block,
265 // we can add a kill marker to the copy we inserted saying that it
266 // kills the incoming value!
267 //
268 if (!ValueIsLive) {
269 MachineBasicBlock::iterator Prev = prior(I);
270 LV->addVirtualRegisterKilled(SrcReg, Prev);
271
272 // This vreg no longer lives all of the way through opBlock.
273 unsigned opBlockNum = opBlock.getNumber();
274 if (opBlockNum < InRegVI.AliveBlocks.size())
275 InRegVI.AliveBlocks[opBlockNum] = false;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000276 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000277 }
Chris Lattner53a79aa2005-10-03 04:47:08 +0000278
279 // Really delete the PHI instruction now!
280 delete MPhi;
Chris Lattner6db07562005-10-03 07:22:07 +0000281 ++NumAtomic;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000282}