blob: 6f3c82c70811ef3b9b3c5f430deb5dbc0a8e0c92 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Chris Lattnercd3245a2006-12-19 22:41:21 +000016#define DEBUG_TYPE "phielim"
Misha Brukmand7a10c82005-05-05 23:45:17 +000017#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner0742b592004-02-23 18:38:20 +000018#include "llvm/CodeGen/Passes.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
Evan Chengf870fbc2008-04-11 17:54:45 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000023#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000024#include "llvm/Target/TargetMachine.h"
Evan Cheng576a2702008-04-03 16:38:20 +000025#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/STLExtras.h"
Chris Lattner6db07562005-10-03 07:22:07 +000027#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner6db07562005-10-03 07:22:07 +000029#include <algorithm>
Evan Cheng10883172008-04-02 17:23:50 +000030#include <map>
Chris Lattner0742b592004-02-23 18:38:20 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattnercd3245a2006-12-19 22:41:21 +000033STATISTIC(NumAtomic, "Number of atomic phis lowered");
Evan Chengfc0b80d2009-03-13 22:59:14 +000034STATISTIC(NumEH, "Number of EH try blocks skipped");
Chris Lattnercd3245a2006-12-19 22:41:21 +000035
Chris Lattnerbc40e892003-01-13 20:01:16 +000036namespace {
Evan Cheng576a2702008-04-03 16:38:20 +000037 class VISIBILITY_HIDDEN PNE : public MachineFunctionPass {
38 MachineRegisterInfo *MRI; // Machine register information
39
40 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000041 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000042 PNE() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000043
Evan Cheng576a2702008-04-03 16:38:20 +000044 virtual bool runOnMachineFunction(MachineFunction &Fn);
45
Chris Lattnerbc40e892003-01-13 20:01:16 +000046 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.addPreserved<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000048 AU.addPreservedID(MachineLoopInfoID);
49 AU.addPreservedID(MachineDominatorsID);
Chris Lattnerbc40e892003-01-13 20:01:16 +000050 MachineFunctionPass::getAnalysisUsage(AU);
51 }
52
53 private:
54 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
55 /// in predecessor basic blocks.
56 ///
57 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
Chris Lattner53a79aa2005-10-03 04:47:08 +000058 void LowerAtomicPHINode(MachineBasicBlock &MBB,
Bill Wendlingca756d22006-09-28 07:10:24 +000059 MachineBasicBlock::iterator AfterPHIsIt);
60
61 /// analyzePHINodes - Gather information about the PHI nodes in
62 /// here. In particular, we want to map the number of uses of a virtual
63 /// register which is used in a PHI node. We map that to the BB the
64 /// vreg is coming from. This is used later to determine when the vreg
65 /// is killed in the BB.
66 ///
67 void analyzePHINodes(const MachineFunction& Fn);
68
Evan Chengfc0b80d2009-03-13 22:59:14 +000069 void WalkPassEHTryRange(MachineBasicBlock &MBB,
70 MachineBasicBlock::iterator &I, unsigned SrcReg);
71
Bill Wendlingca756d22006-09-28 07:10:24 +000072 typedef std::pair<const MachineBasicBlock*, unsigned> BBVRegPair;
73 typedef std::map<BBVRegPair, unsigned> VRegPHIUse;
74
75 VRegPHIUse VRegPHIUseCount;
Evan Cheng576a2702008-04-03 16:38:20 +000076
77 // Defs of PHI sources which are implicit_def.
78 SmallPtrSet<MachineInstr*, 4> ImpDefs;
Chris Lattnerbc40e892003-01-13 20:01:16 +000079 };
Chris Lattnerbc40e892003-01-13 20:01:16 +000080}
81
Dan Gohman844731a2008-05-13 00:00:25 +000082char PNE::ID = 0;
83static RegisterPass<PNE>
84X("phi-node-elimination", "Eliminate PHI nodes for register allocation");
85
Dan Gohman6ddba2b2008-05-13 02:05:11 +000086const PassInfo *const llvm::PHIEliminationID = &X;
Chris Lattnerbc40e892003-01-13 20:01:16 +000087
Evan Cheng576a2702008-04-03 16:38:20 +000088bool PNE::runOnMachineFunction(MachineFunction &Fn) {
89 MRI = &Fn.getRegInfo();
90
91 analyzePHINodes(Fn);
92
93 bool Changed = false;
94
95 // Eliminate PHI instructions by inserting copies into predecessor blocks.
96 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
97 Changed |= EliminatePHINodes(Fn, *I);
98
99 // Remove dead IMPLICIT_DEF instructions.
100 for (SmallPtrSet<MachineInstr*,4>::iterator I = ImpDefs.begin(),
101 E = ImpDefs.end(); I != E; ++I) {
102 MachineInstr *DefMI = *I;
103 unsigned DefReg = DefMI->getOperand(0).getReg();
Evan Cheng1b38ec82008-06-19 01:21:26 +0000104 if (MRI->use_empty(DefReg))
Evan Cheng576a2702008-04-03 16:38:20 +0000105 DefMI->eraseFromParent();
106 }
107
108 ImpDefs.clear();
109 VRegPHIUseCount.clear();
110 return Changed;
111}
112
113
Chris Lattnerbc40e892003-01-13 20:01:16 +0000114/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
115/// predecessor basic blocks.
116///
117bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000118 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattner53a79aa2005-10-03 04:47:08 +0000119 return false; // Quick exit for basic blocks without PHIs.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000120
Chris Lattner791f8962004-05-10 18:47:18 +0000121 // Get an iterator to the first instruction after the last PHI node (this may
Chris Lattner53a79aa2005-10-03 04:47:08 +0000122 // also be the end of the basic block).
Chris Lattner791f8962004-05-10 18:47:18 +0000123 MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
124 while (AfterPHIsIt != MBB.end() &&
Chris Lattnerbee88722004-05-12 21:47:57 +0000125 AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
Chris Lattner791f8962004-05-10 18:47:18 +0000126 ++AfterPHIsIt; // Skip over all of the PHI nodes...
127
Bill Wendlingca756d22006-09-28 07:10:24 +0000128 while (MBB.front().getOpcode() == TargetInstrInfo::PHI)
129 LowerAtomicPHINode(MBB, AfterPHIsIt);
130
Chris Lattner53a79aa2005-10-03 04:47:08 +0000131 return true;
132}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Evan Cheng1b38ec82008-06-19 01:21:26 +0000134/// isSourceDefinedByImplicitDef - Return true if all sources of the phi node
135/// are implicit_def's.
Bill Wendlingae94dda2008-05-12 22:15:05 +0000136static bool isSourceDefinedByImplicitDef(const MachineInstr *MPhi,
Evan Cheng1b38ec82008-06-19 01:21:26 +0000137 const MachineRegisterInfo *MRI) {
Evan Chengb3e0a6d2008-05-10 00:17:50 +0000138 for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2) {
139 unsigned SrcReg = MPhi->getOperand(i).getReg();
Bill Wendlingae94dda2008-05-12 22:15:05 +0000140 const MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
Evan Chengb3e0a6d2008-05-10 00:17:50 +0000141 if (!DefMI || DefMI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF)
142 return false;
143 }
144 return true;
Evan Chengf870fbc2008-04-11 17:54:45 +0000145}
146
Evan Chengfc0b80d2009-03-13 22:59:14 +0000147void PNE::WalkPassEHTryRange(MachineBasicBlock &MBB,
148 MachineBasicBlock::iterator &I, unsigned SrcReg) {
149 if (I == MBB.begin())
150 return;
151 MachineBasicBlock::iterator PI = prior(I);
152 if (PI->getOpcode() != TargetInstrInfo::EH_LABEL)
153 return;
154
155 // Trying to walk pass the EH try range. If we run into a use instruction,
156 // we want to insert the copy there.
157 SmallPtrSet<MachineInstr*, 4> UsesInMBB;
158 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
159 UE = MRI->use_end(); UI != UE; ++UI) {
160 MachineInstr *UseMI = &*UI;
161 if (UseMI->getParent() == &MBB)
162 UsesInMBB.insert(UseMI);
163 }
164
165 while (PI != MBB.begin()) {
166 --PI;
167 if (PI->getOpcode() == TargetInstrInfo::EH_LABEL) {
168 ++NumEH;
169 I = PI;
170 return;
171 } else if (UsesInMBB.count(&*PI)) {
172 ++NumEH;
173 I = next(PI);
174 return;
175 }
176 }
177 return;
178}
179
Chris Lattner53a79aa2005-10-03 04:47:08 +0000180/// LowerAtomicPHINode - Lower the PHI node at the top of the specified block,
181/// under the assuption that it needs to be lowered in a way that supports
182/// atomic execution of PHIs. This lowering method is always correct all of the
183/// time.
Bill Wendlingae94dda2008-05-12 22:15:05 +0000184///
Chris Lattner53a79aa2005-10-03 04:47:08 +0000185void PNE::LowerAtomicPHINode(MachineBasicBlock &MBB,
Bill Wendlingca756d22006-09-28 07:10:24 +0000186 MachineBasicBlock::iterator AfterPHIsIt) {
Chris Lattner53a79aa2005-10-03 04:47:08 +0000187 // Unlink the PHI node from the basic block, but don't delete the PHI yet.
188 MachineInstr *MPhi = MBB.remove(MBB.begin());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000189
Evan Chengf870fbc2008-04-11 17:54:45 +0000190 unsigned NumSrcs = (MPhi->getNumOperands() - 1) / 2;
Chris Lattner53a79aa2005-10-03 04:47:08 +0000191 unsigned DestReg = MPhi->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000192 bool isDead = MPhi->getOperand(0).isDead();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000193
Bill Wendlingca756d22006-09-28 07:10:24 +0000194 // Create a new register for the incoming PHI arguments.
Chris Lattner53a79aa2005-10-03 04:47:08 +0000195 MachineFunction &MF = *MBB.getParent();
Chris Lattner84bc5422007-12-31 04:13:23 +0000196 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(DestReg);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000197 unsigned IncomingReg = 0;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000198
Bill Wendlingae94dda2008-05-12 22:15:05 +0000199 // Insert a register to register copy at the top of the current block (but
Chris Lattner53a79aa2005-10-03 04:47:08 +0000200 // after any remaining phi nodes) which copies the new incoming register
201 // into the phi node destination.
Owen Andersond10fd972007-12-31 06:32:00 +0000202 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
Evan Chengb3e0a6d2008-05-10 00:17:50 +0000203 if (isSourceDefinedByImplicitDef(MPhi, MRI))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000204 // If all sources of a PHI node are implicit_def, just emit an
205 // implicit_def instead of a copy.
Bill Wendlingd62e06c2009-02-03 02:29:34 +0000206 BuildMI(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000207 TII->get(TargetInstrInfo::IMPLICIT_DEF), DestReg);
208 else {
209 IncomingReg = MF.getRegInfo().createVirtualRegister(RC);
Evan Chengf870fbc2008-04-11 17:54:45 +0000210 TII->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC, RC);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000211 }
Chris Lattner53a79aa2005-10-03 04:47:08 +0000212
Bill Wendlingae94dda2008-05-12 22:15:05 +0000213 // Update live variable information if there is any.
Duncan Sands1465d612009-01-28 13:14:17 +0000214 LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>();
Chris Lattner53a79aa2005-10-03 04:47:08 +0000215 if (LV) {
216 MachineInstr *PHICopy = prior(AfterPHIsIt);
217
Evan Cheng9f1c8312008-07-03 09:09:37 +0000218 if (IncomingReg) {
219 // Increment use count of the newly created virtual register.
220 LV->getVarInfo(IncomingReg).NumUses++;
Evan Cheng3fefc182007-04-18 00:36:11 +0000221
Evan Cheng9f1c8312008-07-03 09:09:37 +0000222 // Add information to LiveVariables to know that the incoming value is
223 // killed. Note that because the value is defined in several places (once
224 // each for each incoming block), the "def" block and instruction fields
225 // for the VarInfo is not filled in.
226 LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
227
228 LV->getVarInfo(IncomingReg).UsedBlocks[MBB.getNumber()] = true;
229 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000230
Bill Wendlingae94dda2008-05-12 22:15:05 +0000231 // Since we are going to be deleting the PHI node, if it is the last use of
232 // any registers, or if the value itself is dead, we need to move this
Chris Lattner53a79aa2005-10-03 04:47:08 +0000233 // information over to the new copy we just inserted.
Chris Lattner53a79aa2005-10-03 04:47:08 +0000234 LV->removeVirtualRegistersKilled(MPhi);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000235
Chris Lattner6db07562005-10-03 07:22:07 +0000236 // If the result is dead, update LV.
Evan Cheng9f1c8312008-07-03 09:09:37 +0000237 if (isDead) {
Chris Lattner6db07562005-10-03 07:22:07 +0000238 LV->addVirtualRegisterDead(DestReg, PHICopy);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000239 LV->removeVirtualRegisterDead(DestReg, MPhi);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000240 }
241 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000242
Bill Wendlingae94dda2008-05-12 22:15:05 +0000243 // Adjust the VRegPHIUseCount map to account for the removal of this PHI node.
Chris Lattner53a79aa2005-10-03 04:47:08 +0000244 for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000245 --VRegPHIUseCount[BBVRegPair(MPhi->getOperand(i + 1).getMBB(),
246 MPhi->getOperand(i).getReg())];
Chris Lattner572c7702003-05-12 14:28:28 +0000247
Bill Wendlingae94dda2008-05-12 22:15:05 +0000248 // Now loop over all of the incoming arguments, changing them to copy into the
249 // IncomingReg register in the corresponding predecessor basic block.
Evan Cheng576a2702008-04-03 16:38:20 +0000250 SmallPtrSet<MachineBasicBlock*, 8> MBBsInsertedInto;
Evan Chengf870fbc2008-04-11 17:54:45 +0000251 for (int i = NumSrcs - 1; i >= 0; --i) {
252 unsigned SrcReg = MPhi->getOperand(i*2+1).getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000253 assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
Chris Lattner6db07562005-10-03 07:22:07 +0000254 "Machine PHI Operands must all be virtual registers!");
Chris Lattner53a79aa2005-10-03 04:47:08 +0000255
Bill Wendlingae94dda2008-05-12 22:15:05 +0000256 // If source is defined by an implicit def, there is no need to insert a
Evan Cheng9f1c8312008-07-03 09:09:37 +0000257 // copy.
Evan Cheng576a2702008-04-03 16:38:20 +0000258 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
259 if (DefMI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
260 ImpDefs.insert(DefMI);
261 continue;
262 }
263
Bill Wendlingae94dda2008-05-12 22:15:05 +0000264 // Get the MachineBasicBlock equivalent of the BasicBlock that is the source
265 // path the PHI.
Evan Chengf870fbc2008-04-11 17:54:45 +0000266 MachineBasicBlock &opBlock = *MPhi->getOperand(i*2+2).getMBB();
Chris Lattner53a79aa2005-10-03 04:47:08 +0000267
Chris Lattner53a79aa2005-10-03 04:47:08 +0000268 // Check to make sure we haven't already emitted the copy for this block.
Bill Wendlingae94dda2008-05-12 22:15:05 +0000269 // This can happen because PHI nodes may have multiple entries for the same
270 // basic block.
Evan Cheng576a2702008-04-03 16:38:20 +0000271 if (!MBBsInsertedInto.insert(&opBlock))
Chris Lattner6db07562005-10-03 07:22:07 +0000272 continue; // If the copy has already been emitted, we're done.
273
Bill Wendlingae94dda2008-05-12 22:15:05 +0000274 // Find a safe location to insert the copy, this may be the first terminator
275 // in the block (or end()).
Evan Chengfc5423d2008-04-04 01:20:05 +0000276 MachineBasicBlock::iterator InsertPos = opBlock.getFirstTerminator();
Evan Cheng1b38ec82008-06-19 01:21:26 +0000277
Evan Chengfc0b80d2009-03-13 22:59:14 +0000278 // Walk pass EH try range if needed.
279 WalkPassEHTryRange(opBlock, InsertPos, SrcReg);
280
Chris Lattner6db07562005-10-03 07:22:07 +0000281 // Insert the copy.
Evan Cheng576a2702008-04-03 16:38:20 +0000282 TII->copyRegToReg(opBlock, InsertPos, IncomingReg, SrcReg, RC, RC);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000283
Chris Lattner6db07562005-10-03 07:22:07 +0000284 // Now update live variable information if we have it. Otherwise we're done
285 if (!LV) continue;
286
Bill Wendlingae94dda2008-05-12 22:15:05 +0000287 // We want to be able to insert a kill of the register if this PHI (aka, the
288 // copy we just inserted) is the last use of the source value. Live
289 // variable analysis conservatively handles this by saying that the value is
290 // live until the end of the block the PHI entry lives in. If the value
291 // really is dead at the PHI copy, there will be no successor blocks which
292 // have the value live-in.
Chris Lattner6db07562005-10-03 07:22:07 +0000293 //
Bill Wendlingae94dda2008-05-12 22:15:05 +0000294 // Check to see if the copy is the last use, and if so, update the live
295 // variables information so that it knows the copy source instruction kills
296 // the incoming value.
Chris Lattner6db07562005-10-03 07:22:07 +0000297 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
Owen Andersona0185402007-11-08 01:20:48 +0000298 InRegVI.UsedBlocks[opBlock.getNumber()] = true;
Chris Lattner6db07562005-10-03 07:22:07 +0000299
Bill Wendlingae94dda2008-05-12 22:15:05 +0000300 // Loop over all of the successors of the basic block, checking to see if
301 // the value is either live in the block, or if it is killed in the block.
302 // Also check to see if this register is in use by another PHI node which
303 // has not yet been eliminated. If so, it will be killed at an appropriate
304 // point later.
Chris Lattner6db07562005-10-03 07:22:07 +0000305
306 // Is it used by any PHI instructions in this block?
Bill Wendlingca756d22006-09-28 07:10:24 +0000307 bool ValueIsLive = VRegPHIUseCount[BBVRegPair(&opBlock, SrcReg)] != 0;
Chris Lattner6db07562005-10-03 07:22:07 +0000308
309 std::vector<MachineBasicBlock*> OpSuccBlocks;
310
311 // Otherwise, scan successors, including the BB the PHI node lives in.
312 for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
313 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
314 MachineBasicBlock *SuccMBB = *SI;
315
316 // Is it alive in this successor?
317 unsigned SuccIdx = SuccMBB->getNumber();
318 if (SuccIdx < InRegVI.AliveBlocks.size() &&
319 InRegVI.AliveBlocks[SuccIdx]) {
320 ValueIsLive = true;
321 break;
Chris Lattner927ce5d2003-05-12 03:55:21 +0000322 }
Chris Lattner6db07562005-10-03 07:22:07 +0000323
324 OpSuccBlocks.push_back(SuccMBB);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000325 }
326
Chris Lattner6db07562005-10-03 07:22:07 +0000327 // Check to see if this value is live because there is a use in a successor
328 // that kills it.
329 if (!ValueIsLive) {
330 switch (OpSuccBlocks.size()) {
331 case 1: {
332 MachineBasicBlock *MBB = OpSuccBlocks[0];
333 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
334 if (InRegVI.Kills[i]->getParent() == MBB) {
Chris Lattner53a79aa2005-10-03 04:47:08 +0000335 ValueIsLive = true;
336 break;
337 }
Chris Lattner6db07562005-10-03 07:22:07 +0000338 break;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000339 }
Chris Lattner6db07562005-10-03 07:22:07 +0000340 case 2: {
341 MachineBasicBlock *MBB1 = OpSuccBlocks[0], *MBB2 = OpSuccBlocks[1];
342 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
343 if (InRegVI.Kills[i]->getParent() == MBB1 ||
344 InRegVI.Kills[i]->getParent() == MBB2) {
345 ValueIsLive = true;
346 break;
347 }
348 break;
349 }
350 default:
351 std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
352 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
353 if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
354 InRegVI.Kills[i]->getParent())) {
355 ValueIsLive = true;
356 break;
357 }
358 }
359 }
360
Bill Wendlingae94dda2008-05-12 22:15:05 +0000361 // Okay, if we now know that the value is not live out of the block, we can
362 // add a kill marker in this block saying that it kills the incoming value!
Chris Lattner6db07562005-10-03 07:22:07 +0000363 if (!ValueIsLive) {
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000364 // In our final twist, we have to decide which instruction kills the
Bill Wendlingae94dda2008-05-12 22:15:05 +0000365 // register. In most cases this is the copy, however, the first
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000366 // terminator instruction at the end of the block may also use the value.
367 // In this case, we should mark *it* as being the killing block, not the
368 // copy.
Evan Cheng576a2702008-04-03 16:38:20 +0000369 MachineBasicBlock::iterator KillInst = prior(InsertPos);
370 MachineBasicBlock::iterator Term = opBlock.getFirstTerminator();
371 if (Term != opBlock.end()) {
372 if (Term->readsRegister(SrcReg))
373 KillInst = Term;
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000374
375 // Check that no other terminators use values.
376#ifndef NDEBUG
Evan Cheng576a2702008-04-03 16:38:20 +0000377 for (MachineBasicBlock::iterator TI = next(Term); TI != opBlock.end();
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000378 ++TI) {
Evan Cheng576a2702008-04-03 16:38:20 +0000379 assert(!TI->readsRegister(SrcReg) &&
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000380 "Terminator instructions cannot use virtual registers unless"
381 "they are the first terminator in a block!");
382 }
383#endif
384 }
385
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000386 // Finally, mark it killed.
387 LV->addVirtualRegisterKilled(SrcReg, KillInst);
Chris Lattner6db07562005-10-03 07:22:07 +0000388
389 // This vreg no longer lives all of the way through opBlock.
390 unsigned opBlockNum = opBlock.getNumber();
391 if (opBlockNum < InRegVI.AliveBlocks.size())
392 InRegVI.AliveBlocks[opBlockNum] = false;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000393 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000394 }
Chris Lattner53a79aa2005-10-03 04:47:08 +0000395
396 // Really delete the PHI instruction now!
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000397 MF.DeleteMachineInstr(MPhi);
Chris Lattner6db07562005-10-03 07:22:07 +0000398 ++NumAtomic;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000399}
Bill Wendlingca756d22006-09-28 07:10:24 +0000400
401/// analyzePHINodes - Gather information about the PHI nodes in here. In
402/// particular, we want to map the number of uses of a virtual register which is
403/// used in a PHI node. We map that to the BB the vreg is coming from. This is
404/// used later to determine when the vreg is killed in the BB.
405///
406void PNE::analyzePHINodes(const MachineFunction& Fn) {
407 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
408 I != E; ++I)
409 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
410 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
411 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000412 ++VRegPHIUseCount[BBVRegPair(BBI->getOperand(i + 1).getMBB(),
413 BBI->getOperand(i).getReg())];
Bill Wendlingca756d22006-09-28 07:10:24 +0000414}