blob: 7473c5c22f9681ef0ba4a60446e1b41cf1231b86 [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 Lattner0742b592004-02-23 18:38:20 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnerbc40e892003-01-13 20:01:16 +000027namespace {
28 struct PNE : public MachineFunctionPass {
29 bool runOnMachineFunction(MachineFunction &Fn) {
30 bool Changed = false;
31
32 // Eliminate PHI instructions by inserting copies into predecessor blocks.
Chris Lattnerbc40e892003-01-13 20:01:16 +000033 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Misha Brukmandedf2bd2005-04-22 04:01:18 +000034 Changed |= EliminatePHINodes(Fn, *I);
Chris Lattnerbc40e892003-01-13 20:01:16 +000035
Chris Lattnerbc40e892003-01-13 20:01:16 +000036 return Changed;
37 }
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addPreserved<LiveVariables>();
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
44 private:
45 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
46 /// in predecessor basic blocks.
47 ///
48 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
49 };
50
51 RegisterPass<PNE> X("phi-node-elimination",
Misha Brukmandedf2bd2005-04-22 04:01:18 +000052 "Eliminate PHI nodes for register allocation");
Chris Lattnerbc40e892003-01-13 20:01:16 +000053}
54
Brian Gaeked0fde302003-11-11 22:41:34 +000055
Chris Lattner0742b592004-02-23 18:38:20 +000056const PassInfo *llvm::PHIEliminationID = X.getPassInfo();
Chris Lattnerbc40e892003-01-13 20:01:16 +000057
58/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
59/// predecessor basic blocks.
60///
61bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000062 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattnerbc40e892003-01-13 20:01:16 +000063 return false; // Quick exit for normal case...
64
65 LiveVariables *LV = getAnalysisToUpdate<LiveVariables>();
Chris Lattner9bcdcd12004-06-02 05:57:12 +000066 const TargetInstrInfo &MII = *MF.getTarget().getInstrInfo();
Chris Lattnerbc40e892003-01-13 20:01:16 +000067 const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
68
Chris Lattner80e20eb2004-05-10 19:06:37 +000069 // VRegPHIUseCount - Keep track of the number of times each virtual register
Chris Lattnerbee88722004-05-12 21:47:57 +000070 // is used by PHI nodes in successors of this block.
Chris Lattner8abf6932004-05-10 19:17:36 +000071 DenseMap<unsigned, VirtReg2IndexFunctor> VRegPHIUseCount;
72 VRegPHIUseCount.grow(MF.getSSARegMap()->getLastVirtReg());
Chris Lattner80e20eb2004-05-10 19:06:37 +000073
Chris Lattnerbee88722004-05-12 21:47:57 +000074 unsigned BBIsSuccOfPreds = 0; // Number of times MBB is a succ of preds
75 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin(),
76 E = MBB.pred_end(); PI != E; ++PI)
77 for (MachineBasicBlock::succ_iterator SI = (*PI)->succ_begin(),
78 E = (*PI)->succ_end(); SI != E; ++SI) {
79 BBIsSuccOfPreds += *SI == &MBB;
80 for (MachineBasicBlock::iterator BBI = (*SI)->begin(); BBI !=(*SI)->end() &&
81 BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
82 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
83 VRegPHIUseCount[BBI->getOperand(i).getReg()]++;
84 }
85
Chris Lattner791f8962004-05-10 18:47:18 +000086 // Get an iterator to the first instruction after the last PHI node (this may
Chris Lattnerbee88722004-05-12 21:47:57 +000087 // also be the end of the basic block). While we are scanning the PHIs,
Chris Lattner80e20eb2004-05-10 19:06:37 +000088 // populate the VRegPHIUseCount map.
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 Lattnera7bfbba2004-07-22 23:05:12 +000095 // Unlink the PHI node from the basic block, but don't delete the PHI yet.
96 MachineInstr *MPhi = MBB.remove(MBB.begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +000097
Chris Lattnera7bfbba2004-07-22 23:05:12 +000098 assert(MRegisterInfo::isVirtualRegister(MPhi->getOperand(0).getReg()) &&
Chris Lattnerbc40e892003-01-13 20:01:16 +000099 "PHI node doesn't write virt reg?");
100
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000101 unsigned DestReg = MPhi->getOperand(0).getReg();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000102
Chris Lattnerbc40e892003-01-13 20:01:16 +0000103 // Create a new register for the incoming PHI arguments
104 const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(DestReg);
105 unsigned IncomingReg = MF.getSSARegMap()->createVirtualRegister(RC);
106
Chris Lattner927ce5d2003-05-12 03:55:21 +0000107 // Insert a register to register copy in the top of the current block (but
Chris Lattnerbc40e892003-01-13 20:01:16 +0000108 // after any remaining phi nodes) which copies the new incoming register
109 // into the phi node destination.
110 //
Chris Lattnerbc40e892003-01-13 20:01:16 +0000111 RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000112
Chris Lattner927ce5d2003-05-12 03:55:21 +0000113 // Update live variable information if there is any...
114 if (LV) {
Chris Lattner791f8962004-05-10 18:47:18 +0000115 MachineInstr *PHICopy = prior(AfterPHIsIt);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000116
Chris Lattner927ce5d2003-05-12 03:55:21 +0000117 // Add information to LiveVariables to know that the incoming value is
Chris Lattnerb52e0242003-05-12 17:37:30 +0000118 // killed. Note that because the value is defined in several places (once
119 // each for each incoming block), the "def" block and instruction fields
120 // for the VarInfo is not filled in.
Chris Lattner927ce5d2003-05-12 03:55:21 +0000121 //
Chris Lattner472405e2004-07-19 06:55:21 +0000122 LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000123
Chris Lattner927ce5d2003-05-12 03:55:21 +0000124 // Since we are going to be deleting the PHI node, if it is the last use
125 // of any registers, or if the value itself is dead, we need to move this
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000126 // information over to the new copy we just inserted.
Chris Lattner927ce5d2003-05-12 03:55:21 +0000127 //
Misha Brukmanedf128a2005-04-21 22:36:52 +0000128 std::pair<LiveVariables::killed_iterator, LiveVariables::killed_iterator>
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000129 RKs = LV->killed_range(MPhi);
Chris Lattner572c7702003-05-12 14:28:28 +0000130 std::vector<std::pair<MachineInstr*, unsigned> > Range;
Chris Lattner94881e82004-07-23 05:27:43 +0000131 if (RKs.first != RKs.second) // Delete the range.
Chris Lattner927ce5d2003-05-12 03:55:21 +0000132 LV->removeVirtualRegistersKilled(RKs.first, RKs.second);
Chris Lattner572c7702003-05-12 14:28:28 +0000133
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000134 RKs = LV->dead_range(MPhi);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000135 if (RKs.first != RKs.second) {
Chris Lattner572c7702003-05-12 14:28:28 +0000136 // Works as above...
137 Range.assign(RKs.first, RKs.second);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000138 LV->removeVirtualRegistersDead(RKs.first, RKs.second);
Chris Lattner572c7702003-05-12 14:28:28 +0000139 for (unsigned i = 0, e = Range.size(); i != e; ++i)
Chris Lattner472405e2004-07-19 06:55:21 +0000140 LV->addVirtualRegisterDead(Range[i].second, PHICopy);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000141 }
142 }
143
Chris Lattner80e20eb2004-05-10 19:06:37 +0000144 // Adjust the VRegPHIUseCount map to account for the removal of this PHI
145 // node.
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000146 for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
147 VRegPHIUseCount[MPhi->getOperand(i).getReg()] -= BBIsSuccOfPreds;
Chris Lattner80e20eb2004-05-10 19:06:37 +0000148
Chris Lattner927ce5d2003-05-12 03:55:21 +0000149 // Now loop over all of the incoming arguments, changing them to copy into
Chris Lattnerbc40e892003-01-13 20:01:16 +0000150 // the IncomingReg register in the corresponding predecessor basic block.
151 //
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000152 for (int i = MPhi->getNumOperands() - 1; i >= 2; i-=2) {
153 MachineOperand &opVal = MPhi->getOperand(i-1);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000154
Chris Lattnerbc40e892003-01-13 20:01:16 +0000155 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
Chris Lattner927ce5d2003-05-12 03:55:21 +0000156 // source path the PHI.
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000157 MachineBasicBlock &opBlock = *MPhi->getOperand(i).getMachineBasicBlock();
Chris Lattnerbc40e892003-01-13 20:01:16 +0000158
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000159 MachineBasicBlock::iterator I = opBlock.getFirstTerminator();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000160
Chris Lattnerbc40e892003-01-13 20:01:16 +0000161 // Check to make sure we haven't already emitted the copy for this block.
162 // This can happen because PHI nodes may have multiple entries for the
163 // same basic block. It doesn't matter which entry we use though, because
164 // all incoming values are guaranteed to be the same for a particular bb.
165 //
Chris Lattner98719d72003-05-12 04:08:54 +0000166 // If we emitted a copy for this basic block already, it will be right
167 // where we want to insert one now. Just check for a definition of the
168 // register we are interested in!
Chris Lattnerbc40e892003-01-13 20:01:16 +0000169 //
170 bool HaveNotEmitted = true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000171
Chris Lattner98719d72003-05-12 04:08:54 +0000172 if (I != opBlock.begin()) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000173 MachineBasicBlock::iterator PrevInst = prior(I);
Chris Lattner98719d72003-05-12 04:08:54 +0000174 for (unsigned i = 0, e = PrevInst->getNumOperands(); i != e; ++i) {
175 MachineOperand &MO = PrevInst->getOperand(i);
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000176 if (MO.isRegister() && MO.getReg() == IncomingReg)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000177 if (MO.isDef()) {
Chris Lattner98719d72003-05-12 04:08:54 +0000178 HaveNotEmitted = false;
179 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000180 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000181 }
Chris Lattner98719d72003-05-12 04:08:54 +0000182 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000183
Chris Lattner572c7702003-05-12 14:28:28 +0000184 if (HaveNotEmitted) { // If the copy has not already been emitted, do it.
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000185 assert(MRegisterInfo::isVirtualRegister(opVal.getReg()) &&
Chris Lattner98719d72003-05-12 04:08:54 +0000186 "Machine PHI Operands must all be virtual registers!");
Chris Lattner572c7702003-05-12 14:28:28 +0000187 unsigned SrcReg = opVal.getReg();
188 RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC);
189
190 // Now update live variable information if we have it.
191 if (LV) {
192 // We want to be able to insert a kill of the register if this PHI
193 // (aka, the copy we just inserted) is the last use of the source
194 // value. Live variable analysis conservatively handles this by
195 // saying that the value is live until the end of the block the PHI
196 // entry lives in. If the value really is dead at the PHI copy, there
197 // will be no successor blocks which have the value live-in.
198 //
199 // Check to see if the copy is the last use, and if so, update the
200 // live variables information so that it knows the copy source
201 // instruction kills the incoming value.
202 //
203 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
204
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000205 // Loop over all of the successors of the basic block, checking to see
206 // if the value is either live in the block, or if it is killed in the
207 // block. Also check to see if this register is in use by another PHI
208 // node which has not yet been eliminated. If so, it will be killed
209 // at an appropriate point later.
Chris Lattner572c7702003-05-12 14:28:28 +0000210 //
211 bool ValueIsLive = false;
Chris Lattner015959e2004-05-01 21:24:39 +0000212 for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
213 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
Chris Lattnerbee88722004-05-12 21:47:57 +0000214 MachineBasicBlock *SuccMBB = *SI;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215
Chris Lattner572c7702003-05-12 14:28:28 +0000216 // Is it alive in this successor?
Chris Lattner8ba97712004-07-01 04:29:47 +0000217 unsigned SuccIdx = SuccMBB->getNumber();
Chris Lattner572c7702003-05-12 14:28:28 +0000218 if (SuccIdx < InRegVI.AliveBlocks.size() &&
219 InRegVI.AliveBlocks[SuccIdx]) {
220 ValueIsLive = true;
221 break;
222 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000223
Chris Lattner572c7702003-05-12 14:28:28 +0000224 // Is it killed in this successor?
Chris Lattner572c7702003-05-12 14:28:28 +0000225 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000226 if (InRegVI.Kills[i]->getParent() == SuccMBB) {
Chris Lattner572c7702003-05-12 14:28:28 +0000227 ValueIsLive = true;
228 break;
229 }
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000230
231 // Is it used by any PHI instructions in this block?
Chris Lattner8abf6932004-05-10 19:17:36 +0000232 if (!ValueIsLive)
233 ValueIsLive = VRegPHIUseCount[SrcReg] != 0;
Chris Lattner572c7702003-05-12 14:28:28 +0000234 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000235
Chris Lattner572c7702003-05-12 14:28:28 +0000236 // Okay, if we now know that the value is not live out of the block,
237 // we can add a kill marker to the copy we inserted saying that it
238 // kills the incoming value!
239 //
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000240 if (!ValueIsLive) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000241 MachineBasicBlock::iterator Prev = prior(I);
Chris Lattner472405e2004-07-19 06:55:21 +0000242 LV->addVirtualRegisterKilled(SrcReg, Prev);
Chris Lattner94881e82004-07-23 05:27:43 +0000243
244 // This vreg no longer lives all of the way through opBlock.
245 unsigned opBlockNum = opBlock.getNumber();
246 if (opBlockNum < InRegVI.AliveBlocks.size())
247 InRegVI.AliveBlocks[opBlockNum] = false;
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000248 }
Chris Lattner572c7702003-05-12 14:28:28 +0000249 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000250 }
251 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252
Chris Lattnera7bfbba2004-07-22 23:05:12 +0000253 // Really delete the PHI instruction now!
254 delete MPhi;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000255 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000256 return true;
257}