2017-08-02 05:20:10 +08:00
|
|
|
//===- RDFCopy.cpp --------------------------------------------------------===//
|
2016-01-13 01:23:48 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-01-19 04:43:57 +08:00
|
|
|
// RDF-based copy propagation.
|
2017-08-02 05:20:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-01-13 01:23:48 +08:00
|
|
|
|
|
|
|
#include "RDFCopy.h"
|
|
|
|
#include "RDFGraph.h"
|
2017-03-11 06:44:24 +08:00
|
|
|
#include "RDFLiveness.h"
|
2017-08-02 05:20:10 +08:00
|
|
|
#include "RDFRegisters.h"
|
2016-01-13 01:23:48 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2017-08-02 05:20:10 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2018-01-31 07:19:44 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2017-08-02 05:20:10 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2016-01-13 01:23:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-08-02 05:20:10 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
|
|
|
|
2016-05-27 18:06:40 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace rdf;
|
2016-01-13 01:23:48 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static cl::opt<unsigned> CpLimit("rdf-cp-limit", cl::init(0), cl::Hidden);
|
|
|
|
static unsigned CpCount = 0;
|
|
|
|
#endif
|
|
|
|
|
2016-01-19 04:43:57 +08:00
|
|
|
bool CopyPropagation::interpretAsCopy(const MachineInstr *MI, EqualityMap &EM) {
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
switch (Opc) {
|
|
|
|
case TargetOpcode::COPY: {
|
|
|
|
const MachineOperand &Dst = MI->getOperand(0);
|
|
|
|
const MachineOperand &Src = MI->getOperand(1);
|
2016-10-15 01:57:55 +08:00
|
|
|
RegisterRef DstR = DFG.makeRegRef(Dst.getReg(), Dst.getSubReg());
|
|
|
|
RegisterRef SrcR = DFG.makeRegRef(Src.getReg(), Src.getSubReg());
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(DstR.Reg));
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SrcR.Reg));
|
|
|
|
const TargetRegisterInfo &TRI = DFG.getTRI();
|
|
|
|
if (TRI.getMinimalPhysRegClass(DstR.Reg) !=
|
|
|
|
TRI.getMinimalPhysRegClass(SrcR.Reg))
|
2016-01-19 04:43:57 +08:00
|
|
|
return false;
|
|
|
|
EM.insert(std::make_pair(DstR, SrcR));
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-15 01:57:55 +08:00
|
|
|
case TargetOpcode::REG_SEQUENCE:
|
|
|
|
llvm_unreachable("Unexpected REG_SEQUENCE");
|
2016-01-19 04:43:57 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyPropagation::recordCopy(NodeAddr<StmtNode*> SA, EqualityMap &EM) {
|
|
|
|
CopyMap.insert(std::make_pair(SA.Id, EM));
|
2016-01-13 01:23:48 +08:00
|
|
|
Copies.push_back(SA.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CopyPropagation::scanBlock(MachineBasicBlock *B) {
|
|
|
|
bool Changed = false;
|
2017-04-19 23:11:23 +08:00
|
|
|
NodeAddr<BlockNode*> BA = DFG.findBlock(B);
|
2016-01-13 01:23:48 +08:00
|
|
|
|
|
|
|
for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
|
|
|
|
if (DFG.IsCode<NodeAttrs::Stmt>(IA)) {
|
|
|
|
NodeAddr<StmtNode*> SA = IA;
|
2016-01-19 04:43:57 +08:00
|
|
|
EqualityMap EM;
|
|
|
|
if (interpretAsCopy(SA.Addr->getCode(), EM))
|
|
|
|
recordCopy(SA, EM);
|
2016-01-13 01:23:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineDomTreeNode *N = MDT.getNode(B);
|
|
|
|
for (auto I : *N)
|
|
|
|
Changed |= scanBlock(I->getBlock());
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:44:24 +08:00
|
|
|
NodeId CopyPropagation::getLocalReachingDef(RegisterRef RefRR,
|
|
|
|
NodeAddr<InstrNode*> IA) {
|
|
|
|
NodeAddr<RefNode*> RA = L.getNearestAliasedRef(RefRR, IA);
|
|
|
|
if (RA.Id != 0) {
|
|
|
|
if (RA.Addr->getKind() == NodeAttrs::Def)
|
|
|
|
return RA.Id;
|
|
|
|
assert(RA.Addr->getKind() == NodeAttrs::Use);
|
|
|
|
if (NodeId RD = RA.Addr->getReachingDef())
|
|
|
|
return RD;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:23:48 +08:00
|
|
|
bool CopyPropagation::run() {
|
|
|
|
scanBlock(&DFG.getMF().front());
|
|
|
|
|
|
|
|
if (trace()) {
|
|
|
|
dbgs() << "Copies:\n";
|
2018-01-31 07:19:44 +08:00
|
|
|
for (NodeId I : Copies) {
|
2016-01-19 04:43:57 +08:00
|
|
|
dbgs() << "Instr: " << *DFG.addr<StmtNode*>(I).Addr->getCode();
|
|
|
|
dbgs() << " eq: {";
|
|
|
|
for (auto J : CopyMap[I])
|
|
|
|
dbgs() << ' ' << Print<RegisterRef>(J.first, DFG) << '='
|
|
|
|
<< Print<RegisterRef>(J.second, DFG);
|
|
|
|
dbgs() << " }\n";
|
|
|
|
}
|
2016-01-13 01:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
bool HasLimit = CpLimit.getNumOccurrences() > 0;
|
|
|
|
#endif
|
|
|
|
|
2016-10-15 01:57:55 +08:00
|
|
|
auto MinPhysReg = [this] (RegisterRef RR) -> unsigned {
|
|
|
|
const TargetRegisterInfo &TRI = DFG.getTRI();
|
|
|
|
const TargetRegisterClass &RC = *TRI.getMinimalPhysRegClass(RR.Reg);
|
|
|
|
if ((RC.LaneMask & RR.Mask) == RC.LaneMask)
|
|
|
|
return RR.Reg;
|
|
|
|
for (MCSubRegIndexIterator S(RR.Reg, &TRI); S.isValid(); ++S)
|
|
|
|
if (RR.Mask == TRI.getSubRegIndexLaneMask(S.getSubRegIndex()))
|
|
|
|
return S.getSubReg();
|
|
|
|
llvm_unreachable("Should have found a register");
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2018-01-31 07:19:44 +08:00
|
|
|
for (NodeId C : Copies) {
|
2016-01-13 01:23:48 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (HasLimit && CpCount >= CpLimit)
|
|
|
|
break;
|
|
|
|
#endif
|
2016-01-19 04:43:57 +08:00
|
|
|
auto SA = DFG.addr<InstrNode*>(C);
|
|
|
|
auto FS = CopyMap.find(SA.Id);
|
|
|
|
if (FS == CopyMap.end())
|
2016-01-13 01:23:48 +08:00
|
|
|
continue;
|
2016-01-19 04:43:57 +08:00
|
|
|
|
|
|
|
EqualityMap &EM = FS->second;
|
|
|
|
for (NodeAddr<DefNode*> DA : SA.Addr->members_if(DFG.IsDef, DFG)) {
|
2016-10-15 01:57:55 +08:00
|
|
|
RegisterRef DR = DA.Addr->getRegRef(DFG);
|
2016-01-19 04:43:57 +08:00
|
|
|
auto FR = EM.find(DR);
|
|
|
|
if (FR == EM.end())
|
2016-01-13 01:23:48 +08:00
|
|
|
continue;
|
2016-01-19 04:43:57 +08:00
|
|
|
RegisterRef SR = FR->second;
|
|
|
|
if (DR == SR)
|
2016-01-13 01:23:48 +08:00
|
|
|
continue;
|
|
|
|
|
2017-03-11 06:44:24 +08:00
|
|
|
NodeId AtCopy = getLocalReachingDef(SR, SA);
|
2016-01-19 04:43:57 +08:00
|
|
|
|
|
|
|
for (NodeId N = DA.Addr->getReachedUse(), NextN; N; N = NextN) {
|
|
|
|
auto UA = DFG.addr<UseNode*>(N);
|
|
|
|
NextN = UA.Addr->getSibling();
|
|
|
|
uint16_t F = UA.Addr->getFlags();
|
|
|
|
if ((F & NodeAttrs::PhiRef) || (F & NodeAttrs::Fixed))
|
|
|
|
continue;
|
2016-10-15 01:57:55 +08:00
|
|
|
if (UA.Addr->getRegRef(DFG) != DR)
|
2016-01-19 04:43:57 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
NodeAddr<InstrNode*> IA = UA.Addr->getOwner(DFG);
|
|
|
|
assert(DFG.IsCode<NodeAttrs::Stmt>(IA));
|
2017-03-11 06:44:24 +08:00
|
|
|
NodeId AtUse = getLocalReachingDef(SR, IA);
|
|
|
|
if (AtCopy != AtUse)
|
2016-01-19 04:43:57 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
MachineOperand &Op = UA.Addr->getOp();
|
|
|
|
if (Op.isTied())
|
|
|
|
continue;
|
|
|
|
if (trace()) {
|
|
|
|
dbgs() << "Can replace " << Print<RegisterRef>(DR, DFG)
|
|
|
|
<< " with " << Print<RegisterRef>(SR, DFG) << " in "
|
|
|
|
<< *NodeAddr<StmtNode*>(IA).Addr->getCode();
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:57:55 +08:00
|
|
|
unsigned NewReg = MinPhysReg(SR);
|
|
|
|
Op.setReg(NewReg);
|
|
|
|
Op.setSubReg(0);
|
2016-01-19 04:43:57 +08:00
|
|
|
DFG.unlinkUse(UA, false);
|
2017-03-11 06:44:24 +08:00
|
|
|
if (AtCopy != 0) {
|
|
|
|
UA.Addr->linkToDef(UA.Id, DFG.addr<DefNode*>(AtCopy));
|
2016-04-28 23:09:19 +08:00
|
|
|
} else {
|
|
|
|
UA.Addr->setReachingDef(0);
|
|
|
|
UA.Addr->setSibling(0);
|
|
|
|
}
|
2016-01-19 04:43:57 +08:00
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (HasLimit && CpCount >= CpLimit)
|
|
|
|
break;
|
|
|
|
CpCount++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto FC = CopyMap.find(IA.Id);
|
|
|
|
if (FC != CopyMap.end()) {
|
|
|
|
// Update the EM map in the copy's entry.
|
|
|
|
auto &M = FC->second;
|
|
|
|
for (auto &J : M) {
|
|
|
|
if (J.second != DR)
|
|
|
|
continue;
|
|
|
|
J.second = SR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // for (N in reached-uses)
|
|
|
|
} // for (DA in defs)
|
|
|
|
} // for (C in Copies)
|
2016-01-13 01:23:48 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|