2010-02-12 01:30:21 +00:00
|
|
|
//===-- OptimizePHIs.cpp - Optimize machine instruction PHIs --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass optimizes machine instruction PHIs to take advantage of
|
|
|
|
// opportunities created during DAG legalization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-02-12 01:30:21 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2010-02-12 01:30:21 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "phi-opt"
|
|
|
|
|
2010-02-12 01:30:21 +00:00
|
|
|
STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
|
2010-02-13 00:31:44 +00:00
|
|
|
STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
|
2010-02-12 01:30:21 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class OptimizePHIs : public MachineFunctionPass {
|
|
|
|
MachineRegisterInfo *MRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification
|
2010-10-19 17:21:58 +00:00
|
|
|
OptimizePHIs() : MachineFunctionPass(ID) {
|
|
|
|
initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-02-12 01:30:21 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2010-02-12 01:30:21 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-02-12 01:30:21 +00:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-02-13 00:31:44 +00:00
|
|
|
typedef SmallPtrSet<MachineInstr*, 16> InstrSet;
|
|
|
|
typedef SmallPtrSetIterator<MachineInstr*> InstrSetIterator;
|
|
|
|
|
|
|
|
bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
|
|
|
|
InstrSet &PHIsInCycle);
|
|
|
|
bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
|
|
|
|
bool OptimizeBB(MachineBasicBlock &MBB);
|
2010-02-12 01:30:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char OptimizePHIs::ID = 0;
|
2012-02-08 21:23:13 +00:00
|
|
|
char &llvm::OptimizePHIsID = OptimizePHIs::ID;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(OptimizePHIs, "opt-phis",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Optimize machine instruction PHIs", false, false)
|
2010-02-12 01:30:21 +00:00
|
|
|
|
|
|
|
bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
|
2014-03-31 17:43:35 +00:00
|
|
|
if (skipOptnoneFunction(*Fn.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
2010-02-12 01:30:21 +00:00
|
|
|
MRI = &Fn.getRegInfo();
|
2014-08-05 02:39:49 +00:00
|
|
|
TII = Fn.getSubtarget().getInstrInfo();
|
2010-02-12 01:30:21 +00:00
|
|
|
|
2010-02-13 00:31:44 +00:00
|
|
|
// Find dead PHI cycles and PHI cycles that can be replaced by a single
|
|
|
|
// value. InstCombine does these optimizations, but DAG legalization may
|
|
|
|
// introduce new opportunities, e.g., when i64 values are split up for
|
|
|
|
// 32-bit targets.
|
2010-02-12 01:30:21 +00:00
|
|
|
bool Changed = false;
|
|
|
|
for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
|
2010-02-13 00:31:44 +00:00
|
|
|
Changed |= OptimizeBB(*I);
|
2010-02-12 01:30:21 +00:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
|
|
|
|
/// are copies of SingleValReg, possibly via copies through other PHIs. If
|
|
|
|
/// SingleValReg is zero on entry, it is set to the register with the single
|
2010-02-13 00:31:44 +00:00
|
|
|
/// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
|
2010-02-12 01:30:21 +00:00
|
|
|
/// have been scanned.
|
2010-02-13 00:31:44 +00:00
|
|
|
bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
|
2010-02-12 01:30:21 +00:00
|
|
|
unsigned &SingleValReg,
|
2010-02-13 00:31:44 +00:00
|
|
|
InstrSet &PHIsInCycle) {
|
2010-02-12 01:30:21 +00:00
|
|
|
assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
|
|
|
|
// See if we already saw this register.
|
2014-11-19 07:49:26 +00:00
|
|
|
if (!PHIsInCycle.insert(MI).second)
|
2010-02-12 01:30:21 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Don't scan crazily complex things.
|
2010-02-13 00:31:44 +00:00
|
|
|
if (PHIsInCycle.size() == 16)
|
2010-02-12 01:30:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Scan the PHI operands.
|
|
|
|
for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
|
|
|
|
unsigned SrcReg = MI->getOperand(i).getReg();
|
|
|
|
if (SrcReg == DstReg)
|
|
|
|
continue;
|
2010-02-13 00:31:44 +00:00
|
|
|
MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
|
2010-02-12 01:30:21 +00:00
|
|
|
|
|
|
|
// Skip over register-to-register moves.
|
2010-07-16 04:45:42 +00:00
|
|
|
if (SrcMI && SrcMI->isCopy() &&
|
|
|
|
!SrcMI->getOperand(0).getSubReg() &&
|
|
|
|
!SrcMI->getOperand(1).getSubReg() &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
|
2010-07-03 00:04:37 +00:00
|
|
|
SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
|
2010-02-12 01:30:21 +00:00
|
|
|
if (!SrcMI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (SrcMI->isPHI()) {
|
2010-02-13 00:31:44 +00:00
|
|
|
if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
|
2010-02-12 01:30:21 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Fail if there is more than one non-phi/non-move register.
|
|
|
|
if (SingleValReg != 0)
|
|
|
|
return false;
|
|
|
|
SingleValReg = SrcReg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-13 00:31:44 +00:00
|
|
|
/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
|
|
|
|
/// other PHIs in a cycle.
|
|
|
|
bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
|
|
|
|
assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
|
|
|
|
"PHI destination is not a virtual register");
|
|
|
|
|
|
|
|
// See if we already saw this register.
|
2014-11-19 07:49:26 +00:00
|
|
|
if (!PHIsInCycle.insert(MI).second)
|
2010-02-13 00:31:44 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Don't scan crazily complex things.
|
|
|
|
if (PHIsInCycle.size() == 16)
|
|
|
|
return false;
|
|
|
|
|
2014-03-17 19:36:09 +00:00
|
|
|
for (MachineInstr &UseMI : MRI->use_instructions(DstReg)) {
|
|
|
|
if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
|
2010-02-13 00:31:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
|
|
|
|
/// a single value.
|
|
|
|
bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
|
2010-02-12 01:30:21 +00:00
|
|
|
bool Changed = false;
|
|
|
|
for (MachineBasicBlock::iterator
|
|
|
|
MII = MBB.begin(), E = MBB.end(); MII != E; ) {
|
|
|
|
MachineInstr *MI = &*MII++;
|
|
|
|
if (!MI->isPHI())
|
|
|
|
break;
|
|
|
|
|
2010-02-13 00:31:44 +00:00
|
|
|
// Check for single-value PHI cycles.
|
2010-02-12 01:30:21 +00:00
|
|
|
unsigned SingleValReg = 0;
|
2010-02-13 00:31:44 +00:00
|
|
|
InstrSet PHIsInCycle;
|
|
|
|
if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
|
2010-02-12 01:30:21 +00:00
|
|
|
SingleValReg != 0) {
|
2011-10-17 21:54:46 +00:00
|
|
|
unsigned OldReg = MI->getOperand(0).getReg();
|
|
|
|
if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MRI->replaceRegWith(OldReg, SingleValReg);
|
2010-02-12 01:30:21 +00:00
|
|
|
MI->eraseFromParent();
|
|
|
|
++NumPHICycles;
|
|
|
|
Changed = true;
|
2010-02-13 00:31:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for dead PHI cycles.
|
|
|
|
PHIsInCycle.clear();
|
|
|
|
if (IsDeadPHICycle(MI, PHIsInCycle)) {
|
|
|
|
for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
MachineInstr *PhiMI = *PI;
|
|
|
|
if (&*MII == PhiMI)
|
|
|
|
++MII;
|
|
|
|
PhiMI->eraseFromParent();
|
|
|
|
}
|
|
|
|
++NumDeadPHICycles;
|
|
|
|
Changed = true;
|
2010-02-12 01:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|