Chromium Code Reviews| 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 /// Analyze a function's CFG for loops. The CFG must not change during the |
| 22 /// lifetime of this object. | 22 /// lifetime of this object. |
| 23 class LoopAnalyzer { | 23 class LoopAnalyzer { |
|
John
2016/07/15 21:13:30
OK, you opened pandora's box... :)
This class is
| |
| 24 LoopAnalyzer() = delete; | |
| 25 LoopAnalyzer(const LoopAnalyzer &) = delete; | |
| 26 LoopAnalyzer &operator=(const LoopAnalyzer &) = delete; | |
| 27 | |
| 28 public: | 24 public: |
| 25 LoopAnalyzer() = default; | |
| 29 explicit LoopAnalyzer(Cfg *Func); | 26 explicit LoopAnalyzer(Cfg *Func); |
| 30 | 27 |
| 31 /// Use Tarjan's strongly connected components algorithm to identify outermost | 28 /// 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 | 29 /// 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 | 30 /// 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. | 31 /// loops but instead all paths to the head come from 'continue' constructs. |
| 35 /// | 32 /// |
| 36 /// This only computes the loop nest depth within the function and does not | 33 /// 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. | 34 /// 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 | 35 // TODO(ascull): this currently uses a extension of Tarjan's algorithm with |
| 39 // is bounded linear. ncbray suggests another algorithm which is linear in | 36 // is bounded linear. ncbray suggests another algorithm which is linear in |
| 40 // practice but not bounded linear. I think it also finds dominators. | 37 // practice but not bounded linear. I think it also finds dominators. |
| 41 // http://lenx.100871.net/papers/loop-SAS.pdf | 38 // http://lenx.100871.net/papers/loop-SAS.pdf |
| 42 CfgUnorderedMap<SizeT, CfgVector<SizeT>> getLoopInfo() { return Loops; } | 39 // CfgUnorderedMap<SizeT, CfgVector<SizeT>> getLoopInfo() { return Loops; } |
| 40 class LoopInfo { | |
| 41 public: | |
| 42 LoopInfo() {} | |
| 43 LoopInfo(Cfg *Func, const CfgVector<CfgUnorderedSet<SizeT>> Loops) | |
|
John
2016/07/18 15:25:36
A nice way to decrease memory usage: pass Loops by
manasijm
2016/07/18 22:27:47
Should be 'moved' by default in new implementation
| |
| 44 : Func(Func), Loops(Loops) { | |
| 45 computeLoopMetaData(); | |
| 46 }; | |
| 47 | |
| 48 template <typename F> void forEachLoop(F Callback) { | |
|
John
2016/07/15 21:13:30
no need for this: just create a getter for LoopInd
manasijm
2016/07/18 22:27:47
Done.
| |
| 49 for (auto Pair : LoopIndexForHeader) { | |
| 50 Callback(Pair.first, Loops[Pair.second]); | |
| 51 } | |
| 52 } | |
| 53 CfgNode *getHeader(CfgNode *Node); | |
|
John
2016/07/18 15:25:36
getHeader, and getBody aren't used anywhere. remov
manasijm
2016/07/18 22:27:47
Done.
| |
| 54 CfgNode *getPreHeader(CfgNode *Node); | |
| 55 const CfgUnorderedSet<SizeT> &getBody(CfgNode *Header); | |
| 56 | |
| 57 private: | |
| 58 void computeLoopMetaData(); | |
| 59 Cfg *Func = nullptr; | |
| 60 CfgVector<CfgUnorderedSet<SizeT>> Loops; | |
|
John
2016/07/18 15:25:36
How about changing this vector to be a vector of s
| |
| 61 CfgUnorderedMap<SizeT, SizeT> HeaderForNode; | |
| 62 CfgUnorderedMap<SizeT, SizeT> PreheaderForHeader; | |
| 63 /// Stores indices to the Loops array. | |
| 64 CfgUnorderedMap<SizeT, SizeT> LoopIndexForHeader; | |
| 65 }; | |
| 66 LoopInfo getLoopInfo() { return LoopInfo(Func, Loops); } | |
| 43 | 67 |
| 44 private: | 68 private: |
| 45 void computeLoopNestDepth(); | 69 void computeLoopNestDepth(); |
| 70 | |
| 46 using IndexT = uint32_t; | 71 using IndexT = uint32_t; |
| 47 static constexpr IndexT UndefinedIndex = 0; | 72 static constexpr IndexT UndefinedIndex = 0; |
| 48 static constexpr IndexT FirstDefinedIndex = 1; | 73 static constexpr IndexT FirstDefinedIndex = 1; |
| 49 | 74 |
| 50 // TODO(ascull): classify the other fields | 75 // TODO(ascull): classify the other fields |
| 51 class LoopNode { | 76 class LoopNode { |
| 52 LoopNode() = delete; | 77 LoopNode() = delete; |
| 53 LoopNode operator=(const LoopNode &) = delete; | 78 LoopNode operator=(const LoopNode &) = delete; |
| 54 | 79 |
| 55 public: | 80 public: |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 LoopNodeList AllNodes; | 131 LoopNodeList AllNodes; |
| 107 /// This is used as a replacement for the call stack. | 132 /// This is used as a replacement for the call stack. |
| 108 LoopNodePtrList WorkStack; | 133 LoopNodePtrList WorkStack; |
| 109 /// Track which loop a node belongs to. | 134 /// Track which loop a node belongs to. |
| 110 LoopNodePtrList LoopStack; | 135 LoopNodePtrList LoopStack; |
| 111 /// The index to assign to the next visited node. | 136 /// The index to assign to the next visited node. |
| 112 IndexT NextIndex = FirstDefinedIndex; | 137 IndexT NextIndex = FirstDefinedIndex; |
| 113 /// The number of nodes which have been marked deleted. This is used to track | 138 /// The number of nodes which have been marked deleted. This is used to track |
| 114 /// when the iteration should end. | 139 /// when the iteration should end. |
| 115 LoopNodePtrList::size_type NumDeletedNodes = 0; | 140 LoopNodePtrList::size_type NumDeletedNodes = 0; |
| 116 /// Detailed loop information | 141 |
| 117 CfgUnorderedMap<SizeT, CfgVector<SizeT>> Loops; | 142 /// All the Loops, in descending order of size |
| 143 CfgVector<CfgUnorderedSet<SizeT>> Loops; | |
| 118 }; | 144 }; |
| 119 | 145 |
| 120 } // end of namespace Ice | 146 } // end of namespace Ice |
| 121 | 147 |
| 122 #endif // SUBZERO_SRC_ICELOOPANALYZER_H | 148 #endif // SUBZERO_SRC_ICELOOPANALYZER_H |
| OLD | NEW |