2009-11-03 23:52:08 +00:00
|
|
|
//===---------------------- ProcessImplicitDefs.cpp -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "processimplicitdefs"
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/ProcessImplicitDefs.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char ProcessImplicitDefs::ID = 0;
|
2012-02-10 04:10:36 +00:00
|
|
|
char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
|
|
|
|
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
|
2010-12-29 11:49:10 +00:00
|
|
|
"Process Implicit Definitions", false, false)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveVariables)
|
|
|
|
INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
|
2010-12-29 11:49:10 +00:00
|
|
|
"Process Implicit Definitions", false, false)
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
|
|
|
AU.addPreserved<LiveVariables>();
|
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
|
|
|
AU.addPreservedID(TwoAddressInstructionPassID);
|
|
|
|
AU.addPreservedID(PHIEliminationID);
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2010-07-14 01:22:19 +00:00
|
|
|
bool
|
|
|
|
ProcessImplicitDefs::CanTurnIntoImplicitDef(MachineInstr *MI,
|
|
|
|
unsigned Reg, unsigned OpIdx,
|
|
|
|
SmallSet<unsigned, 8> &ImpDefRegs) {
|
2010-07-03 00:04:37 +00:00
|
|
|
switch(OpIdx) {
|
2010-07-14 01:22:19 +00:00
|
|
|
case 1:
|
2012-01-25 23:36:27 +00:00
|
|
|
return MI->isCopy() && (!MI->getOperand(0).readsReg() ||
|
2010-07-14 01:22:19 +00:00
|
|
|
ImpDefRegs.count(MI->getOperand(0).getReg()));
|
|
|
|
case 2:
|
2012-01-25 23:36:27 +00:00
|
|
|
return MI->isSubregToReg() && (!MI->getOperand(0).readsReg() ||
|
2010-07-14 01:22:19 +00:00
|
|
|
ImpDefRegs.count(MI->getOperand(0).getReg()));
|
|
|
|
default: return false;
|
2010-07-03 00:04:37 +00:00
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 01:22:19 +00:00
|
|
|
static bool isUndefCopy(MachineInstr *MI, unsigned Reg,
|
|
|
|
SmallSet<unsigned, 8> &ImpDefRegs) {
|
|
|
|
if (MI->isCopy()) {
|
|
|
|
MachineOperand &MO0 = MI->getOperand(0);
|
|
|
|
MachineOperand &MO1 = MI->getOperand(1);
|
|
|
|
if (MO1.getReg() != Reg)
|
|
|
|
return false;
|
2012-01-25 23:36:27 +00:00
|
|
|
if (!MO0.readsReg() || ImpDefRegs.count(MO0.getReg()))
|
2010-07-14 01:22:19 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
|
|
|
|
/// there is one implicit_def for each use. Add isUndef marker to
|
|
|
|
/// implicit_def defs and their uses.
|
|
|
|
bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &fn) {
|
|
|
|
|
2010-01-05 01:24:28 +00:00
|
|
|
DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
|
2009-11-03 23:52:08 +00:00
|
|
|
<< "********** Function: "
|
|
|
|
<< ((Value*)fn.getFunction())->getName() << '\n');
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2011-03-14 20:57:14 +00:00
|
|
|
TII = fn.getTarget().getInstrInfo();
|
|
|
|
TRI = fn.getTarget().getRegisterInfo();
|
|
|
|
MRI = &fn.getRegInfo();
|
2012-02-10 04:10:36 +00:00
|
|
|
LV = getAnalysisIfAvailable<LiveVariables>();
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
SmallSet<unsigned, 8> ImpDefRegs;
|
|
|
|
SmallVector<MachineInstr*, 8> ImpDefMIs;
|
2009-11-25 21:13:39 +00:00
|
|
|
SmallVector<MachineInstr*, 4> RUses;
|
2009-11-03 23:52:08 +00:00
|
|
|
SmallPtrSet<MachineBasicBlock*,16> Visited;
|
2009-11-16 05:52:06 +00:00
|
|
|
SmallPtrSet<MachineInstr*, 8> ModInsts;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2009-11-25 21:13:39 +00:00
|
|
|
MachineBasicBlock *Entry = fn.begin();
|
2009-11-03 23:52:08 +00:00
|
|
|
for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
|
|
|
|
DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
|
|
|
|
DFI != E; ++DFI) {
|
|
|
|
MachineBasicBlock *MBB = *DFI;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
|
|
|
|
I != E; ) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
++I;
|
2010-02-09 19:54:29 +00:00
|
|
|
if (MI->isImplicitDef()) {
|
2012-01-25 23:36:27 +00:00
|
|
|
ImpDefMIs.push_back(MI);
|
|
|
|
// Is this a sub-register read-modify-write?
|
|
|
|
if (MI->getOperand(0).readsReg())
|
2010-05-10 21:25:30 +00:00
|
|
|
continue;
|
2009-11-03 23:52:08 +00:00
|
|
|
unsigned Reg = MI->getOperand(0).getReg();
|
|
|
|
ImpDefRegs.insert(Reg);
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
2012-03-05 05:37:41 +00:00
|
|
|
for (const uint16_t *SS = TRI->getSubRegisters(Reg); *SS; ++SS)
|
2009-11-03 23:52:08 +00:00
|
|
|
ImpDefRegs.insert(*SS);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-07-06 23:26:25 +00:00
|
|
|
// Eliminate %reg1032:sub<def> = COPY undef.
|
2012-01-25 23:36:27 +00:00
|
|
|
if (MI->isCopy() && MI->getOperand(0).readsReg()) {
|
2010-07-06 23:26:25 +00:00
|
|
|
MachineOperand &MO = MI->getOperand(1);
|
2010-07-14 01:22:19 +00:00
|
|
|
if (MO.isUndef() || ImpDefRegs.count(MO.getReg())) {
|
2012-02-10 04:10:36 +00:00
|
|
|
if (LV && MO.isKill()) {
|
2011-03-14 20:57:14 +00:00
|
|
|
LiveVariables::VarInfo& vi = LV->getVarInfo(MO.getReg());
|
2010-07-06 23:26:25 +00:00
|
|
|
vi.removeKill(MI);
|
|
|
|
}
|
2011-07-28 21:38:51 +00:00
|
|
|
unsigned Reg = MI->getOperand(0).getReg();
|
2010-07-06 23:26:25 +00:00
|
|
|
MI->eraseFromParent();
|
|
|
|
Changed = true;
|
2011-07-28 21:38:51 +00:00
|
|
|
|
|
|
|
// A REG_SEQUENCE may have been expanded into partial definitions.
|
|
|
|
// If this was the last one, mark Reg as implicitly defined.
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg) && MRI->def_empty(Reg))
|
|
|
|
ImpDefRegs.insert(Reg);
|
2010-07-06 23:26:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
bool ChangedToImpDef = false;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2012-01-25 23:36:27 +00:00
|
|
|
if (!MO.isReg() || !MO.readsReg())
|
2009-11-03 23:52:08 +00:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (!ImpDefRegs.count(Reg))
|
|
|
|
continue;
|
|
|
|
// Use is a copy, just turn it into an implicit_def.
|
2011-03-14 20:57:14 +00:00
|
|
|
if (CanTurnIntoImplicitDef(MI, Reg, i, ImpDefRegs)) {
|
2009-11-03 23:52:08 +00:00
|
|
|
bool isKill = MO.isKill();
|
2011-03-14 20:57:14 +00:00
|
|
|
MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
|
2009-11-03 23:52:08 +00:00
|
|
|
for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
|
|
|
|
MI->RemoveOperand(j);
|
|
|
|
if (isKill) {
|
|
|
|
ImpDefRegs.erase(Reg);
|
2012-02-10 04:10:36 +00:00
|
|
|
if (LV) {
|
|
|
|
LiveVariables::VarInfo& vi = LV->getVarInfo(Reg);
|
|
|
|
vi.removeKill(MI);
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
ChangedToImpDef = true;
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
MO.setIsUndef();
|
2010-07-06 23:26:25 +00:00
|
|
|
// This is a partial register redef of an implicit def.
|
|
|
|
// Make sure the whole register is defined by the instruction.
|
|
|
|
if (MO.isDef()) {
|
|
|
|
MI->addRegisterDefined(Reg);
|
|
|
|
continue;
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
|
2012-01-25 23:36:27 +00:00
|
|
|
// Make sure other reads of Reg are also marked <undef>.
|
2009-11-03 23:52:08 +00:00
|
|
|
for (unsigned j = i+1; j != e; ++j) {
|
|
|
|
MachineOperand &MOJ = MI->getOperand(j);
|
2012-01-25 23:36:27 +00:00
|
|
|
if (MOJ.isReg() && MOJ.getReg() == Reg && MOJ.readsReg())
|
2009-11-03 23:52:08 +00:00
|
|
|
MOJ.setIsUndef();
|
|
|
|
}
|
|
|
|
ImpDefRegs.erase(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChangedToImpDef) {
|
|
|
|
// Backtrack to process this new implicit_def.
|
|
|
|
--I;
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.isDef())
|
|
|
|
continue;
|
|
|
|
ImpDefRegs.erase(MO.getReg());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any outstanding liveout implicit_def's?
|
|
|
|
for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = ImpDefMIs[i];
|
|
|
|
unsigned Reg = MI->getOperand(0).getReg();
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
|
|
|
|
!ImpDefRegs.count(Reg)) {
|
|
|
|
// Delete all "local" implicit_def's. That include those which define
|
|
|
|
// physical registers since they cannot be liveout.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are multiple defs of the same register and at least one
|
|
|
|
// is not an implicit_def, do not insert implicit_def's before the
|
|
|
|
// uses.
|
|
|
|
bool Skip = false;
|
2009-11-26 00:32:36 +00:00
|
|
|
SmallVector<MachineInstr*, 4> DeadImpDefs;
|
2011-03-14 20:57:14 +00:00
|
|
|
for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(Reg),
|
|
|
|
DE = MRI->def_end(); DI != DE; ++DI) {
|
2009-11-26 00:32:36 +00:00
|
|
|
MachineInstr *DeadImpDef = &*DI;
|
2010-02-09 19:54:29 +00:00
|
|
|
if (!DeadImpDef->isImplicitDef()) {
|
2009-11-03 23:52:08 +00:00
|
|
|
Skip = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-26 00:32:36 +00:00
|
|
|
DeadImpDefs.push_back(DeadImpDef);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
if (Skip)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// The only implicit_def which we want to keep are those that are live
|
|
|
|
// out of its block.
|
2009-11-26 00:32:36 +00:00
|
|
|
for (unsigned j = 0, ee = DeadImpDefs.size(); j != ee; ++j)
|
|
|
|
DeadImpDefs[j]->eraseFromParent();
|
2009-11-03 23:52:08 +00:00
|
|
|
Changed = true;
|
|
|
|
|
2009-11-25 21:13:39 +00:00
|
|
|
// Process each use instruction once.
|
2011-03-14 20:57:14 +00:00
|
|
|
for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
|
|
|
|
UE = MRI->use_end(); UI != UE; ++UI) {
|
2010-02-15 22:03:29 +00:00
|
|
|
if (UI.getOperand().isUndef())
|
2009-11-03 23:52:08 +00:00
|
|
|
continue;
|
2010-02-15 22:03:29 +00:00
|
|
|
MachineInstr *RMI = &*UI;
|
2009-11-25 21:13:39 +00:00
|
|
|
if (ModInsts.insert(RMI))
|
|
|
|
RUses.push_back(RMI);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = RUses.size(); i != e; ++i) {
|
|
|
|
MachineInstr *RMI = RUses[i];
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
// Turn a copy use into an implicit_def.
|
2011-03-14 20:57:14 +00:00
|
|
|
if (isUndefCopy(RMI, Reg, ImpDefRegs)) {
|
|
|
|
RMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
|
2009-11-25 21:13:39 +00:00
|
|
|
|
|
|
|
bool isKill = false;
|
|
|
|
SmallVector<unsigned, 4> Ops;
|
|
|
|
for (unsigned j = 0, ee = RMI->getNumOperands(); j != ee; ++j) {
|
|
|
|
MachineOperand &RRMO = RMI->getOperand(j);
|
|
|
|
if (RRMO.isReg() && RRMO.getReg() == Reg) {
|
|
|
|
Ops.push_back(j);
|
|
|
|
if (RRMO.isKill())
|
|
|
|
isKill = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Leave the other operands along.
|
|
|
|
for (unsigned j = 0, ee = Ops.size(); j != ee; ++j) {
|
|
|
|
unsigned OpIdx = Ops[j];
|
|
|
|
RMI->RemoveOperand(OpIdx-j);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update LiveVariables varinfo if the instruction is a kill.
|
2012-02-10 04:10:36 +00:00
|
|
|
if (LV && isKill) {
|
2011-03-14 20:57:14 +00:00
|
|
|
LiveVariables::VarInfo& vi = LV->getVarInfo(Reg);
|
2009-11-16 02:07:31 +00:00
|
|
|
vi.removeKill(RMI);
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:13:39 +00:00
|
|
|
// Replace Reg with a new vreg that's marked implicit.
|
2011-03-14 20:57:14 +00:00
|
|
|
const TargetRegisterClass* RC = MRI->getRegClass(Reg);
|
|
|
|
unsigned NewVReg = MRI->createVirtualRegister(RC);
|
2009-11-25 21:13:39 +00:00
|
|
|
bool isKill = true;
|
|
|
|
for (unsigned j = 0, ee = RMI->getNumOperands(); j != ee; ++j) {
|
|
|
|
MachineOperand &RRMO = RMI->getOperand(j);
|
|
|
|
if (RRMO.isReg() && RRMO.getReg() == Reg) {
|
|
|
|
RRMO.setReg(NewVReg);
|
|
|
|
RRMO.setIsUndef();
|
|
|
|
if (isKill) {
|
|
|
|
// Only the first operand of NewVReg is marked kill.
|
|
|
|
RRMO.setIsKill();
|
|
|
|
isKill = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
2009-11-25 21:13:39 +00:00
|
|
|
RUses.clear();
|
2010-02-04 18:46:28 +00:00
|
|
|
ModInsts.clear();
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
ImpDefRegs.clear();
|
|
|
|
ImpDefMIs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|