1293838Sdim//===--- RDFDeadCode.h ----------------------------------------------------===//
2293838Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6293838Sdim//
7293838Sdim//===----------------------------------------------------------------------===//
8293838Sdim//
9293838Sdim// RDF-based generic dead code elimination.
10293838Sdim//
11293838Sdim// The main interface of this class are functions "collect" and "erase".
12293838Sdim// This allows custom processing of the function being optimized by a
13293838Sdim// particular consumer. The simplest way to use this class would be to
14293838Sdim// instantiate an object, and then simply call "collect" and "erase",
15293838Sdim// passing the result of "getDeadInstrs()" to it.
16293838Sdim// A more complex scenario would be to call "collect" first, then visit
17293838Sdim// all post-increment instructions to see if the address update is dead
18293838Sdim// or not, and if it is, convert the instruction to a non-updating form.
19293838Sdim// After that "erase" can be called with the set of nodes including both,
20293838Sdim// dead defs from the updating instructions and the nodes corresponding
21293838Sdim// to the dead instructions.
22293838Sdim
23293838Sdim#ifndef RDF_DEADCODE_H
24293838Sdim#define RDF_DEADCODE_H
25293838Sdim
26363496Sdim#include "llvm/CodeGen/RDFGraph.h"
27363496Sdim#include "llvm/CodeGen/RDFLiveness.h"
28293838Sdim#include "llvm/ADT/SetVector.h"
29293838Sdim
30293838Sdimnamespace llvm {
31293838Sdim  class MachineRegisterInfo;
32293838Sdim
33293838Sdimnamespace rdf {
34293838Sdim  struct DeadCodeElimination {
35293838Sdim    DeadCodeElimination(DataFlowGraph &dfg, MachineRegisterInfo &mri)
36293838Sdim      : Trace(false), DFG(dfg), MRI(mri), LV(mri, dfg) {}
37293838Sdim
38293838Sdim    bool collect();
39293838Sdim    bool erase(const SetVector<NodeId> &Nodes);
40293838Sdim    void trace(bool On) { Trace = On; }
41293838Sdim    bool trace() const { return Trace; }
42293838Sdim
43293838Sdim    SetVector<NodeId> getDeadNodes() { return DeadNodes; }
44293838Sdim    SetVector<NodeId> getDeadInstrs() { return DeadInstrs; }
45293838Sdim    DataFlowGraph &getDFG() { return DFG; }
46293838Sdim
47293838Sdim  private:
48293838Sdim    bool Trace;
49293838Sdim    SetVector<NodeId> LiveNodes;
50293838Sdim    SetVector<NodeId> DeadNodes;
51293838Sdim    SetVector<NodeId> DeadInstrs;
52293838Sdim    DataFlowGraph &DFG;
53293838Sdim    MachineRegisterInfo &MRI;
54293838Sdim    Liveness LV;
55293838Sdim
56309124Sdim    template<typename T> struct SetQueue;
57309124Sdim
58293838Sdim    bool isLiveInstr(const MachineInstr *MI) const;
59309124Sdim    void scanInstr(NodeAddr<InstrNode*> IA, SetQueue<NodeId> &WorkQ);
60309124Sdim    void processDef(NodeAddr<DefNode*> DA, SetQueue<NodeId> &WorkQ);
61309124Sdim    void processUse(NodeAddr<UseNode*> UA, SetQueue<NodeId> &WorkQ);
62293838Sdim  };
63309124Sdim} // namespace rdf
64309124Sdim} // namespace llvm
65293838Sdim
66293838Sdim#endif
67