blob: ffde4beaf0a221c12a867f78ff15cc65aa92a75b [file] [log] [blame]
Chris Lattnerbc40e892003-01-13 20:01:16 +00001//===-- PhiElimination.cpp - Eliminate PHI nodes by inserting copies ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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
Chris Lattner0742b592004-02-23 18:38:20 +000016#include "llvm/CodeGen/Passes.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/SSARegMap.h"
20#include "llvm/CodeGen/LiveVariables.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"
Alkis Evlogimenosf81af212004-02-14 01:18:34 +000023#include "Support/STLExtras.h"
Chris Lattner0742b592004-02-23 18:38:20 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerbc40e892003-01-13 20:01:16 +000026namespace {
27 struct PNE : public MachineFunctionPass {
28 bool runOnMachineFunction(MachineFunction &Fn) {
29 bool Changed = false;
30
31 // Eliminate PHI instructions by inserting copies into predecessor blocks.
32 //
33 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
34 Changed |= EliminatePHINodes(Fn, *I);
35
36 //std::cerr << "AFTER PHI NODE ELIM:\n";
37 //Fn.dump();
38 return Changed;
39 }
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addPreserved<LiveVariables>();
43 MachineFunctionPass::getAnalysisUsage(AU);
44 }
45
46 private:
47 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
48 /// in predecessor basic blocks.
49 ///
50 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
51 };
52
53 RegisterPass<PNE> X("phi-node-elimination",
54 "Eliminate PHI nodes for register allocation");
55}
56
Brian Gaeked0fde302003-11-11 22:41:34 +000057
Chris Lattner0742b592004-02-23 18:38:20 +000058const PassInfo *llvm::PHIEliminationID = X.getPassInfo();
Chris Lattnerbc40e892003-01-13 20:01:16 +000059
60/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
61/// predecessor basic blocks.
62///
63bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000064 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattnerbc40e892003-01-13 20:01:16 +000065 return false; // Quick exit for normal case...
66
67 LiveVariables *LV = getAnalysisToUpdate<LiveVariables>();
68 const TargetInstrInfo &MII = MF.getTarget().getInstrInfo();
69 const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
70
Chris Lattner791f8962004-05-10 18:47:18 +000071 // Get an iterator to the first instruction after the last PHI node (this may
72 // allso be the end of the basic block).
73 MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
74 while (AfterPHIsIt != MBB.end() &&
75 AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
76 ++AfterPHIsIt; // Skip over all of the PHI nodes...
77
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000078 while (MBB.front().getOpcode() == TargetInstrInfo::PHI) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000079 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattner4f6410f2004-03-31 21:59:29 +000080 MachineInstr *MI = MBB.remove(MBB.begin());
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000081
Chris Lattner1cbe4d02004-02-10 21:12:22 +000082 assert(MRegisterInfo::isVirtualRegister(MI->getOperand(0).getReg()) &&
Chris Lattnerbc40e892003-01-13 20:01:16 +000083 "PHI node doesn't write virt reg?");
84
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000085 unsigned DestReg = MI->getOperand(0).getReg();
Chris Lattnerbc40e892003-01-13 20:01:16 +000086
87 // Create a new register for the incoming PHI arguments
88 const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(DestReg);
89 unsigned IncomingReg = MF.getSSARegMap()->createVirtualRegister(RC);
90
Chris Lattner927ce5d2003-05-12 03:55:21 +000091 // Insert a register to register copy in the top of the current block (but
Chris Lattnerbc40e892003-01-13 20:01:16 +000092 // after any remaining phi nodes) which copies the new incoming register
93 // into the phi node destination.
94 //
Chris Lattnerbc40e892003-01-13 20:01:16 +000095 RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC);
Chris Lattner927ce5d2003-05-12 03:55:21 +000096
97 // Update live variable information if there is any...
98 if (LV) {
Chris Lattner791f8962004-05-10 18:47:18 +000099 MachineInstr *PHICopy = prior(AfterPHIsIt);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000100
Chris Lattner927ce5d2003-05-12 03:55:21 +0000101 // Add information to LiveVariables to know that the incoming value is
Chris Lattnerb52e0242003-05-12 17:37:30 +0000102 // killed. Note that because the value is defined in several places (once
103 // each for each incoming block), the "def" block and instruction fields
104 // for the VarInfo is not filled in.
Chris Lattner927ce5d2003-05-12 03:55:21 +0000105 //
Chris Lattnerb52e0242003-05-12 17:37:30 +0000106 LV->addVirtualRegisterKilled(IncomingReg, &MBB, PHICopy);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000107
Chris Lattner927ce5d2003-05-12 03:55:21 +0000108 // Since we are going to be deleting the PHI node, if it is the last use
109 // of any registers, or if the value itself is dead, we need to move this
110 // information over to the new copy we just inserted...
111 //
112 std::pair<LiveVariables::killed_iterator, LiveVariables::killed_iterator>
113 RKs = LV->killed_range(MI);
Chris Lattner572c7702003-05-12 14:28:28 +0000114 std::vector<std::pair<MachineInstr*, unsigned> > Range;
Chris Lattner927ce5d2003-05-12 03:55:21 +0000115 if (RKs.first != RKs.second) {
Chris Lattner572c7702003-05-12 14:28:28 +0000116 // Copy the range into a vector...
117 Range.assign(RKs.first, RKs.second);
118
119 // Delete the range...
Chris Lattner927ce5d2003-05-12 03:55:21 +0000120 LV->removeVirtualRegistersKilled(RKs.first, RKs.second);
Chris Lattner572c7702003-05-12 14:28:28 +0000121
122 // Add all of the kills back, which will update the appropriate info...
123 for (unsigned i = 0, e = Range.size(); i != e; ++i)
124 LV->addVirtualRegisterKilled(Range[i].second, &MBB, PHICopy);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000125 }
126
127 RKs = LV->dead_range(MI);
128 if (RKs.first != RKs.second) {
Chris Lattner572c7702003-05-12 14:28:28 +0000129 // Works as above...
130 Range.assign(RKs.first, RKs.second);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000131 LV->removeVirtualRegistersDead(RKs.first, RKs.second);
Chris Lattner572c7702003-05-12 14:28:28 +0000132 for (unsigned i = 0, e = Range.size(); i != e; ++i)
133 LV->addVirtualRegisterDead(Range[i].second, &MBB, PHICopy);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000134 }
135 }
136
137 // Now loop over all of the incoming arguments, changing them to copy into
Chris Lattnerbc40e892003-01-13 20:01:16 +0000138 // the IncomingReg register in the corresponding predecessor basic block.
139 //
140 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
141 MachineOperand &opVal = MI->getOperand(i-1);
142
143 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
Chris Lattner927ce5d2003-05-12 03:55:21 +0000144 // source path the PHI.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000145 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
146
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000147 MachineBasicBlock::iterator I = opBlock.getFirstTerminator();
Chris Lattner98719d72003-05-12 04:08:54 +0000148
Chris Lattnerbc40e892003-01-13 20:01:16 +0000149 // Check to make sure we haven't already emitted the copy for this block.
150 // This can happen because PHI nodes may have multiple entries for the
151 // same basic block. It doesn't matter which entry we use though, because
152 // all incoming values are guaranteed to be the same for a particular bb.
153 //
Chris Lattner98719d72003-05-12 04:08:54 +0000154 // If we emitted a copy for this basic block already, it will be right
155 // where we want to insert one now. Just check for a definition of the
156 // register we are interested in!
Chris Lattnerbc40e892003-01-13 20:01:16 +0000157 //
158 bool HaveNotEmitted = true;
Chris Lattner98719d72003-05-12 04:08:54 +0000159
160 if (I != opBlock.begin()) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000161 MachineBasicBlock::iterator PrevInst = prior(I);
Chris Lattner98719d72003-05-12 04:08:54 +0000162 for (unsigned i = 0, e = PrevInst->getNumOperands(); i != e; ++i) {
163 MachineOperand &MO = PrevInst->getOperand(i);
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000164 if (MO.isRegister() && MO.getReg() == IncomingReg)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000165 if (MO.isDef()) {
Chris Lattner98719d72003-05-12 04:08:54 +0000166 HaveNotEmitted = false;
167 break;
168 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000169 }
Chris Lattner98719d72003-05-12 04:08:54 +0000170 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000171
Chris Lattner572c7702003-05-12 14:28:28 +0000172 if (HaveNotEmitted) { // If the copy has not already been emitted, do it.
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000173 assert(MRegisterInfo::isVirtualRegister(opVal.getReg()) &&
Chris Lattner98719d72003-05-12 04:08:54 +0000174 "Machine PHI Operands must all be virtual registers!");
Chris Lattner572c7702003-05-12 14:28:28 +0000175 unsigned SrcReg = opVal.getReg();
176 RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC);
177
178 // Now update live variable information if we have it.
179 if (LV) {
180 // We want to be able to insert a kill of the register if this PHI
181 // (aka, the copy we just inserted) is the last use of the source
182 // value. Live variable analysis conservatively handles this by
183 // saying that the value is live until the end of the block the PHI
184 // entry lives in. If the value really is dead at the PHI copy, there
185 // will be no successor blocks which have the value live-in.
186 //
187 // Check to see if the copy is the last use, and if so, update the
188 // live variables information so that it knows the copy source
189 // instruction kills the incoming value.
190 //
191 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
192
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000193 // Loop over all of the successors of the basic block, checking to see
194 // if the value is either live in the block, or if it is killed in the
195 // block. Also check to see if this register is in use by another PHI
196 // node which has not yet been eliminated. If so, it will be killed
197 // at an appropriate point later.
Chris Lattner572c7702003-05-12 14:28:28 +0000198 //
199 bool ValueIsLive = false;
Chris Lattner015959e2004-05-01 21:24:39 +0000200 for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
201 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
202 MachineBasicBlock *MBB = *SI;
Chris Lattner572c7702003-05-12 14:28:28 +0000203
204 // Is it alive in this successor?
Chris Lattner015959e2004-05-01 21:24:39 +0000205 unsigned SuccIdx = LV->getMachineBasicBlockIndex(MBB);
Chris Lattner572c7702003-05-12 14:28:28 +0000206 if (SuccIdx < InRegVI.AliveBlocks.size() &&
207 InRegVI.AliveBlocks[SuccIdx]) {
208 ValueIsLive = true;
209 break;
210 }
211
212 // Is it killed in this successor?
Chris Lattner572c7702003-05-12 14:28:28 +0000213 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
214 if (InRegVI.Kills[i].first == MBB) {
215 ValueIsLive = true;
216 break;
217 }
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000218
219 // Is it used by any PHI instructions in this block?
220 if (ValueIsLive) break;
221
222 // Loop over all of the PHIs in this successor, checking to see if
223 // the register is being used...
224 for (MachineBasicBlock::iterator BBI = MBB->begin(), E=MBB->end();
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000225 BBI != E && BBI->getOpcode() == TargetInstrInfo::PHI;
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000226 ++BBI)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000227 for (unsigned i = 1, e = BBI->getNumOperands(); i < e; i += 2)
228 if (BBI->getOperand(i).getReg() == SrcReg) {
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000229 ValueIsLive = true;
230 break;
231 }
Chris Lattner572c7702003-05-12 14:28:28 +0000232 }
233
234 // Okay, if we now know that the value is not live out of the block,
235 // we can add a kill marker to the copy we inserted saying that it
236 // kills the incoming value!
237 //
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000238 if (!ValueIsLive) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000239 MachineBasicBlock::iterator Prev = prior(I);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000240 LV->addVirtualRegisterKilled(SrcReg, &opBlock, Prev);
241 }
Chris Lattner572c7702003-05-12 14:28:28 +0000242 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000243 }
244 }
245
246 // really delete the PHI instruction now!
247 delete MI;
248 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000249 return true;
250}