2017-07-29 02:56:56 +02:00
|
|
|
//===- HexagonRDFOpt.cpp --------------------------------------------------===//
|
2016-01-12 20:09:01 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-01-12 20:09:01 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "HexagonInstrInfo.h"
|
|
|
|
#include "HexagonSubtarget.h"
|
2017-07-29 02:56:56 +02:00
|
|
|
#include "MCTargetDesc/HexagonBaseInfo.h"
|
2016-01-12 20:09:01 +01:00
|
|
|
#include "RDFCopy.h"
|
|
|
|
#include "RDFDeadCode.h"
|
2017-07-29 02:56:56 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-01-12 20:09:01 +01:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominanceFrontier.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-07-29 02:56:56 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2016-01-12 20:09:01 +01:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2020-03-17 19:45:11 +01:00
|
|
|
#include "llvm/CodeGen/RDFGraph.h"
|
|
|
|
#include "llvm/CodeGen/RDFLiveness.h"
|
|
|
|
#include "llvm/CodeGen/RDFRegisters.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-07-29 02:56:56 +02:00
|
|
|
#include "llvm/Pass.h"
|
2016-01-12 20:09:01 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-07-29 02:56:56 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
|
|
|
#include <utility>
|
2016-01-12 20:09:01 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace rdf;
|
|
|
|
|
|
|
|
namespace llvm {
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
void initializeHexagonRDFOptPass(PassRegistry&);
|
|
|
|
FunctionPass *createHexagonRDFOpt();
|
2017-07-29 02:56:56 +02:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
static unsigned RDFCount = 0;
|
|
|
|
|
|
|
|
static cl::opt<unsigned> RDFLimit("rdf-limit",
|
|
|
|
cl::init(std::numeric_limits<unsigned>::max()));
|
|
|
|
static cl::opt<bool> RDFDump("rdf-dump", cl::init(false));
|
2016-01-12 20:09:01 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class HexagonRDFOpt : public MachineFunctionPass {
|
|
|
|
public:
|
2017-10-30 15:11:52 +01:00
|
|
|
HexagonRDFOpt() : MachineFunctionPass(ID) {}
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
|
|
|
AU.addRequired<MachineDominanceFrontier>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override {
|
2016-01-12 20:09:01 +01:00
|
|
|
return "Hexagon RDF optimizations";
|
|
|
|
}
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
2016-04-04 19:09:25 +02:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 03:27:13 +02:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-04 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
static char ID;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MachineDominatorTree *MDT;
|
|
|
|
MachineRegisterInfo *MRI;
|
|
|
|
};
|
|
|
|
|
2016-01-18 21:45:51 +01:00
|
|
|
struct HexagonCP : public CopyPropagation {
|
|
|
|
HexagonCP(DataFlowGraph &G) : CopyPropagation(G) {}
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-01-18 21:45:51 +01:00
|
|
|
bool interpretAsCopy(const MachineInstr *MI, EqualityMap &EM) override;
|
|
|
|
};
|
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
struct HexagonDCE : public DeadCodeElimination {
|
2016-01-12 20:27:59 +01:00
|
|
|
HexagonDCE(DataFlowGraph &G, MachineRegisterInfo &MRI)
|
|
|
|
: DeadCodeElimination(G, MRI) {}
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
bool rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove);
|
|
|
|
void removeOperand(NodeAddr<InstrNode*> IA, unsigned OpNum);
|
|
|
|
|
|
|
|
bool run();
|
|
|
|
};
|
2017-07-29 02:56:56 +02:00
|
|
|
|
2016-02-05 14:50:53 +01:00
|
|
|
} // end anonymous namespace
|
2016-01-12 20:09:01 +01:00
|
|
|
|
2017-07-29 02:56:56 +02:00
|
|
|
char HexagonRDFOpt::ID = 0;
|
|
|
|
|
2017-10-30 15:11:52 +01:00
|
|
|
INITIALIZE_PASS_BEGIN(HexagonRDFOpt, "hexagon-rdf-opt",
|
|
|
|
"Hexagon RDF optimizations", false, false)
|
2017-07-29 02:56:56 +02:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
|
2017-10-30 15:11:52 +01:00
|
|
|
INITIALIZE_PASS_END(HexagonRDFOpt, "hexagon-rdf-opt",
|
|
|
|
"Hexagon RDF optimizations", false, false)
|
2016-01-12 20:09:01 +01:00
|
|
|
|
2016-01-18 21:45:51 +01:00
|
|
|
bool HexagonCP::interpretAsCopy(const MachineInstr *MI, EqualityMap &EM) {
|
2017-01-13 18:12:16 +01:00
|
|
|
auto mapRegs = [&EM] (RegisterRef DstR, RegisterRef SrcR) -> void {
|
2016-01-18 21:45:51 +01:00
|
|
|
EM.insert(std::make_pair(DstR, SrcR));
|
|
|
|
};
|
|
|
|
|
2016-10-14 19:57:55 +02:00
|
|
|
DataFlowGraph &DFG = getDFG();
|
2016-01-18 21:45:51 +01:00
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
switch (Opc) {
|
|
|
|
case Hexagon::A2_combinew: {
|
|
|
|
const MachineOperand &DstOp = MI->getOperand(0);
|
|
|
|
const MachineOperand &HiOp = MI->getOperand(1);
|
|
|
|
const MachineOperand &LoOp = MI->getOperand(2);
|
|
|
|
assert(DstOp.getSubReg() == 0 && "Unexpected subregister");
|
2016-11-09 17:19:08 +01:00
|
|
|
mapRegs(DFG.makeRegRef(DstOp.getReg(), Hexagon::isub_hi),
|
2016-10-14 19:57:55 +02:00
|
|
|
DFG.makeRegRef(HiOp.getReg(), HiOp.getSubReg()));
|
2016-11-09 17:19:08 +01:00
|
|
|
mapRegs(DFG.makeRegRef(DstOp.getReg(), Hexagon::isub_lo),
|
2016-10-14 19:57:55 +02:00
|
|
|
DFG.makeRegRef(LoOp.getReg(), LoOp.getSubReg()));
|
2016-01-18 21:45:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Hexagon::A2_addi: {
|
|
|
|
const MachineOperand &A = MI->getOperand(2);
|
|
|
|
if (!A.isImm() || A.getImm() != 0)
|
|
|
|
return false;
|
2016-08-17 22:30:52 +02:00
|
|
|
LLVM_FALLTHROUGH;
|
2016-01-18 21:45:51 +01:00
|
|
|
}
|
|
|
|
case Hexagon::A2_tfr: {
|
|
|
|
const MachineOperand &DstOp = MI->getOperand(0);
|
|
|
|
const MachineOperand &SrcOp = MI->getOperand(1);
|
2016-10-14 19:57:55 +02:00
|
|
|
mapRegs(DFG.makeRegRef(DstOp.getReg(), DstOp.getSubReg()),
|
|
|
|
DFG.makeRegRef(SrcOp.getReg(), SrcOp.getSubReg()));
|
2016-01-18 21:45:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CopyPropagation::interpretAsCopy(MI, EM);
|
|
|
|
}
|
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
bool HexagonDCE::run() {
|
|
|
|
bool Collected = collect();
|
|
|
|
if (!Collected)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const SetVector<NodeId> &DeadNodes = getDeadNodes();
|
|
|
|
const SetVector<NodeId> &DeadInstrs = getDeadInstrs();
|
|
|
|
|
2017-07-29 02:56:56 +02:00
|
|
|
using RefToInstrMap = DenseMap<NodeId, NodeId>;
|
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
RefToInstrMap R2I;
|
|
|
|
SetVector<NodeId> PartlyDead;
|
|
|
|
DataFlowGraph &DFG = getDFG();
|
|
|
|
|
|
|
|
for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
|
|
|
|
for (auto TA : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Stmt>, DFG)) {
|
|
|
|
NodeAddr<StmtNode*> SA = TA;
|
|
|
|
for (NodeAddr<RefNode*> RA : SA.Addr->members(DFG)) {
|
|
|
|
R2I.insert(std::make_pair(RA.Id, SA.Id));
|
|
|
|
if (DFG.IsDef(RA) && DeadNodes.count(RA.Id))
|
|
|
|
if (!DeadInstrs.count(SA.Id))
|
|
|
|
PartlyDead.insert(SA.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes to remove.
|
|
|
|
SetVector<NodeId> Remove = DeadInstrs;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
for (NodeId N : PartlyDead) {
|
|
|
|
auto SA = DFG.addr<StmtNode*>(N);
|
|
|
|
if (trace())
|
|
|
|
dbgs() << "Partly dead: " << *SA.Addr->getCode();
|
|
|
|
Changed |= rewrite(SA, Remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
return erase(Remove) || Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonDCE::removeOperand(NodeAddr<InstrNode*> IA, unsigned OpNum) {
|
|
|
|
MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
|
|
|
|
|
|
|
|
auto getOpNum = [MI] (MachineOperand &Op) -> unsigned {
|
|
|
|
for (unsigned i = 0, n = MI->getNumOperands(); i != n; ++i)
|
|
|
|
if (&MI->getOperand(i) == &Op)
|
|
|
|
return i;
|
|
|
|
llvm_unreachable("Invalid operand");
|
|
|
|
};
|
|
|
|
DenseMap<NodeId,unsigned> OpMap;
|
2016-10-14 19:57:55 +02:00
|
|
|
DataFlowGraph &DFG = getDFG();
|
|
|
|
NodeList Refs = IA.Addr->members(DFG);
|
2016-01-12 20:09:01 +01:00
|
|
|
for (NodeAddr<RefNode*> RA : Refs)
|
|
|
|
OpMap.insert(std::make_pair(RA.Id, getOpNum(RA.Addr->getOp())));
|
|
|
|
|
|
|
|
MI->RemoveOperand(OpNum);
|
|
|
|
|
|
|
|
for (NodeAddr<RefNode*> RA : Refs) {
|
|
|
|
unsigned N = OpMap[RA.Id];
|
|
|
|
if (N < OpNum)
|
2016-10-14 19:57:55 +02:00
|
|
|
RA.Addr->setRegRef(&MI->getOperand(N), DFG);
|
2016-01-12 20:09:01 +01:00
|
|
|
else if (N > OpNum)
|
2016-10-14 19:57:55 +02:00
|
|
|
RA.Addr->setRegRef(&MI->getOperand(N-1), DFG);
|
2016-01-12 20:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonDCE::rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove) {
|
|
|
|
if (!getDFG().IsCode<NodeAttrs::Stmt>(IA))
|
|
|
|
return false;
|
|
|
|
DataFlowGraph &DFG = getDFG();
|
2016-07-29 23:49:42 +02:00
|
|
|
MachineInstr &MI = *NodeAddr<StmtNode*>(IA).Addr->getCode();
|
2016-01-12 20:09:01 +01:00
|
|
|
auto &HII = static_cast<const HexagonInstrInfo&>(DFG.getTII());
|
|
|
|
if (HII.getAddrMode(MI) != HexagonII::PostInc)
|
|
|
|
return false;
|
2016-07-29 23:49:42 +02:00
|
|
|
unsigned Opc = MI.getOpcode();
|
2016-01-12 20:09:01 +01:00
|
|
|
unsigned OpNum, NewOpc;
|
|
|
|
switch (Opc) {
|
|
|
|
case Hexagon::L2_loadri_pi:
|
|
|
|
NewOpc = Hexagon::L2_loadri_io;
|
|
|
|
OpNum = 1;
|
|
|
|
break;
|
|
|
|
case Hexagon::L2_loadrd_pi:
|
|
|
|
NewOpc = Hexagon::L2_loadrd_io;
|
|
|
|
OpNum = 1;
|
|
|
|
break;
|
|
|
|
case Hexagon::V6_vL32b_pi:
|
|
|
|
NewOpc = Hexagon::V6_vL32b_ai;
|
|
|
|
OpNum = 1;
|
|
|
|
break;
|
|
|
|
case Hexagon::S2_storeri_pi:
|
|
|
|
NewOpc = Hexagon::S2_storeri_io;
|
|
|
|
OpNum = 0;
|
|
|
|
break;
|
|
|
|
case Hexagon::S2_storerd_pi:
|
|
|
|
NewOpc = Hexagon::S2_storerd_io;
|
|
|
|
OpNum = 0;
|
|
|
|
break;
|
|
|
|
case Hexagon::V6_vS32b_pi:
|
|
|
|
NewOpc = Hexagon::V6_vS32b_ai;
|
|
|
|
OpNum = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto IsDead = [this] (NodeAddr<DefNode*> DA) -> bool {
|
|
|
|
return getDeadNodes().count(DA.Id);
|
|
|
|
};
|
|
|
|
NodeList Defs;
|
2016-07-29 23:49:42 +02:00
|
|
|
MachineOperand &Op = MI.getOperand(OpNum);
|
2016-01-12 20:09:01 +01:00
|
|
|
for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG)) {
|
|
|
|
if (&DA.Addr->getOp() != &Op)
|
|
|
|
continue;
|
|
|
|
Defs = DFG.getRelatedRefs(IA, DA);
|
2017-07-29 02:56:56 +02:00
|
|
|
if (!llvm::all_of(Defs, IsDead))
|
2016-01-12 20:09:01 +01:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all nodes in Defs for removal.
|
|
|
|
for (auto D : Defs)
|
|
|
|
Remove.insert(D.Id);
|
|
|
|
|
|
|
|
if (trace())
|
2016-07-29 23:49:42 +02:00
|
|
|
dbgs() << "Rewriting: " << MI;
|
|
|
|
MI.setDesc(HII.get(NewOpc));
|
|
|
|
MI.getOperand(OpNum+2).setImm(0);
|
2016-01-12 20:09:01 +01:00
|
|
|
removeOperand(IA, OpNum);
|
|
|
|
if (trace())
|
2016-07-29 23:49:42 +02:00
|
|
|
dbgs() << " to: " << MI;
|
2016-01-12 20:09:01 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonRDFOpt::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-15 23:22:58 +01:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2016-04-26 21:46:28 +02:00
|
|
|
return false;
|
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
if (RDFLimit.getPosition()) {
|
|
|
|
if (RDFCount >= RDFLimit)
|
|
|
|
return false;
|
|
|
|
RDFCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
MDT = &getAnalysis<MachineDominatorTree>();
|
|
|
|
const auto &MDF = getAnalysis<MachineDominanceFrontier>();
|
|
|
|
const auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
|
|
|
|
const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
|
|
|
|
MRI = &MF.getRegInfo();
|
2016-01-18 21:45:51 +01:00
|
|
|
bool Changed;
|
2016-01-12 20:09:01 +01:00
|
|
|
|
|
|
|
if (RDFDump)
|
|
|
|
MF.print(dbgs() << "Before " << getPassName() << "\n", nullptr);
|
2016-01-18 21:45:51 +01:00
|
|
|
|
|
|
|
TargetOperandInfo TOI(HII);
|
2016-10-03 19:14:48 +02:00
|
|
|
DataFlowGraph G(MF, HII, HRI, *MDT, MDF, TOI);
|
2016-04-28 22:17:06 +02:00
|
|
|
// Dead phi nodes are necessary for copy propagation: we can add a use
|
|
|
|
// of a register in a block where it would need a phi node, but which
|
|
|
|
// was dead (and removed) during the graph build time.
|
|
|
|
G.build(BuildOptions::KeepDeadPhis);
|
2016-01-12 20:09:01 +01:00
|
|
|
|
2016-01-18 21:45:51 +01:00
|
|
|
if (RDFDump)
|
|
|
|
dbgs() << "Starting copy propagation on: " << MF.getName() << '\n'
|
|
|
|
<< PrintNode<FuncNode*>(G.getFunc(), G) << '\n';
|
|
|
|
HexagonCP CP(G);
|
2016-01-12 20:09:01 +01:00
|
|
|
CP.trace(RDFDump);
|
|
|
|
Changed = CP.run();
|
|
|
|
|
2016-01-18 21:45:51 +01:00
|
|
|
if (RDFDump)
|
|
|
|
dbgs() << "Starting dead code elimination on: " << MF.getName() << '\n'
|
|
|
|
<< PrintNode<FuncNode*>(G.getFunc(), G) << '\n';
|
2016-01-12 20:09:01 +01:00
|
|
|
HexagonDCE DCE(G, *MRI);
|
|
|
|
DCE.trace(RDFDump);
|
|
|
|
Changed |= DCE.run();
|
|
|
|
|
|
|
|
if (Changed) {
|
2016-01-18 21:45:51 +01:00
|
|
|
if (RDFDump)
|
|
|
|
dbgs() << "Starting liveness recomputation on: " << MF.getName() << '\n';
|
2016-01-12 20:09:01 +01:00
|
|
|
Liveness LV(*MRI, G);
|
|
|
|
LV.trace(RDFDump);
|
|
|
|
LV.computeLiveIns();
|
|
|
|
LV.resetLiveIns();
|
|
|
|
LV.resetKills();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RDFDump)
|
|
|
|
MF.print(dbgs() << "After " << getPassName() << "\n", nullptr);
|
2016-01-18 21:45:51 +01:00
|
|
|
|
2016-01-12 20:09:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createHexagonRDFOpt() {
|
|
|
|
return new HexagonRDFOpt();
|
|
|
|
}
|