Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- subzero/src/IceLoopAnalyzer.h - Loop Analysis ------------*- C++ -*-===// | |
| 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 /// \file | |
| 11 /// \brief This analysis identifies loops in the CFG. | |
| 12 //===----------------------------------------------------------------------===// | |
| 13 | |
| 14 #ifndef SUBZERO_SRC_ICELOOPANALYZER_H | |
| 15 #define SUBZERO_SRC_ICELOOPANALYZER_H | |
| 16 | |
| 17 #include "IceDefs.h" | |
| 18 | |
| 19 namespace Ice { | |
| 20 | |
| 21 /// Analyze the functions CFG for loops. The CFG must not change during the | |
|
Jim Stichnoth
2015/09/03 21:35:09
Drop the word "functions", or at least change to "
ascull
2015/09/03 22:47:13
Done.
| |
| 22 /// lifetine of this object. | |
|
Jim Stichnoth
2015/09/03 21:35:09
lifetime
ascull
2015/09/03 22:47:13
Done.
| |
| 23 class LoopAnalyzer { | |
| 24 LoopAnalyzer() = delete; | |
| 25 LoopAnalyzer(const LoopAnalyzer &) = delete; | |
| 26 LoopAnalyzer &operator=(const LoopAnalyzer &) = delete; | |
| 27 | |
| 28 public: | |
| 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 void computeLoopNestDepth(); | |
| 39 | |
| 40 private: | |
| 41 using IndexT = uint32_t; | |
| 42 static constexpr IndexT UndefinedIndex = 0; | |
| 43 static constexpr IndexT FirstDefinedIndex = 1; | |
| 44 | |
| 45 // TODO(ascull): classify the other fields | |
| 46 class LoopNode { | |
| 47 LoopNode() = delete; | |
| 48 LoopNode operator=(const LoopNode &) = delete; | |
| 49 | |
| 50 public: | |
| 51 explicit LoopNode(CfgNode *BB) : BB(BB), Deleted(false) {} | |
|
Jim Stichnoth
2015/09/03 21:35:09
Use "bool Deleted = false;" below instead of "Dele
ascull
2015/09/03 22:47:13
Done.
| |
| 52 LoopNode(const LoopNode &) = default; | |
| 53 | |
| 54 void reset(); | |
| 55 | |
| 56 NodeList::const_iterator successorsEnd() const; | |
| 57 NodeList::const_iterator currentSuccessor() const { return Succ; } | |
| 58 void nextSuccessor() { ++Succ; } | |
| 59 | |
| 60 void visit(IndexT VisitIndex) { Index = LowLink = VisitIndex; } | |
| 61 bool isVisited() const { return Index != UndefinedIndex; } | |
| 62 IndexT getIndex() const { return Index; } | |
| 63 | |
| 64 void tryLink(IndexT NewLink) { | |
| 65 if (NewLink < LowLink) | |
| 66 LowLink = NewLink; | |
| 67 } | |
| 68 IndexT getLowLink() const { return LowLink; } | |
| 69 | |
| 70 void setOnStack(bool NewValue = true) { OnStack = NewValue; } | |
| 71 bool isOnStack() const { return OnStack; } | |
| 72 | |
| 73 void setDeleted() { Deleted = true; } | |
| 74 bool isDeleted() const { return Deleted; } | |
| 75 | |
| 76 void incrementLoopNestDepth(); | |
| 77 | |
| 78 private: | |
| 79 CfgNode *BB; | |
| 80 NodeList::const_iterator Succ; | |
|
Jim Stichnoth
2015/09/03 21:35:09
Make sure Succ/Index/LowLink/OnStack get initializ
ascull
2015/09/03 22:47:13
Done.
Jim Stichnoth
2015/09/03 23:23:30
I don't see this in the latest patchset. To be cl
ascull
2015/09/04 00:23:51
I call reset() in the constructor which initialize
Jim Stichnoth
2015/09/04 00:47:26
Ah, sorry, I missed the reset() call.
| |
| 81 IndexT Index; | |
| 82 IndexT LowLink; | |
| 83 bool OnStack; | |
| 84 bool Deleted; | |
| 85 }; | |
| 86 | |
| 87 using LoopNodeList = std::vector<LoopNode, CfgLocalAllocator<LoopNode>>; | |
| 88 using LoopNodePtrList = | |
| 89 std::vector<LoopNode *, CfgLocalAllocator<LoopNode *>>; | |
| 90 | |
| 91 /// Process the node as part as part of Tarjan's algorithm and return either | |
| 92 /// a node to recurse into or nullptr when the node has been fully processed. | |
| 93 LoopNode *processNode(LoopNode &Node); | |
| 94 | |
| 95 /// The fuction to analyze for loops. | |
| 96 Cfg *Func; | |
|
Jim Stichnoth
2015/09/03 21:35:09
Maybe Cfg *const Func;
ascull
2015/09/03 22:47:13
Done.
| |
| 97 /// A list of decorated nodes in the same order as Func->getNodes() which | |
| 98 /// means the node's index will also be valid in this list. | |
| 99 LoopNodeList AllNodes; | |
| 100 /// This is used as a replacement for the call stack. | |
| 101 LoopNodePtrList WorkStack; | |
| 102 /// Track which loop a node belongs to. | |
| 103 LoopNodePtrList LoopStack; | |
| 104 /// The index to assign to the next visited node. | |
| 105 IndexT NextIndex; | |
|
Jim Stichnoth
2015/09/03 21:35:09
Initialize NextIndex in the ctor
ascull
2015/09/03 22:47:13
Done.
| |
| 106 /// The number of nodes which have been marked deleted. This is used to track | |
| 107 /// when the iteration should end. | |
| 108 LoopNodePtrList::size_type NumDeletedNodes; | |
| 109 }; | |
| 110 | |
| 111 } // end of namespace Ice | |
| 112 | |
| 113 #endif // SUBZERO_SRC_ICELOOPANALYZER_H | |
| OLD | NEW |