45 cl::init(std::numeric_limits<unsigned>::max()));
58 void getAnalysisUsage(AnalysisUsage &AU)
const override {
65 StringRef getPassName()
const override {
66 return "Hexagon RDF optimizations";
69 bool runOnMachineFunction(MachineFunction &MF)
override;
71 MachineFunctionProperties getRequiredProperties()
const override {
72 return MachineFunctionProperties().setNoVRegs();
78 MachineDominatorTree *MDT;
79 MachineRegisterInfo *MRI;
82struct HexagonCP :
public CopyPropagation {
83 HexagonCP(DataFlowGraph &
G) : CopyPropagation(
G) {}
85 bool interpretAsCopy(
const MachineInstr *
MI, EqualityMap &EM)
override;
88struct HexagonDCE :
public DeadCodeElimination {
89 HexagonDCE(DataFlowGraph &
G, MachineRegisterInfo &
MRI)
90 : DeadCodeElimination(
G,
MRI) {}
92 bool rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove);
93 void removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum);
100char HexagonRDFOpt::ID = 0;
103 "Hexagon RDF optimizations",
false,
false)
111 EM.insert(std::make_pair(DstR, SrcR));
115 unsigned Opc =
MI->getOpcode();
117 case Hexagon::A2_combinew: {
121 assert(
DstOp.getSubReg() == 0 &&
"Unexpected subregister");
128 case Hexagon::A2_addi: {
130 if (!
A.isImm() ||
A.getImm() != 0)
134 case Hexagon::A2_tfr: {
143 return CopyPropagation::interpretAsCopy(
MI, EM);
146bool HexagonDCE::run() {
147 bool Collected = collect();
151 const SetVector<NodeId> &DeadNodes = getDeadNodes();
152 const SetVector<NodeId> &DeadInstrs = getDeadInstrs();
154 using RefToInstrMap = DenseMap<NodeId, NodeId>;
157 SetVector<NodeId> PartlyDead;
158 DataFlowGraph &DFG = getDFG();
162 NodeAddr<StmtNode*> SA =
TA;
163 for (NodeAddr<RefNode*>
RA : SA.
Addr->members(DFG)) {
164 R2I.insert(std::make_pair(
RA.Id, SA.
Id));
173 SetVector<NodeId> Remove = DeadInstrs;
177 auto SA = DFG.
addr<StmtNode*>(
N);
179 dbgs() <<
"Partly dead: " << *SA.
Addr->getCode();
180 Changed |= rewrite(SA, Remove);
186void HexagonDCE::removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum) {
187 MachineInstr *
MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
189 auto getOpNum = [
MI] (MachineOperand &
Op) ->
unsigned {
190 for (
unsigned i = 0, n =
MI->getNumOperands(); i != n; ++i)
191 if (&
MI->getOperand(i) == &
Op)
195 DenseMap<NodeId,unsigned> OpMap;
196 DataFlowGraph &DFG = getDFG();
198 for (NodeAddr<RefNode*>
RA : Refs)
199 OpMap.
insert(std::make_pair(
RA.Id, getOpNum(
RA.Addr->getOp())));
201 MI->removeOperand(OpNum);
203 for (NodeAddr<RefNode*>
RA : Refs) {
204 unsigned N = OpMap[
RA.Id];
206 RA.Addr->setRegRef(&
MI->getOperand(
N), DFG);
208 RA.Addr->setRegRef(&
MI->getOperand(
N-1), DFG);
212bool HexagonDCE::rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove) {
213 if (!getDFG().IsCode<NodeAttrs::Stmt>(IA))
215 DataFlowGraph &DFG = getDFG();
216 MachineInstr &
MI = *NodeAddr<StmtNode*>(IA).Addr->getCode();
217 auto &HII =
static_cast<const HexagonInstrInfo&
>(DFG.
getTII());
220 unsigned Opc =
MI.getOpcode();
221 unsigned OpNum, NewOpc;
223 case Hexagon::L2_loadri_pi:
224 NewOpc = Hexagon::L2_loadri_io;
227 case Hexagon::L2_loadrd_pi:
228 NewOpc = Hexagon::L2_loadrd_io;
231 case Hexagon::V6_vL32b_pi:
232 NewOpc = Hexagon::V6_vL32b_ai;
235 case Hexagon::S2_storeri_pi:
236 NewOpc = Hexagon::S2_storeri_io;
239 case Hexagon::S2_storerd_pi:
240 NewOpc = Hexagon::S2_storerd_io;
243 case Hexagon::V6_vS32b_pi:
244 NewOpc = Hexagon::V6_vS32b_ai;
250 auto IsDead = [
this] (NodeAddr<DefNode*>
DA) ->
bool {
251 return getDeadNodes().count(
DA.Id);
254 MachineOperand &
Op =
MI.getOperand(OpNum);
255 for (NodeAddr<DefNode*> DA :
IA.Addr->members_if(DFG.
IsDef, DFG)) {
256 if (&
DA.Addr->getOp() != &
Op)
269 dbgs() <<
"Rewriting: " <<
MI;
270 MI.setDesc(HII.get(NewOpc));
271 MI.getOperand(OpNum+2).setImm(0);
272 removeOperand(IA, OpNum);
279bool HexagonRDFOpt::runOnMachineFunction(MachineFunction &MF) {
287 dbgs() <<
"Skipping " << getPassName() <<
": too many basic blocks\n";
297 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
298 const auto &MDF = getAnalysis<MachineDominanceFrontier>();
299 const auto &HII = *MF.
getSubtarget<HexagonSubtarget>().getInstrInfo();
300 const auto &HRI = *MF.
getSubtarget<HexagonSubtarget>().getRegisterInfo();
305 MF.
print(
dbgs() <<
"Before " << getPassName() <<
"\n",
nullptr);
307 DataFlowGraph
G(MF, HII, HRI, *MDT, MDF);
311 DataFlowGraph::Config Cfg;
318 dbgs() <<
"Starting copy propagation on: " << MF.
getName() <<
'\n'
319 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
325 dbgs() <<
"Starting dead code elimination on: " << MF.
getName() <<
'\n'
326 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
327 HexagonDCE DCE(
G, *
MRI);
333 dbgs() <<
"Starting liveness recomputation on: " << MF.
getName() <<
'\n'
334 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
336 Liveness LV(*
MRI,
G);
344 MF.
print(
dbgs() <<
"After " << getPassName() <<
"\n",
nullptr);
350 return new HexagonRDFOpt();
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file defines the DenseMap class.
cl::opt< unsigned > RDFFuncBlockLimit
static cl::opt< bool > RDFTrackReserved("hexagon-rdf-track-reserved", cl::Hidden)
static cl::opt< unsigned > RDFLimit("hexagon-rdf-limit", cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< bool > RDFDump("hexagon-rdf-dump", cl::Hidden)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
SI optimize exec mask operations pre RA
This file implements a set that has insertion order iteration characteristics.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
FunctionPass class - This class is used to implement most global optimizations.
Analysis pass which computes a MachineDominatorTree.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
SmallVector< Node, 4 > NodeList
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
DWARFExpression::Operation Op
FunctionPass * createHexagonRDFOpt()
NodeList members(const DataFlowGraph &G) const
RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const
static bool IsDef(const Node BA)
NodeList getRelatedRefs(Instr IA, Ref RA) const
const TargetInstrInfo & getTII() const
static bool IsCode(const Node BA)
NodeAddr< T > addr(NodeId N) const