blob: 80c884dc9ae77f9c58265dd33f6b917e4385cc87 [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 Lattner53a79aa2005-10-03 04:47:08 +000025#include <set>
Chris Lattner0742b592004-02-23 18:38:20 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerbc40e892003-01-13 20:01:16 +000028namespace {
29 struct PNE : public MachineFunctionPass {
30 bool runOnMachineFunction(MachineFunction &Fn) {
31 bool Changed = false;
32
33 // Eliminate PHI instructions by inserting copies into predecessor blocks.
Chris Lattnerbc40e892003-01-13 20:01:16 +000034 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Misha Brukmandedf2bd2005-04-22 04:01:18 +000035 Changed |= EliminatePHINodes(Fn, *I);
Chris Lattnerbc40e892003-01-13 20:01:16 +000036
Chris Lattnerbc40e892003-01-13 20:01:16 +000037 return Changed;
38 }
39
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.addPreserved<LiveVariables>();
42 MachineFunctionPass::getAnalysisUsage(AU);
43 }
44
45 private:
46 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
47 /// in predecessor basic blocks.
48 ///
49 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
Chris Lattner53a79aa2005-10-03 04:47:08 +000050 void LowerAtomicPHINode(MachineBasicBlock &MBB,
51 MachineBasicBlock::iterator AfterPHIsIt,
52 DenseMap<unsigned, VirtReg2IndexFunctor> &VUC,
53 unsigned BBIsSuccOfPreds);
Chris Lattnerbc40e892003-01-13 20:01:16 +000054 };
55
56 RegisterPass<PNE> X("phi-node-elimination",
Misha Brukmandedf2bd2005-04-22 04:01:18 +000057 "Eliminate PHI nodes for register allocation");
Chris Lattnerbc40e892003-01-13 20:01:16 +000058}
59
Brian Gaeked0fde302003-11-11 22:41:34 +000060
Chris Lattner0742b592004-02-23 18:38:20 +000061const PassInfo *llvm::PHIEliminationID = X.getPassInfo();
Chris Lattnerbc40e892003-01-13 20:01:16 +000062
63/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
64/// predecessor basic blocks.
65///
66bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000067 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattner53a79aa2005-10-03 04:47:08 +000068 return false; // Quick exit for basic blocks without PHIs.
Chris Lattnerbc40e892003-01-13 20:01:16 +000069
Chris Lattner80e20eb2004-05-10 19:06:37 +000070 // VRegPHIUseCount - Keep track of the number of times each virtual register
Chris Lattnerbee88722004-05-12 21:47:57 +000071 // is used by PHI nodes in successors of this block.
Chris Lattner8abf6932004-05-10 19:17:36 +000072 DenseMap<unsigned, VirtReg2IndexFunctor> VRegPHIUseCount;
73 VRegPHIUseCount.grow(MF.getSSARegMap()->getLastVirtReg());
Chris Lattner80e20eb2004-05-10 19:06:37 +000074
Chris Lattnerbee88722004-05-12 21:47:57 +000075 unsigned BBIsSuccOfPreds = 0; // Number of times MBB is a succ of preds
76 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin(),
77 E = MBB.pred_end(); PI != E; ++PI)
78 for (MachineBasicBlock::succ_iterator SI = (*PI)->succ_begin(),
79 E = (*PI)->succ_end(); SI != E; ++SI) {
80 BBIsSuccOfPreds += *SI == &MBB;
81 for (MachineBasicBlock::iterator BBI = (*SI)->begin(); BBI !=(*SI)->end() &&
82 BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
83 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
84 VRegPHIUseCount[BBI->getOperand(i).getReg()]++;
85 }
86
Chris Lattner791f8962004-05-10 18:47:18 +000087 // Get an iterator to the first instruction after the last PHI node (this may
Chris Lattner53a79aa2005-10-03 04:47:08 +000088 // also be the end of the basic block).
Chris Lattner791f8962004-05-10 18:47:18 +000089 MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
90 while (AfterPHIsIt != MBB.end() &&
Chris Lattnerbee88722004-05-12 21:47:57 +000091 AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
Chris Lattner791f8962004-05-10 18:47:18 +000092 ++AfterPHIsIt; // Skip over all of the PHI nodes...
93
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000094 while (MBB.front().getOpcode() == TargetInstrInfo::PHI) {
Chris Lattner53a79aa2005-10-03 04:47:08 +000095 LowerAtomicPHINode(MBB, AfterPHIsIt, VRegPHIUseCount, BBIsSuccOfPreds);
96 }
97 return true;
98}
Misha Brukmanedf128a2005-04-21 22:36:52 +000099
Chris Lattner53a79aa2005-10-03 04:47:08 +0000100/// LowerAtomicPHINode - Lower the PHI node at the top of the specified block,
101/// under the assuption that it needs to be lowered in a way that supports
102/// atomic execution of PHIs. This lowering method is always correct all of the
103/// time.
104void PNE::LowerAtomicPHINode(MachineBasicBlock &MBB,
105 MachineBasicBlock::iterator AfterPHIsIt,
106 DenseMap<unsigned, VirtReg2IndexFunctor> &VRegPHIUseCount,
107 unsigned BBIsSuccOfPreds) {
108 // 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 Lattner53a79aa2005-10-03 04:47:08 +0000143 std::pair<LiveVariables::killed_iterator, LiveVariables::killed_iterator>
144 RKs = LV->dead_range(MPhi);
145 if (RKs.first != RKs.second) {
146 for (LiveVariables::killed_iterator I = RKs.first; I != RKs.second; ++I)
147 LV->addVirtualRegisterDead(*I, PHICopy);
148 LV->removeVirtualRegistersDead(MPhi);
149 }
150 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000151
Chris Lattner53a79aa2005-10-03 04:47:08 +0000152 // Adjust the VRegPHIUseCount map to account for the removal of this PHI
153 // node.
154 for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
155 VRegPHIUseCount[MPhi->getOperand(i).getReg()] -= BBIsSuccOfPreds;
Chris Lattner572c7702003-05-12 14:28:28 +0000156
Chris Lattner53a79aa2005-10-03 04:47:08 +0000157 // Now loop over all of the incoming arguments, changing them to copy into
158 // the IncomingReg register in the corresponding predecessor basic block.
159 //
160 for (int i = MPhi->getNumOperands() - 1; i >= 2; i-=2) {
161 MachineOperand &opVal = MPhi->getOperand(i-1);
162
163 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
164 // source path the PHI.
165 MachineBasicBlock &opBlock = *MPhi->getOperand(i).getMachineBasicBlock();
166
167 MachineBasicBlock::iterator I = opBlock.getFirstTerminator();
168
169 // Check to make sure we haven't already emitted the copy for this block.
170 // This can happen because PHI nodes may have multiple entries for the
171 // same basic block. It doesn't matter which entry we use though, because
172 // all incoming values are guaranteed to be the same for a particular bb.
173 //
174 // If we emitted a copy for this basic block already, it will be right
175 // where we want to insert one now. Just check for a definition of the
176 // register we are interested in!
177 //
178 bool HaveNotEmitted = true;
179
180 if (I != opBlock.begin()) {
181 MachineBasicBlock::iterator PrevInst = prior(I);
182 for (unsigned i = 0, e = PrevInst->getNumOperands(); i != e; ++i) {
183 MachineOperand &MO = PrevInst->getOperand(i);
184 if (MO.isRegister() && MO.getReg() == IncomingReg)
185 if (MO.isDef()) {
186 HaveNotEmitted = false;
187 break;
188 }
Chris Lattner927ce5d2003-05-12 03:55:21 +0000189 }
190 }
191
Chris Lattner53a79aa2005-10-03 04:47:08 +0000192 if (HaveNotEmitted) { // If the copy has not already been emitted, do it.
193 assert(MRegisterInfo::isVirtualRegister(opVal.getReg()) &&
194 "Machine PHI Operands must all be virtual registers!");
195 unsigned SrcReg = opVal.getReg();
196 RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC);
Chris Lattner80e20eb2004-05-10 19:06:37 +0000197
Chris Lattner53a79aa2005-10-03 04:47:08 +0000198 // Now update live variable information if we have it.
199 if (LV) {
200 // We want to be able to insert a kill of the register if this PHI
201 // (aka, the copy we just inserted) is the last use of the source
202 // value. Live variable analysis conservatively handles this by
203 // saying that the value is live until the end of the block the PHI
204 // entry lives in. If the value really is dead at the PHI copy, there
205 // will be no successor blocks which have the value live-in.
206 //
207 // Check to see if the copy is the last use, and if so, update the
208 // live variables information so that it knows the copy source
209 // instruction kills the incoming value.
210 //
211 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000212
Chris Lattner53a79aa2005-10-03 04:47:08 +0000213 // Loop over all of the successors of the basic block, checking to see
214 // if the value is either live in the block, or if it is killed in the
215 // block. Also check to see if this register is in use by another PHI
216 // node which has not yet been eliminated. If so, it will be killed
217 // at an appropriate point later.
218 //
219 bool ValueIsLive = false;
220 for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
221 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
222 MachineBasicBlock *SuccMBB = *SI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000223
Chris Lattner53a79aa2005-10-03 04:47:08 +0000224 // Is it alive in this successor?
225 unsigned SuccIdx = SuccMBB->getNumber();
226 if (SuccIdx < InRegVI.AliveBlocks.size() &&
227 InRegVI.AliveBlocks[SuccIdx]) {
228 ValueIsLive = true;
229 break;
230 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000231
Chris Lattner53a79aa2005-10-03 04:47:08 +0000232 // Is it killed in this successor?
233 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
234 if (InRegVI.Kills[i]->getParent() == SuccMBB) {
Chris Lattner572c7702003-05-12 14:28:28 +0000235 ValueIsLive = true;
236 break;
237 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238
Chris Lattner53a79aa2005-10-03 04:47:08 +0000239 // Is it used by any PHI instructions in this block?
240 if (!ValueIsLive)
241 ValueIsLive = VRegPHIUseCount[SrcReg] != 0;
242 }
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000243
Chris Lattner53a79aa2005-10-03 04:47:08 +0000244 // Okay, if we now know that the value is not live out of the block,
245 // we can add a kill marker to the copy we inserted saying that it
246 // kills the incoming value!
247 //
248 if (!ValueIsLive) {
249 MachineBasicBlock::iterator Prev = prior(I);
250 LV->addVirtualRegisterKilled(SrcReg, Prev);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000251
Chris Lattner53a79aa2005-10-03 04:47:08 +0000252 // This vreg no longer lives all of the way through opBlock.
253 unsigned opBlockNum = opBlock.getNumber();
254 if (opBlockNum < InRegVI.AliveBlocks.size())
255 InRegVI.AliveBlocks[opBlockNum] = false;
Chris Lattner572c7702003-05-12 14:28:28 +0000256 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000257 }
258 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000259 }
Chris Lattner53a79aa2005-10-03 04:47:08 +0000260
261 // Really delete the PHI instruction now!
262 delete MPhi;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000263}