blob: 06305826e80e7e93ffea3e65a9b6287cbb104462 [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
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/SSARegMap.h"
19#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000020#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner572c7702003-05-12 14:28:28 +000022#include "llvm/Support/CFG.h"
Alkis Evlogimenosf81af212004-02-14 01:18:34 +000023#include "Support/STLExtras.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
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.
33 //
34 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
35 Changed |= EliminatePHINodes(Fn, *I);
36
37 //std::cerr << "AFTER PHI NODE ELIM:\n";
38 //Fn.dump();
39 return Changed;
40 }
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addPreserved<LiveVariables>();
44 MachineFunctionPass::getAnalysisUsage(AU);
45 }
46
47 private:
48 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
49 /// in predecessor basic blocks.
50 ///
51 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
52 };
53
54 RegisterPass<PNE> X("phi-node-elimination",
55 "Eliminate PHI nodes for register allocation");
56}
57
Brian Gaeked0fde302003-11-11 22:41:34 +000058
Chris Lattnerbc40e892003-01-13 20:01:16 +000059const PassInfo *PHIEliminationID = X.getPassInfo();
60
61/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
62/// predecessor basic blocks.
63///
64bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000065 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattnerbc40e892003-01-13 20:01:16 +000066 return false; // Quick exit for normal case...
67
68 LiveVariables *LV = getAnalysisToUpdate<LiveVariables>();
69 const TargetInstrInfo &MII = MF.getTarget().getInstrInfo();
70 const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
71
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000072 while (MBB.front().getOpcode() == TargetInstrInfo::PHI) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000073 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000074 MachineBasicBlock::iterator begin = MBB.begin();
75 MachineInstr *MI = MBB.remove(begin);
76
Chris Lattner1cbe4d02004-02-10 21:12:22 +000077 assert(MRegisterInfo::isVirtualRegister(MI->getOperand(0).getReg()) &&
Chris Lattnerbc40e892003-01-13 20:01:16 +000078 "PHI node doesn't write virt reg?");
79
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000080 unsigned DestReg = MI->getOperand(0).getReg();
Chris Lattnerbc40e892003-01-13 20:01:16 +000081
82 // Create a new register for the incoming PHI arguments
83 const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(DestReg);
84 unsigned IncomingReg = MF.getSSARegMap()->createVirtualRegister(RC);
85
Chris Lattner927ce5d2003-05-12 03:55:21 +000086 // Insert a register to register copy in the top of the current block (but
Chris Lattnerbc40e892003-01-13 20:01:16 +000087 // after any remaining phi nodes) which copies the new incoming register
88 // into the phi node destination.
89 //
90 MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
Chris Lattnera13f0d32003-05-12 14:26:38 +000091 while (AfterPHIsIt != MBB.end() &&
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000092 AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
Chris Lattnera13f0d32003-05-12 14:26:38 +000093 ++AfterPHIsIt; // Skip over all of the PHI nodes...
Chris Lattnerbc40e892003-01-13 20:01:16 +000094 RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC);
Chris Lattner927ce5d2003-05-12 03:55:21 +000095
96 // Update live variable information if there is any...
97 if (LV) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000098 MachineInstr *PHICopy = --AfterPHIsIt;
Chris Lattnerbc40e892003-01-13 20:01:16 +000099
Chris Lattner927ce5d2003-05-12 03:55:21 +0000100 // Add information to LiveVariables to know that the incoming value is
Chris Lattnerb52e0242003-05-12 17:37:30 +0000101 // killed. Note that because the value is defined in several places (once
102 // each for each incoming block), the "def" block and instruction fields
103 // for the VarInfo is not filled in.
Chris Lattner927ce5d2003-05-12 03:55:21 +0000104 //
Chris Lattnerb52e0242003-05-12 17:37:30 +0000105 LV->addVirtualRegisterKilled(IncomingReg, &MBB, PHICopy);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000106
Chris Lattner927ce5d2003-05-12 03:55:21 +0000107 // Since we are going to be deleting the PHI node, if it is the last use
108 // of any registers, or if the value itself is dead, we need to move this
109 // information over to the new copy we just inserted...
110 //
111 std::pair<LiveVariables::killed_iterator, LiveVariables::killed_iterator>
112 RKs = LV->killed_range(MI);
Chris Lattner572c7702003-05-12 14:28:28 +0000113 std::vector<std::pair<MachineInstr*, unsigned> > Range;
Chris Lattner927ce5d2003-05-12 03:55:21 +0000114 if (RKs.first != RKs.second) {
Chris Lattner572c7702003-05-12 14:28:28 +0000115 // Copy the range into a vector...
116 Range.assign(RKs.first, RKs.second);
117
118 // Delete the range...
Chris Lattner927ce5d2003-05-12 03:55:21 +0000119 LV->removeVirtualRegistersKilled(RKs.first, RKs.second);
Chris Lattner572c7702003-05-12 14:28:28 +0000120
121 // Add all of the kills back, which will update the appropriate info...
122 for (unsigned i = 0, e = Range.size(); i != e; ++i)
123 LV->addVirtualRegisterKilled(Range[i].second, &MBB, PHICopy);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000124 }
125
126 RKs = LV->dead_range(MI);
127 if (RKs.first != RKs.second) {
Chris Lattner572c7702003-05-12 14:28:28 +0000128 // Works as above...
129 Range.assign(RKs.first, RKs.second);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000130 LV->removeVirtualRegistersDead(RKs.first, RKs.second);
Chris Lattner572c7702003-05-12 14:28:28 +0000131 for (unsigned i = 0, e = Range.size(); i != e; ++i)
132 LV->addVirtualRegisterDead(Range[i].second, &MBB, PHICopy);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000133 }
134 }
135
136 // Now loop over all of the incoming arguments, changing them to copy into
Chris Lattnerbc40e892003-01-13 20:01:16 +0000137 // the IncomingReg register in the corresponding predecessor basic block.
138 //
139 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
140 MachineOperand &opVal = MI->getOperand(i-1);
141
142 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
Chris Lattner927ce5d2003-05-12 03:55:21 +0000143 // source path the PHI.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000144 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
145
Chris Lattner98719d72003-05-12 04:08:54 +0000146 // Figure out where to insert the copy, which is at the end of the
147 // predecessor basic block, but before any terminator/branch
148 // instructions...
149 MachineBasicBlock::iterator I = opBlock.end();
150 if (I != opBlock.begin()) { // Handle empty blocks
151 --I;
152 // must backtrack over ALL the branches in the previous block
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000153 while (MII.isTerminatorInstr(I->getOpcode()) &&
Chris Lattner98719d72003-05-12 04:08:54 +0000154 I != opBlock.begin())
155 --I;
156
157 // move back to the first branch instruction so new instructions
158 // are inserted right in front of it and not in front of a non-branch
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000159 if (!MII.isTerminatorInstr(I->getOpcode()))
Chris Lattner98719d72003-05-12 04:08:54 +0000160 ++I;
161 }
162
Chris Lattnerbc40e892003-01-13 20:01:16 +0000163 // Check to make sure we haven't already emitted the copy for this block.
164 // This can happen because PHI nodes may have multiple entries for the
165 // same basic block. It doesn't matter which entry we use though, because
166 // all incoming values are guaranteed to be the same for a particular bb.
167 //
Chris Lattner98719d72003-05-12 04:08:54 +0000168 // If we emitted a copy for this basic block already, it will be right
169 // where we want to insert one now. Just check for a definition of the
170 // register we are interested in!
Chris Lattnerbc40e892003-01-13 20:01:16 +0000171 //
172 bool HaveNotEmitted = true;
Chris Lattner98719d72003-05-12 04:08:54 +0000173
174 if (I != opBlock.begin()) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000175 MachineBasicBlock::iterator PrevInst = prior(I);
Chris Lattner98719d72003-05-12 04:08:54 +0000176 for (unsigned i = 0, e = PrevInst->getNumOperands(); i != e; ++i) {
177 MachineOperand &MO = PrevInst->getOperand(i);
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000178 if (MO.isRegister() && MO.getReg() == IncomingReg)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000179 if (MO.isDef()) {
Chris Lattner98719d72003-05-12 04:08:54 +0000180 HaveNotEmitted = false;
181 break;
182 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000183 }
Chris Lattner98719d72003-05-12 04:08:54 +0000184 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000185
Chris Lattner572c7702003-05-12 14:28:28 +0000186 if (HaveNotEmitted) { // If the copy has not already been emitted, do it.
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000187 assert(MRegisterInfo::isVirtualRegister(opVal.getReg()) &&
Chris Lattner98719d72003-05-12 04:08:54 +0000188 "Machine PHI Operands must all be virtual registers!");
Chris Lattner572c7702003-05-12 14:28:28 +0000189 unsigned SrcReg = opVal.getReg();
190 RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC);
191
192 // Now update live variable information if we have it.
193 if (LV) {
194 // We want to be able to insert a kill of the register if this PHI
195 // (aka, the copy we just inserted) is the last use of the source
196 // value. Live variable analysis conservatively handles this by
197 // saying that the value is live until the end of the block the PHI
198 // entry lives in. If the value really is dead at the PHI copy, there
199 // will be no successor blocks which have the value live-in.
200 //
201 // Check to see if the copy is the last use, and if so, update the
202 // live variables information so that it knows the copy source
203 // instruction kills the incoming value.
204 //
205 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
206
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000207 // Loop over all of the successors of the basic block, checking to see
208 // if the value is either live in the block, or if it is killed in the
209 // block. Also check to see if this register is in use by another PHI
210 // node which has not yet been eliminated. If so, it will be killed
211 // at an appropriate point later.
Chris Lattner572c7702003-05-12 14:28:28 +0000212 //
213 bool ValueIsLive = false;
Chris Lattner9e2dd8f2003-07-26 23:24:56 +0000214 const BasicBlock *BB = opBlock.getBasicBlock();
215 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB);
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000216 SI != E && !ValueIsLive; ++SI) {
Chris Lattner572c7702003-05-12 14:28:28 +0000217 const std::pair<MachineBasicBlock*, unsigned> &
218 SuccInfo = LV->getBasicBlockInfo(*SI);
219
220 // Is it alive in this successor?
221 unsigned SuccIdx = SuccInfo.second;
222 if (SuccIdx < InRegVI.AliveBlocks.size() &&
223 InRegVI.AliveBlocks[SuccIdx]) {
224 ValueIsLive = true;
225 break;
226 }
227
228 // Is it killed in this successor?
229 MachineBasicBlock *MBB = SuccInfo.first;
230 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
231 if (InRegVI.Kills[i].first == MBB) {
232 ValueIsLive = true;
233 break;
234 }
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000235
236 // Is it used by any PHI instructions in this block?
237 if (ValueIsLive) break;
238
239 // Loop over all of the PHIs in this successor, checking to see if
240 // the register is being used...
241 for (MachineBasicBlock::iterator BBI = MBB->begin(), E=MBB->end();
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000242 BBI != E && BBI->getOpcode() == TargetInstrInfo::PHI;
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000243 ++BBI)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000244 for (unsigned i = 1, e = BBI->getNumOperands(); i < e; i += 2)
245 if (BBI->getOperand(i).getReg() == SrcReg) {
Chris Lattner08d2e4e2003-06-05 17:15:04 +0000246 ValueIsLive = true;
247 break;
248 }
Chris Lattner572c7702003-05-12 14:28:28 +0000249 }
250
251 // Okay, if we now know that the value is not live out of the block,
252 // we can add a kill marker to the copy we inserted saying that it
253 // kills the incoming value!
254 //
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000255 if (!ValueIsLive) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000256 MachineBasicBlock::iterator Prev = prior(I);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000257 LV->addVirtualRegisterKilled(SrcReg, &opBlock, Prev);
258 }
Chris Lattner572c7702003-05-12 14:28:28 +0000259 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000260 }
261 }
262
263 // really delete the PHI instruction now!
264 delete MI;
265 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000266 return true;
267}
Brian Gaeked0fde302003-11-11 22:41:34 +0000268
269} // End llvm namespace