OLD | NEW |
---|---|
1 //===- subzero/src/IceLoopAnalyzer.h - Loop Analysis ------------*- C++ -*-===// | 1 //===- subzero/src/IceLoopAnalyzer.h - Loop Analysis ------------*- C++ -*-===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 /// | 9 /// |
10 /// \file | 10 /// \file |
11 /// \brief This analysis identifies loops in the CFG. | 11 /// \brief This analysis identifies loops in the CFG. |
12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
13 | 13 |
14 #ifndef SUBZERO_SRC_ICELOOPANALYZER_H | 14 #ifndef SUBZERO_SRC_ICELOOPANALYZER_H |
15 #define SUBZERO_SRC_ICELOOPANALYZER_H | 15 #define SUBZERO_SRC_ICELOOPANALYZER_H |
16 | 16 |
17 #include "IceDefs.h" | 17 #include "IceDefs.h" |
18 | 18 |
19 namespace Ice { | 19 namespace Ice { |
20 | 20 |
21 /// Analyze a function's CFG for loops. The CFG must not change during the | 21 struct Loop { |
22 /// lifetime of this object. | 22 Loop(CfgNode *Header, CfgNode *PreHeader, CfgUnorderedSet<SizeT> Body) |
23 class LoopAnalyzer { | 23 : Header(Header), PreHeader(PreHeader), Body(Body) {} |
24 LoopAnalyzer() = delete; | 24 CfgNode *Header; |
25 LoopAnalyzer(const LoopAnalyzer &) = delete; | 25 CfgNode *PreHeader; |
26 LoopAnalyzer &operator=(const LoopAnalyzer &) = delete; | 26 CfgUnorderedSet<SizeT> Body; // Node IDs |
John
2016/07/19 16:13:12
add const to the members here. I never know if you
manasijm
2016/07/19 20:17:01
It seems to be
CfgNode *const
But:
LoopInfo = Com
| |
27 }; | |
27 | 28 |
28 public: | 29 CfgVector<Loop> ComputeLoopInfo(Cfg *Func); |
29 explicit LoopAnalyzer(Cfg *Func); | |
30 | |
31 /// Use Tarjan's strongly connected components algorithm to identify outermost | |
32 /// to innermost loops. By deleting the head of the loop from the graph, inner | |
33 /// loops can be found. This assumes that the head node is not shared between | |
34 /// loops but instead all paths to the head come from 'continue' constructs. | |
35 /// | |
36 /// This only computes the loop nest depth within the function and does not | |
37 /// take into account whether the function was called from within a loop. | |
38 // TODO(ascull): this currently uses a extension of Tarjan's algorithm with | |
39 // is bounded linear. ncbray suggests another algorithm which is linear in | |
40 // practice but not bounded linear. I think it also finds dominators. | |
41 // http://lenx.100871.net/papers/loop-SAS.pdf | |
42 CfgUnorderedMap<SizeT, CfgVector<SizeT>> getLoopInfo() { return Loops; } | |
43 | |
44 private: | |
45 void computeLoopNestDepth(); | |
46 using IndexT = uint32_t; | |
47 static constexpr IndexT UndefinedIndex = 0; | |
48 static constexpr IndexT FirstDefinedIndex = 1; | |
49 | |
50 // TODO(ascull): classify the other fields | |
51 class LoopNode { | |
52 LoopNode() = delete; | |
53 LoopNode operator=(const LoopNode &) = delete; | |
54 | |
55 public: | |
56 explicit LoopNode(CfgNode *BB) : BB(BB) { reset(); } | |
57 LoopNode(const LoopNode &) = default; | |
58 | |
59 void reset(); | |
60 | |
61 NodeList::const_iterator successorsEnd() const; | |
62 NodeList::const_iterator currentSuccessor() const { return Succ; } | |
63 void nextSuccessor() { ++Succ; } | |
64 | |
65 void visit(IndexT VisitIndex) { Index = LowLink = VisitIndex; } | |
66 bool isVisited() const { return Index != UndefinedIndex; } | |
67 IndexT getIndex() const { return Index; } | |
68 | |
69 void tryLink(IndexT NewLink) { | |
70 if (NewLink < LowLink) | |
71 LowLink = NewLink; | |
72 } | |
73 IndexT getLowLink() const { return LowLink; } | |
74 | |
75 void setOnStack(bool NewValue = true) { OnStack = NewValue; } | |
76 bool isOnStack() const { return OnStack; } | |
77 | |
78 void setDeleted() { Deleted = true; } | |
79 bool isDeleted() const { return Deleted; } | |
80 | |
81 void incrementLoopNestDepth(); | |
82 bool hasSelfEdge() const; | |
83 | |
84 CfgNode *getNode() { return BB; } | |
85 | |
86 private: | |
87 CfgNode *BB; | |
88 NodeList::const_iterator Succ; | |
89 IndexT Index; | |
90 IndexT LowLink; | |
91 bool OnStack; | |
92 bool Deleted = false; | |
93 }; | |
94 | |
95 using LoopNodeList = CfgVector<LoopNode>; | |
96 using LoopNodePtrList = CfgVector<LoopNode *>; | |
97 | |
98 /// Process the node as part as part of Tarjan's algorithm and return either a | |
99 /// node to recurse into or nullptr when the node has been fully processed. | |
100 LoopNode *processNode(LoopNode &Node); | |
101 | |
102 /// The function to analyze for loops. | |
103 Cfg *const Func; | |
104 /// A list of decorated nodes in the same order as Func->getNodes() which | |
105 /// means the node's index will also be valid in this list. | |
106 LoopNodeList AllNodes; | |
107 /// This is used as a replacement for the call stack. | |
108 LoopNodePtrList WorkStack; | |
109 /// Track which loop a node belongs to. | |
110 LoopNodePtrList LoopStack; | |
111 /// The index to assign to the next visited node. | |
112 IndexT NextIndex = FirstDefinedIndex; | |
113 /// The number of nodes which have been marked deleted. This is used to track | |
114 /// when the iteration should end. | |
115 LoopNodePtrList::size_type NumDeletedNodes = 0; | |
116 /// Detailed loop information | |
117 CfgUnorderedMap<SizeT, CfgVector<SizeT>> Loops; | |
118 }; | |
119 | 30 |
120 } // end of namespace Ice | 31 } // end of namespace Ice |
121 | 32 |
122 #endif // SUBZERO_SRC_ICELOOPANALYZER_H | 33 #endif // SUBZERO_SRC_ICELOOPANALYZER_H |
OLD | NEW |