Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 1 | //===--- RDFLiveness.h ----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Recalculate the liveness information given a data flow graph. |
| 11 | // This includes block live-ins and kill flags. |
| 12 | |
| 13 | #ifndef RDF_LIVENESS_H |
| 14 | #define RDF_LIVENESS_H |
| 15 | |
| 16 | #include "RDFGraph.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include <map> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace llvm { |
| 23 | class MachineBasicBlock; |
| 24 | class MachineFunction; |
| 25 | class MachineRegisterInfo; |
| 26 | class TargetRegisterInfo; |
| 27 | class MachineDominatorTree; |
| 28 | class MachineDominanceFrontier; |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 29 | |
| 30 | namespace rdf { |
| 31 | struct Liveness { |
| 32 | public: |
Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 33 | // This is really a std::map, except that it provides a non-trivial |
| 34 | // default constructor to the element accessed via []. |
| 35 | struct LiveMapType { |
| 36 | LiveMapType(const TargetRegisterInfo &tri) : Empty(tri) {} |
| 37 | |
| 38 | RegisterAggr &operator[] (MachineBasicBlock *B) { |
| 39 | return Map.emplace(B, Empty).first->second; |
| 40 | } |
| 41 | private: |
| 42 | RegisterAggr Empty; |
| 43 | std::map<MachineBasicBlock*,RegisterAggr> Map; |
| 44 | }; |
| 45 | |
| 46 | typedef std::pair<NodeId,LaneBitmask> NodeRef; |
| 47 | typedef std::set<NodeRef> NodeRefSet; |
| 48 | // RegisterId in RefMap must be normalized. |
| 49 | typedef std::map<RegisterId,NodeRefSet> RefMap; |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 50 | |
| 51 | Liveness(MachineRegisterInfo &mri, const DataFlowGraph &g) |
| 52 | : DFG(g), TRI(g.getTRI()), MDT(g.getDT()), MDF(g.getDF()), |
Krzysztof Parzyszek | 7bb63ac | 2016-10-19 16:30:56 +0000 | [diff] [blame] | 53 | MRI(mri), LiveMap(g.getTRI()), Empty(), NoRegs(g.getTRI()), |
| 54 | Trace(false) {} |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 55 | |
| 56 | NodeList getAllReachingDefs(RegisterRef RefRR, NodeAddr<RefNode*> RefA, |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 57 | bool FullChain, const RegisterAggr &DefRRs); |
| 58 | NodeList getAllReachingDefs(NodeAddr<RefNode*> RefA) { |
Krzysztof Parzyszek | 445bd12 | 2016-10-14 17:57:55 +0000 | [diff] [blame] | 59 | return getAllReachingDefs(RefA.Addr->getRegRef(DFG), RefA, false, NoRegs); |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 60 | } |
| 61 | NodeList getAllReachingDefs(RegisterRef RefRR, NodeAddr<RefNode*> RefA) { |
| 62 | return getAllReachingDefs(RefRR, RefA, false, NoRegs); |
| 63 | } |
Krzysztof Parzyszek | f5cbac9 | 2016-04-29 15:49:13 +0000 | [diff] [blame] | 64 | NodeSet getAllReachingDefsRec(RegisterRef RefRR, NodeAddr<RefNode*> RefA, |
| 65 | NodeSet &Visited, const NodeSet &Defs); |
| 66 | NodeSet getAllReachedUses(RegisterRef RefRR, NodeAddr<DefNode*> DefA, |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 67 | const RegisterAggr &DefRRs); |
| 68 | NodeSet getAllReachedUses(RegisterRef RefRR, NodeAddr<DefNode*> DefA) { |
| 69 | return getAllReachedUses(RefRR, DefA, NoRegs); |
| 70 | } |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 71 | |
| 72 | LiveMapType &getLiveMap() { return LiveMap; } |
| 73 | const LiveMapType &getLiveMap() const { return LiveMap; } |
| 74 | const RefMap &getRealUses(NodeId P) const { |
| 75 | auto F = RealUseMap.find(P); |
| 76 | return F == RealUseMap.end() ? Empty : F->second; |
| 77 | } |
| 78 | |
| 79 | void computePhiInfo(); |
| 80 | void computeLiveIns(); |
| 81 | void resetLiveIns(); |
| 82 | void resetKills(); |
| 83 | void resetKills(MachineBasicBlock *B); |
| 84 | |
| 85 | void trace(bool T) { Trace = T; } |
| 86 | |
| 87 | private: |
| 88 | const DataFlowGraph &DFG; |
| 89 | const TargetRegisterInfo &TRI; |
| 90 | const MachineDominatorTree &MDT; |
| 91 | const MachineDominanceFrontier &MDF; |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 92 | MachineRegisterInfo &MRI; |
| 93 | LiveMapType LiveMap; |
| 94 | const RefMap Empty; |
Krzysztof Parzyszek | a77fe4e | 2016-10-03 17:14:48 +0000 | [diff] [blame] | 95 | const RegisterAggr NoRegs; |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 96 | bool Trace; |
| 97 | |
| 98 | // Cache of mapping from node ids (for RefNodes) to the containing |
| 99 | // basic blocks. Not computing it each time for each node reduces |
| 100 | // the liveness calculation time by a large fraction. |
| 101 | typedef DenseMap<NodeId,MachineBasicBlock*> NodeBlockMap; |
| 102 | NodeBlockMap NBMap; |
| 103 | |
| 104 | // Phi information: |
| 105 | // |
Krzysztof Parzyszek | 6e7fa99 | 2016-10-21 19:12:13 +0000 | [diff] [blame] | 106 | // RealUseMap |
| 107 | // map: NodeId -> (map: RegisterId -> NodeRefSet) |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 108 | // phi id -> (map: register -> set of reached non-phi uses) |
| 109 | std::map<NodeId, RefMap> RealUseMap; |
| 110 | |
| 111 | // Inverse iterated dominance frontier. |
| 112 | std::map<MachineBasicBlock*,std::set<MachineBasicBlock*>> IIDF; |
| 113 | |
| 114 | // Live on entry. |
| 115 | std::map<MachineBasicBlock*,RefMap> PhiLON; |
| 116 | |
| 117 | // Phi uses are considered to be located at the end of the block that |
| 118 | // they are associated with. The reaching def of a phi use dominates the |
| 119 | // block that the use corresponds to, but not the block that contains |
| 120 | // the phi itself. To include these uses in the liveness propagation (up |
| 121 | // the dominator tree), create a map: block -> set of uses live on exit. |
| 122 | std::map<MachineBasicBlock*,RefMap> PhiLOX; |
| 123 | |
Krzysztof Parzyszek | 2db0c8b | 2016-09-07 20:37:05 +0000 | [diff] [blame] | 124 | bool isRestrictedToRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA, |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 125 | RegisterRef RR) const; |
| 126 | RegisterRef getRestrictedRegRef(NodeAddr<RefNode*> RA) const; |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 127 | MachineBasicBlock *getBlockWithRef(NodeId RN) const; |
| 128 | void traverse(MachineBasicBlock *B, RefMap &LiveIn); |
| 129 | void emptify(RefMap &M); |
| 130 | }; |
Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 131 | } // namespace rdf |
| 132 | } // namespace llvm |
Krzysztof Parzyszek | acdff46 | 2016-01-12 15:56:33 +0000 | [diff] [blame] | 133 | |
| 134 | #endif // RDF_LIVENESS_H |