2003-01-13 21:01:16 +01:00
|
|
|
//===-- PhiElimination.cpp - Eliminate PHI nodes by inserting copies ------===//
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-01-13 21:01:16 +01:00
|
|
|
//
|
|
|
|
// This pass eliminates machine instruction PHI nodes by inserting copy
|
|
|
|
// instructions. This destroys SSA information, but is the desired input for
|
|
|
|
// some register allocators.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 23:41:21 +01:00
|
|
|
#define DEBUG_TYPE "phielim"
|
2009-07-22 01:47:33 +02:00
|
|
|
#include "PHIElimination.h"
|
2005-05-06 01:45:17 +02:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
2004-02-23 19:38:20 +01:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2009-11-14 01:38:06 +01:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2003-01-13 21:01:16 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2008-04-11 19:54:45 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 05:13:23 +01:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-11-11 20:31:31 +01:00
|
|
|
#include "llvm/Function.h"
|
2003-01-13 21:01:16 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-04-03 18:38:20 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2005-10-03 09:22:07 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-11-10 23:01:05 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-08-27 14:54:02 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-11-10 23:01:05 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2005-10-03 09:22:07 +02:00
|
|
|
#include <algorithm>
|
2008-04-02 19:23:50 +02:00
|
|
|
#include <map>
|
2004-02-23 19:38:20 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-12-19 23:41:21 +01:00
|
|
|
STATISTIC(NumAtomic, "Number of atomic phis lowered");
|
2009-11-10 23:01:05 +01:00
|
|
|
STATISTIC(NumSplits, "Number of critical edges split on demand");
|
2009-12-16 19:55:53 +01:00
|
|
|
STATISTIC(NumReused, "Number of reused lowered phis");
|
2009-11-10 23:01:05 +01:00
|
|
|
|
2009-07-22 01:47:33 +02:00
|
|
|
char PHIElimination::ID = 0;
|
|
|
|
static RegisterPass<PHIElimination>
|
2008-05-13 02:00:25 +02:00
|
|
|
X("phi-node-elimination", "Eliminate PHI nodes for register allocation");
|
|
|
|
|
2008-05-13 04:05:11 +02:00
|
|
|
const PassInfo *const llvm::PHIEliminationID = &X;
|
2003-01-13 21:01:16 +01:00
|
|
|
|
2009-07-22 01:47:33 +02:00
|
|
|
void llvm::PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-08-01 01:37:33 +02:00
|
|
|
AU.addPreserved<LiveVariables>();
|
2009-11-14 01:38:06 +01:00
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
2009-11-18 19:01:35 +01:00
|
|
|
// rdar://7401784 This would be nice:
|
|
|
|
// AU.addPreservedID(MachineLoopInfoID);
|
2009-08-01 01:37:33 +02:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2009-07-22 01:47:33 +02:00
|
|
|
|
|
|
|
bool llvm::PHIElimination::runOnMachineFunction(MachineFunction &Fn) {
|
2008-04-03 18:38:20 +02:00
|
|
|
MRI = &Fn.getRegInfo();
|
|
|
|
|
2009-07-23 07:44:24 +02:00
|
|
|
PHIDefs.clear();
|
|
|
|
PHIKills.clear();
|
2008-04-03 18:38:20 +02:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-11-11 20:31:31 +01:00
|
|
|
// Split critical edges to help the coalescer
|
2009-11-18 19:01:35 +01:00
|
|
|
if (LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>())
|
2009-11-11 20:31:31 +01:00
|
|
|
for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
|
2009-11-18 19:01:35 +01:00
|
|
|
Changed |= SplitPHIEdges(Fn, *I, *LV);
|
2009-11-11 20:31:31 +01:00
|
|
|
|
|
|
|
// Populate VRegPHIUseCount
|
|
|
|
analyzePHINodes(Fn);
|
|
|
|
|
2008-04-03 18:38:20 +02:00
|
|
|
// Eliminate PHI instructions by inserting copies into predecessor blocks.
|
|
|
|
for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
|
|
|
|
Changed |= EliminatePHINodes(Fn, *I);
|
|
|
|
|
|
|
|
// Remove dead IMPLICIT_DEF instructions.
|
2009-12-18 00:42:32 +01:00
|
|
|
for (SmallPtrSet<MachineInstr*, 4>::iterator I = ImpDefs.begin(),
|
2008-04-03 18:38:20 +02:00
|
|
|
E = ImpDefs.end(); I != E; ++I) {
|
|
|
|
MachineInstr *DefMI = *I;
|
|
|
|
unsigned DefReg = DefMI->getOperand(0).getReg();
|
2008-06-19 03:21:26 +02:00
|
|
|
if (MRI->use_empty(DefReg))
|
2008-04-03 18:38:20 +02:00
|
|
|
DefMI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2009-12-16 19:55:53 +01:00
|
|
|
// Clean up the lowered PHI instructions.
|
|
|
|
for (LoweredPHIMap::iterator I = LoweredPHIs.begin(), E = LoweredPHIs.end();
|
|
|
|
I != E; ++I)
|
|
|
|
Fn.DeleteMachineInstr(I->first);
|
|
|
|
|
2009-12-18 00:42:32 +01:00
|
|
|
LoweredPHIs.clear();
|
2008-04-03 18:38:20 +02:00
|
|
|
ImpDefs.clear();
|
|
|
|
VRegPHIUseCount.clear();
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2003-01-13 21:01:16 +01:00
|
|
|
/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
|
|
|
|
/// predecessor basic blocks.
|
|
|
|
///
|
2009-07-22 01:47:33 +02:00
|
|
|
bool llvm::PHIElimination::EliminatePHINodes(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) {
|
2004-02-12 03:27:10 +01:00
|
|
|
if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
|
2005-10-03 06:47:08 +02:00
|
|
|
return false; // Quick exit for basic blocks without PHIs.
|
2003-01-13 21:01:16 +01:00
|
|
|
|
2004-05-10 20:47:18 +02:00
|
|
|
// Get an iterator to the first instruction after the last PHI node (this may
|
2005-10-03 06:47:08 +02:00
|
|
|
// also be the end of the basic block).
|
2009-03-17 10:46:22 +01:00
|
|
|
MachineBasicBlock::iterator AfterPHIsIt = SkipPHIsAndLabels(MBB, MBB.begin());
|
2004-05-10 20:47:18 +02:00
|
|
|
|
2006-09-28 09:10:24 +02:00
|
|
|
while (MBB.front().getOpcode() == TargetInstrInfo::PHI)
|
|
|
|
LowerAtomicPHINode(MBB, AfterPHIsIt);
|
|
|
|
|
2005-10-03 06:47:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2008-06-19 03:21:26 +02:00
|
|
|
/// isSourceDefinedByImplicitDef - Return true if all sources of the phi node
|
|
|
|
/// are implicit_def's.
|
2008-05-13 00:15:05 +02:00
|
|
|
static bool isSourceDefinedByImplicitDef(const MachineInstr *MPhi,
|
2008-06-19 03:21:26 +02:00
|
|
|
const MachineRegisterInfo *MRI) {
|
2008-05-10 02:17:50 +02:00
|
|
|
for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2) {
|
|
|
|
unsigned SrcReg = MPhi->getOperand(i).getReg();
|
2008-05-13 00:15:05 +02:00
|
|
|
const MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
|
2008-05-10 02:17:50 +02:00
|
|
|
if (!DefMI || DefMI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-04-11 19:54:45 +02:00
|
|
|
}
|
|
|
|
|
2009-11-13 22:56:15 +01:00
|
|
|
// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg
|
|
|
|
// when following the CFG edge to SuccMBB. This needs to be after any def of
|
|
|
|
// SrcReg, but before any subsequent point where control flow might jump out of
|
|
|
|
// the basic block.
|
2009-07-22 01:47:33 +02:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
llvm::PHIElimination::FindCopyInsertPoint(MachineBasicBlock &MBB,
|
2009-11-13 22:56:15 +01:00
|
|
|
MachineBasicBlock &SuccMBB,
|
2009-07-22 01:47:33 +02:00
|
|
|
unsigned SrcReg) {
|
2009-03-17 10:46:22 +01:00
|
|
|
// Handle the trivial case trivially.
|
|
|
|
if (MBB.empty())
|
|
|
|
return MBB.begin();
|
|
|
|
|
2009-11-13 22:56:15 +01:00
|
|
|
// Usually, we just want to insert the copy before the first terminator
|
|
|
|
// instruction. However, for the edge going to a landing pad, we must insert
|
|
|
|
// the copy before the call/invoke instruction.
|
|
|
|
if (!SuccMBB.isLandingPad())
|
2009-03-17 10:46:22 +01:00
|
|
|
return MBB.getFirstTerminator();
|
|
|
|
|
2009-11-16 03:00:09 +01:00
|
|
|
// Discover any defs/uses in this basic block.
|
2009-03-17 10:46:22 +01:00
|
|
|
SmallPtrSet<MachineInstr*, 8> DefUsesInMBB;
|
2009-11-16 03:00:09 +01:00
|
|
|
for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(SrcReg),
|
|
|
|
RE = MRI->reg_end(); RI != RE; ++RI) {
|
2009-03-17 10:46:22 +01:00
|
|
|
MachineInstr *DefUseMI = &*RI;
|
|
|
|
if (DefUseMI->getParent() == &MBB)
|
|
|
|
DefUsesInMBB.insert(DefUseMI);
|
2009-03-13 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2009-03-17 10:46:22 +01:00
|
|
|
MachineBasicBlock::iterator InsertPoint;
|
|
|
|
if (DefUsesInMBB.empty()) {
|
2009-11-13 22:56:15 +01:00
|
|
|
// No defs. Insert the copy at the start of the basic block.
|
2009-03-17 10:46:22 +01:00
|
|
|
InsertPoint = MBB.begin();
|
|
|
|
} else if (DefUsesInMBB.size() == 1) {
|
2009-11-16 03:00:09 +01:00
|
|
|
// Insert the copy immediately after the def/use.
|
2009-03-17 10:46:22 +01:00
|
|
|
InsertPoint = *DefUsesInMBB.begin();
|
|
|
|
++InsertPoint;
|
|
|
|
} else {
|
2009-11-16 03:00:09 +01:00
|
|
|
// Insert the copy immediately after the last def/use.
|
2009-03-17 10:46:22 +01:00
|
|
|
InsertPoint = MBB.end();
|
|
|
|
while (!DefUsesInMBB.count(&*--InsertPoint)) {}
|
|
|
|
++InsertPoint;
|
2009-03-13 23:59:14 +01:00
|
|
|
}
|
2009-03-17 10:46:22 +01:00
|
|
|
|
|
|
|
// Make sure the copy goes after any phi nodes however.
|
|
|
|
return SkipPHIsAndLabels(MBB, InsertPoint);
|
2009-03-13 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2005-10-03 06:47:08 +02:00
|
|
|
/// LowerAtomicPHINode - Lower the PHI node at the top of the specified block,
|
|
|
|
/// under the assuption that it needs to be lowered in a way that supports
|
|
|
|
/// atomic execution of PHIs. This lowering method is always correct all of the
|
|
|
|
/// time.
|
2009-11-10 23:00:56 +01:00
|
|
|
///
|
2009-07-22 01:47:33 +02:00
|
|
|
void llvm::PHIElimination::LowerAtomicPHINode(
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator AfterPHIsIt) {
|
2009-12-16 19:55:53 +01:00
|
|
|
++NumAtomic;
|
2005-10-03 06:47:08 +02:00
|
|
|
// Unlink the PHI node from the basic block, but don't delete the PHI yet.
|
|
|
|
MachineInstr *MPhi = MBB.remove(MBB.begin());
|
|
|
|
|
2008-04-11 19:54:45 +02:00
|
|
|
unsigned NumSrcs = (MPhi->getNumOperands() - 1) / 2;
|
2005-10-03 06:47:08 +02:00
|
|
|
unsigned DestReg = MPhi->getOperand(0).getReg();
|
2008-07-03 11:09:37 +02:00
|
|
|
bool isDead = MPhi->getOperand(0).isDead();
|
2005-10-03 06:47:08 +02:00
|
|
|
|
2006-09-28 09:10:24 +02:00
|
|
|
// Create a new register for the incoming PHI arguments.
|
2005-10-03 06:47:08 +02:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2007-12-31 05:13:23 +01:00
|
|
|
const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(DestReg);
|
2008-07-03 11:09:37 +02:00
|
|
|
unsigned IncomingReg = 0;
|
2009-12-16 19:55:53 +01:00
|
|
|
bool reusedIncoming = false; // Is IncomingReg reused from an earlier PHI?
|
2005-10-03 06:47:08 +02:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Insert a register to register copy at the top of the current block (but
|
2005-10-03 06:47:08 +02:00
|
|
|
// after any remaining phi nodes) which copies the new incoming register
|
|
|
|
// into the phi node destination.
|
2007-12-31 07:32:00 +01:00
|
|
|
const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
|
2008-05-10 02:17:50 +02:00
|
|
|
if (isSourceDefinedByImplicitDef(MPhi, MRI))
|
2008-07-03 11:09:37 +02:00
|
|
|
// If all sources of a PHI node are implicit_def, just emit an
|
|
|
|
// implicit_def instead of a copy.
|
2009-02-03 03:29:34 +01:00
|
|
|
BuildMI(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
|
2008-07-03 11:09:37 +02:00
|
|
|
TII->get(TargetInstrInfo::IMPLICIT_DEF), DestReg);
|
|
|
|
else {
|
2009-12-16 19:55:53 +01:00
|
|
|
// Can we reuse an earlier PHI node? This only happens for critical edges,
|
|
|
|
// typically those created by tail duplication.
|
|
|
|
unsigned &entry = LoweredPHIs[MPhi];
|
|
|
|
if (entry) {
|
|
|
|
// An identical PHI node was already lowered. Reuse the incoming register.
|
|
|
|
IncomingReg = entry;
|
|
|
|
reusedIncoming = true;
|
|
|
|
++NumReused;
|
2010-01-05 02:24:24 +01:00
|
|
|
DEBUG(dbgs() << "Reusing %reg" << IncomingReg << " for " << *MPhi);
|
2009-12-16 19:55:53 +01:00
|
|
|
} else {
|
|
|
|
entry = IncomingReg = MF.getRegInfo().createVirtualRegister(RC);
|
|
|
|
}
|
2008-04-11 19:54:45 +02:00
|
|
|
TII->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC, RC);
|
2008-07-03 11:09:37 +02:00
|
|
|
}
|
2003-01-13 21:01:16 +01:00
|
|
|
|
2009-07-23 06:34:03 +02:00
|
|
|
// Record PHI def.
|
2009-11-10 23:00:56 +01:00
|
|
|
assert(!hasPHIDef(DestReg) && "Vreg has multiple phi-defs?");
|
2009-07-23 07:44:24 +02:00
|
|
|
PHIDefs[DestReg] = &MBB;
|
2009-07-23 06:34:03 +02:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Update live variable information if there is any.
|
2009-01-28 14:14:17 +01:00
|
|
|
LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>();
|
2005-10-03 06:47:08 +02:00
|
|
|
if (LV) {
|
|
|
|
MachineInstr *PHICopy = prior(AfterPHIsIt);
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2008-07-03 11:09:37 +02:00
|
|
|
if (IncomingReg) {
|
2009-12-16 19:55:53 +01:00
|
|
|
LiveVariables::VarInfo &VI = LV->getVarInfo(IncomingReg);
|
|
|
|
|
2008-07-03 11:09:37 +02:00
|
|
|
// Increment use count of the newly created virtual register.
|
2009-12-16 19:55:53 +01:00
|
|
|
VI.NumUses++;
|
|
|
|
|
|
|
|
// When we are reusing the incoming register, it may already have been
|
|
|
|
// killed in this block. The old kill will also have been inserted at
|
|
|
|
// AfterPHIsIt, so it appears before the current PHICopy.
|
|
|
|
if (reusedIncoming)
|
|
|
|
if (MachineInstr *OldKill = VI.findKill(&MBB)) {
|
2010-01-05 02:24:24 +01:00
|
|
|
DEBUG(dbgs() << "Remove old kill from " << *OldKill);
|
2009-12-16 19:55:53 +01:00
|
|
|
LV->removeVirtualRegisterKilled(IncomingReg, OldKill);
|
|
|
|
DEBUG(MBB.dump());
|
|
|
|
}
|
2008-07-03 11:09:37 +02:00
|
|
|
|
|
|
|
// Add information to LiveVariables to know that the incoming value is
|
|
|
|
// killed. Note that because the value is defined in several places (once
|
|
|
|
// each for each incoming block), the "def" block and instruction fields
|
|
|
|
// for the VarInfo is not filled in.
|
|
|
|
LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
|
|
|
|
}
|
2003-01-13 21:01:16 +01:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Since we are going to be deleting the PHI node, if it is the last use of
|
|
|
|
// any registers, or if the value itself is dead, we need to move this
|
2005-10-03 06:47:08 +02:00
|
|
|
// information over to the new copy we just inserted.
|
|
|
|
LV->removeVirtualRegistersKilled(MPhi);
|
|
|
|
|
2005-10-03 09:22:07 +02:00
|
|
|
// If the result is dead, update LV.
|
2008-07-03 11:09:37 +02:00
|
|
|
if (isDead) {
|
2005-10-03 09:22:07 +02:00
|
|
|
LV->addVirtualRegisterDead(DestReg, PHICopy);
|
2008-07-03 11:09:37 +02:00
|
|
|
LV->removeVirtualRegisterDead(DestReg, MPhi);
|
2003-05-12 05:55:21 +02:00
|
|
|
}
|
2005-10-03 06:47:08 +02:00
|
|
|
}
|
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Adjust the VRegPHIUseCount map to account for the removal of this PHI node.
|
2005-10-03 06:47:08 +02:00
|
|
|
for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
|
2009-12-16 19:55:53 +01:00
|
|
|
--VRegPHIUseCount[BBVRegPair(MPhi->getOperand(i+1).getMBB()->getNumber(),
|
2007-12-31 00:10:15 +01:00
|
|
|
MPhi->getOperand(i).getReg())];
|
2005-10-03 06:47:08 +02:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Now loop over all of the incoming arguments, changing them to copy into the
|
|
|
|
// IncomingReg register in the corresponding predecessor basic block.
|
2008-04-03 18:38:20 +02:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> MBBsInsertedInto;
|
2008-04-11 19:54:45 +02:00
|
|
|
for (int i = NumSrcs - 1; i >= 0; --i) {
|
|
|
|
unsigned SrcReg = MPhi->getOperand(i*2+1).getReg();
|
2008-02-10 19:45:23 +01:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
|
2005-10-03 09:22:07 +02:00
|
|
|
"Machine PHI Operands must all be virtual registers!");
|
2005-10-03 06:47:08 +02:00
|
|
|
|
2009-07-23 06:34:03 +02:00
|
|
|
// Get the MachineBasicBlock equivalent of the BasicBlock that is the source
|
|
|
|
// path the PHI.
|
|
|
|
MachineBasicBlock &opBlock = *MPhi->getOperand(i*2+2).getMBB();
|
|
|
|
|
|
|
|
// Record the kill.
|
2009-07-23 07:44:24 +02:00
|
|
|
PHIKills[SrcReg].insert(&opBlock);
|
2009-07-23 06:34:03 +02:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// If source is defined by an implicit def, there is no need to insert a
|
2008-07-03 11:09:37 +02:00
|
|
|
// copy.
|
2008-04-03 18:38:20 +02:00
|
|
|
MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
|
|
|
|
if (DefMI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
|
|
|
|
ImpDefs.insert(DefMI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-10-03 06:47:08 +02:00
|
|
|
// Check to make sure we haven't already emitted the copy for this block.
|
2008-05-13 00:15:05 +02:00
|
|
|
// This can happen because PHI nodes may have multiple entries for the same
|
|
|
|
// basic block.
|
2008-04-03 18:38:20 +02:00
|
|
|
if (!MBBsInsertedInto.insert(&opBlock))
|
2005-10-03 09:22:07 +02:00
|
|
|
continue; // If the copy has already been emitted, we're done.
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Find a safe location to insert the copy, this may be the first terminator
|
|
|
|
// in the block (or end()).
|
2009-11-13 22:56:15 +01:00
|
|
|
MachineBasicBlock::iterator InsertPos =
|
|
|
|
FindCopyInsertPoint(opBlock, MBB, SrcReg);
|
2009-03-13 23:59:14 +01:00
|
|
|
|
2005-10-03 09:22:07 +02:00
|
|
|
// Insert the copy.
|
2009-12-16 19:55:53 +01:00
|
|
|
if (!reusedIncoming && IncomingReg)
|
|
|
|
TII->copyRegToReg(opBlock, InsertPos, IncomingReg, SrcReg, RC, RC);
|
2005-10-03 09:22:07 +02:00
|
|
|
|
|
|
|
// Now update live variable information if we have it. Otherwise we're done
|
|
|
|
if (!LV) continue;
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// We want to be able to insert a kill of the register if this PHI (aka, the
|
|
|
|
// copy we just inserted) is the last use of the source value. Live
|
|
|
|
// variable analysis conservatively handles this by saying that the value is
|
|
|
|
// live until the end of the block the PHI entry lives in. If the value
|
|
|
|
// really is dead at the PHI copy, there will be no successor blocks which
|
|
|
|
// have the value live-in.
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Also check to see if this register is in use by another PHI node which
|
|
|
|
// has not yet been eliminated. If so, it will be killed at an appropriate
|
|
|
|
// point later.
|
2005-10-03 09:22:07 +02:00
|
|
|
|
|
|
|
// Is it used by any PHI instructions in this block?
|
2009-12-16 19:55:53 +01:00
|
|
|
bool ValueIsUsed = VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)];
|
2005-10-03 09:22:07 +02:00
|
|
|
|
2008-05-13 00:15:05 +02:00
|
|
|
// Okay, if we now know that the value is not live out of the block, we can
|
|
|
|
// add a kill marker in this block saying that it kills the incoming value!
|
2009-12-01 18:13:31 +01:00
|
|
|
if (!ValueIsUsed && !LV->isLiveOut(SrcReg, opBlock)) {
|
2006-01-04 08:12:21 +01:00
|
|
|
// In our final twist, we have to decide which instruction kills the
|
2008-05-13 00:15:05 +02:00
|
|
|
// register. In most cases this is the copy, however, the first
|
2006-01-04 08:12:21 +01:00
|
|
|
// terminator instruction at the end of the block may also use the value.
|
|
|
|
// In this case, we should mark *it* as being the killing block, not the
|
|
|
|
// copy.
|
2009-12-16 19:55:53 +01:00
|
|
|
MachineBasicBlock::iterator KillInst;
|
2008-04-03 18:38:20 +02:00
|
|
|
MachineBasicBlock::iterator Term = opBlock.getFirstTerminator();
|
2009-12-16 19:55:53 +01:00
|
|
|
if (Term != opBlock.end() && Term->readsRegister(SrcReg)) {
|
|
|
|
KillInst = Term;
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2006-01-04 08:12:21 +01:00
|
|
|
// Check that no other terminators use values.
|
|
|
|
#ifndef NDEBUG
|
2009-12-03 01:50:42 +01:00
|
|
|
for (MachineBasicBlock::iterator TI = llvm::next(Term);
|
|
|
|
TI != opBlock.end(); ++TI) {
|
2008-04-03 18:38:20 +02:00
|
|
|
assert(!TI->readsRegister(SrcReg) &&
|
2006-01-04 08:12:21 +01:00
|
|
|
"Terminator instructions cannot use virtual registers unless"
|
|
|
|
"they are the first terminator in a block!");
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-16 19:55:53 +01:00
|
|
|
} else if (reusedIncoming || !IncomingReg) {
|
|
|
|
// We may have to rewind a bit if we didn't insert a copy this time.
|
|
|
|
KillInst = Term;
|
|
|
|
while (KillInst != opBlock.begin())
|
|
|
|
if ((--KillInst)->readsRegister(SrcReg))
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// We just inserted this copy.
|
|
|
|
KillInst = prior(InsertPos);
|
2006-01-04 08:12:21 +01:00
|
|
|
}
|
2009-12-16 19:55:53 +01:00
|
|
|
assert(KillInst->readsRegister(SrcReg) && "Cannot find kill instruction");
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2006-01-04 08:12:21 +01:00
|
|
|
// Finally, mark it killed.
|
|
|
|
LV->addVirtualRegisterKilled(SrcReg, KillInst);
|
2005-10-03 09:22:07 +02:00
|
|
|
|
|
|
|
// This vreg no longer lives all of the way through opBlock.
|
|
|
|
unsigned opBlockNum = opBlock.getNumber();
|
2009-11-10 23:00:56 +01:00
|
|
|
LV->getVarInfo(SrcReg).AliveBlocks.reset(opBlockNum);
|
2003-01-13 21:01:16 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2009-12-16 19:55:53 +01:00
|
|
|
// Really delete the PHI instruction now, if it is not in the LoweredPHIs map.
|
|
|
|
if (reusedIncoming || !IncomingReg)
|
|
|
|
MF.DeleteMachineInstr(MPhi);
|
2003-01-13 21:01:16 +01:00
|
|
|
}
|
2006-09-28 09:10:24 +02:00
|
|
|
|
|
|
|
/// analyzePHINodes - Gather information about the PHI nodes in here. In
|
|
|
|
/// particular, we want to map the number of uses of a virtual register which is
|
|
|
|
/// used in a PHI node. We map that to the BB the vreg is coming from. This is
|
|
|
|
/// used later to determine when the vreg is killed in the BB.
|
|
|
|
///
|
2009-07-22 01:47:33 +02:00
|
|
|
void llvm::PHIElimination::analyzePHINodes(const MachineFunction& Fn) {
|
2006-09-28 09:10:24 +02:00
|
|
|
for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
|
|
|
|
I != E; ++I)
|
|
|
|
for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
|
|
|
|
BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
|
|
|
|
for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
|
2009-12-16 19:55:53 +01:00
|
|
|
++VRegPHIUseCount[BBVRegPair(BBI->getOperand(i+1).getMBB()->getNumber(),
|
2007-12-31 00:10:15 +01:00
|
|
|
BBI->getOperand(i).getReg())];
|
2006-09-28 09:10:24 +02:00
|
|
|
}
|
2009-11-10 23:00:56 +01:00
|
|
|
|
2009-11-11 20:31:31 +01:00
|
|
|
bool llvm::PHIElimination::SplitPHIEdges(MachineFunction &MF,
|
2009-11-18 19:01:35 +01:00
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
LiveVariables &LV) {
|
2009-12-18 00:42:32 +01:00
|
|
|
if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI ||
|
|
|
|
MBB.isLandingPad())
|
2009-11-11 20:31:31 +01:00
|
|
|
return false; // Quick exit for basic blocks without PHIs.
|
2009-11-18 19:01:35 +01:00
|
|
|
|
2009-11-10 23:01:05 +01:00
|
|
|
for (MachineBasicBlock::const_iterator BBI = MBB.begin(), BBE = MBB.end();
|
|
|
|
BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) {
|
|
|
|
for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
|
|
|
|
unsigned Reg = BBI->getOperand(i).getReg();
|
|
|
|
MachineBasicBlock *PreMBB = BBI->getOperand(i+1).getMBB();
|
|
|
|
// We break edges when registers are live out from the predecessor block
|
2009-11-11 20:31:31 +01:00
|
|
|
// (not considering PHI nodes). If the register is live in to this block
|
|
|
|
// anyway, we would gain nothing from splitting.
|
2009-12-01 18:13:31 +01:00
|
|
|
if (!LV.isLiveIn(Reg, MBB) && LV.isLiveOut(Reg, *PreMBB))
|
2009-11-10 23:01:05 +01:00
|
|
|
SplitCriticalEdge(PreMBB, &MBB);
|
|
|
|
}
|
|
|
|
}
|
2009-11-11 20:31:31 +01:00
|
|
|
return true;
|
2009-11-10 23:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *PHIElimination::SplitCriticalEdge(MachineBasicBlock *A,
|
|
|
|
MachineBasicBlock *B) {
|
|
|
|
assert(A && B && "Missing MBB end point");
|
|
|
|
|
|
|
|
MachineFunction *MF = A->getParent();
|
2009-11-17 21:46:00 +01:00
|
|
|
|
|
|
|
// We may need to update A's terminator, but we can't do that if AnalyzeBranch
|
2009-11-18 01:02:18 +01:00
|
|
|
// fails. If A uses a jump table, we won't touch it.
|
|
|
|
const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
if (TII->AnalyzeBranch(*A, TBB, FBB, Cond))
|
|
|
|
return NULL;
|
2009-11-17 21:46:00 +01:00
|
|
|
|
|
|
|
++NumSplits;
|
|
|
|
|
2009-11-13 22:56:15 +01:00
|
|
|
MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
|
2009-12-03 01:50:42 +01:00
|
|
|
MF->insert(llvm::next(MachineFunction::iterator(A)), NMBB);
|
2010-01-05 02:24:24 +01:00
|
|
|
DEBUG(dbgs() << "PHIElimination splitting critical edge:"
|
2009-11-10 23:01:05 +01:00
|
|
|
" BB#" << A->getNumber()
|
2009-11-12 21:53:43 +01:00
|
|
|
<< " -- BB#" << NMBB->getNumber()
|
2009-11-10 23:01:05 +01:00
|
|
|
<< " -- BB#" << B->getNumber() << '\n');
|
|
|
|
|
|
|
|
A->ReplaceUsesOfBlockWith(B, NMBB);
|
2009-11-19 20:42:16 +01:00
|
|
|
A->updateTerminator();
|
2009-11-10 23:01:05 +01:00
|
|
|
|
2009-11-19 20:42:16 +01:00
|
|
|
// Insert unconditional "jump B" instruction in NMBB if necessary.
|
2009-11-14 01:38:13 +01:00
|
|
|
NMBB->addSuccessor(B);
|
2009-11-19 20:42:16 +01:00
|
|
|
if (!NMBB->isLayoutSuccessor(B)) {
|
|
|
|
Cond.clear();
|
|
|
|
MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, B, NULL, Cond);
|
|
|
|
}
|
2009-11-10 23:01:05 +01:00
|
|
|
|
|
|
|
// Fix PHI nodes in B so they refer to NMBB instead of A
|
|
|
|
for (MachineBasicBlock::iterator i = B->begin(), e = B->end();
|
|
|
|
i != e && i->getOpcode() == TargetInstrInfo::PHI; ++i)
|
|
|
|
for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
|
2009-11-11 20:31:31 +01:00
|
|
|
if (i->getOperand(ni+1).getMBB() == A)
|
2009-11-10 23:01:05 +01:00
|
|
|
i->getOperand(ni+1).setMBB(NMBB);
|
2009-11-14 01:38:06 +01:00
|
|
|
|
|
|
|
if (LiveVariables *LV=getAnalysisIfAvailable<LiveVariables>())
|
2009-11-21 03:05:21 +01:00
|
|
|
LV->addNewBlock(NMBB, A, B);
|
2009-11-14 01:38:06 +01:00
|
|
|
|
|
|
|
if (MachineDominatorTree *MDT=getAnalysisIfAvailable<MachineDominatorTree>())
|
|
|
|
MDT->addNewBlock(NMBB, A);
|
|
|
|
|
2009-11-10 23:01:05 +01:00
|
|
|
return NMBB;
|
|
|
|
}
|
2009-12-16 19:55:53 +01:00
|
|
|
|
|
|
|
unsigned
|
|
|
|
PHIElimination::PHINodeTraits::getHashValue(const MachineInstr *MI) {
|
|
|
|
if (!MI || MI==getEmptyKey() || MI==getTombstoneKey())
|
|
|
|
return DenseMapInfo<MachineInstr*>::getHashValue(MI);
|
|
|
|
unsigned hash = 0;
|
|
|
|
for (unsigned ni = 1, ne = MI->getNumOperands(); ni != ne; ni += 2)
|
|
|
|
hash = hash*37 + DenseMapInfo<BBVRegPair>::
|
|
|
|
getHashValue(BBVRegPair(MI->getOperand(ni+1).getMBB()->getNumber(),
|
|
|
|
MI->getOperand(ni).getReg()));
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PHIElimination::PHINodeTraits::isEqual(const MachineInstr *LHS,
|
|
|
|
const MachineInstr *RHS) {
|
|
|
|
const MachineInstr *EmptyKey = getEmptyKey();
|
|
|
|
const MachineInstr *TombstoneKey = getTombstoneKey();
|
|
|
|
if (!LHS || !RHS || LHS==EmptyKey || RHS==EmptyKey ||
|
|
|
|
LHS==TombstoneKey || RHS==TombstoneKey)
|
|
|
|
return LHS==RHS;
|
|
|
|
|
|
|
|
unsigned ne = LHS->getNumOperands();
|
|
|
|
if (ne != RHS->getNumOperands())
|
|
|
|
return false;
|
|
|
|
// Ignore operand 0, the defined register.
|
|
|
|
for (unsigned ni = 1; ni != ne; ni += 2)
|
|
|
|
if (LHS->getOperand(ni).getReg() != RHS->getOperand(ni).getReg() ||
|
|
|
|
LHS->getOperand(ni+1).getMBB() != RHS->getOperand(ni+1).getMBB())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|