Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceLoopAnalyzer.cpp - Loop Analysis --------------------===// | 1 //===- subzero/src/IceLoopAnalyzer.cpp - Loop Analysis --------------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 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 Implements the loop analysis on the CFG. | 11 /// \brief Implements the loop analysis on the CFG. |
| 12 /// | 12 /// |
| 13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 14 #include "IceLoopAnalyzer.h" | 14 #include "IceLoopAnalyzer.h" |
| 15 | 15 |
| 16 #include "IceCfg.h" | 16 #include "IceCfg.h" |
| 17 #include "IceCfgNode.h" | 17 #include "IceCfgNode.h" |
| 18 | 18 |
| 19 #include <algorithm> | |
| 20 | |
| 19 namespace Ice { | 21 namespace Ice { |
| 20 | 22 |
| 21 void LoopAnalyzer::LoopNode::reset() { | 23 void LoopAnalyzer::LoopNode::reset() { |
| 22 if (Deleted) | 24 if (Deleted) |
| 23 return; | 25 return; |
| 24 Succ = BB->getOutEdges().begin(); | 26 Succ = BB->getOutEdges().begin(); |
| 25 Index = LowLink = UndefinedIndex; | 27 Index = LowLink = UndefinedIndex; |
| 26 OnStack = false; | 28 OnStack = false; |
| 27 } | 29 } |
| 28 | 30 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 // Reaching here means a loop has been found! It consists of the nodes on the | 137 // Reaching here means a loop has been found! It consists of the nodes on the |
| 136 // top of the stack, down until the current node being processed, Node, is | 138 // top of the stack, down until the current node being processed, Node, is |
| 137 // found. | 139 // found. |
| 138 for (auto It = LoopStack.rbegin(); It != LoopStack.rend(); ++It) { | 140 for (auto It = LoopStack.rbegin(); It != LoopStack.rend(); ++It) { |
| 139 (*It)->setOnStack(false); | 141 (*It)->setOnStack(false); |
| 140 (*It)->incrementLoopNestDepth(); | 142 (*It)->incrementLoopNestDepth(); |
| 141 // Remove the loop from the stack and delete the head node | 143 // Remove the loop from the stack and delete the head node |
| 142 if (*It == &Node) { | 144 if (*It == &Node) { |
| 143 (*It)->setDeleted(); | 145 (*It)->setDeleted(); |
| 144 ++NumDeletedNodes; | 146 ++NumDeletedNodes; |
| 145 CfgVector<SizeT> LoopNodes; | 147 CfgUnorderedSet<SizeT> LoopNodes; |
| 146 for (auto LoopIter = It.base() - 1; LoopIter != LoopStack.end(); | 148 for (auto LoopIter = It.base() - 1; LoopIter != LoopStack.end(); |
| 147 ++LoopIter) { | 149 ++LoopIter) { |
| 148 LoopNodes.push_back((*LoopIter)->getNode()->getIndex()); | 150 LoopNodes.insert((*LoopIter)->getNode()->getIndex()); |
| 149 } | 151 } |
| 150 Loops[(*It)->getNode()->getIndex()] = LoopNodes; | 152 Loops.push_back(LoopNodes); |
|
John
2016/07/15 21:13:30
this is awfully inefficient! you're copying LoopNo
| |
| 151 LoopStack.erase(It.base() - 1, LoopStack.end()); | 153 LoopStack.erase(It.base() - 1, LoopStack.end()); |
| 152 break; | 154 break; |
| 153 } | 155 } |
| 154 } | 156 } |
| 155 | 157 |
| 156 return nullptr; | 158 return nullptr; |
| 157 } | 159 } |
| 160 void LoopAnalyzer::LoopInfo::computeLoopMetaData() { | |
|
John
2016/07/15 21:13:30
Why do you need this method? where was it defined
John
2016/07/18 15:25:36
nevermind.
manasijm
2016/07/18 22:27:47
It was not there.
I am computing headers and pre-h
| |
| 161 std::sort(Loops.begin(), Loops.end(), [](const CfgUnorderedSet<SizeT> &A, | |
| 162 const CfgUnorderedSet<SizeT> &B) { | |
| 163 return A.size() > B.size(); | |
| 164 }); | |
| 165 for (SizeT i = 0; i < Loops.size(); ++i) { | |
| 166 auto &Loop = Loops[i]; | |
| 167 CfgNode *Header = nullptr; | |
| 168 bool IsSimpleLoop = true; | |
| 169 for (auto NodeIndex : Loop) { | |
| 170 CfgNode *Cur = Func->getNodes()[NodeIndex]; | |
| 171 for (auto *Prev : Cur->getInEdges()) { | |
| 172 if (Loop.find(Prev->getIndex()) == Loop.end()) { // coming from outside | |
| 173 if (Header == nullptr) { | |
| 174 Header = Cur; | |
| 175 } else { | |
| 176 Header = nullptr; | |
| 177 IsSimpleLoop = false; | |
| 178 break; | |
| 179 } | |
| 180 } | |
| 181 } | |
| 182 if (!IsSimpleLoop) { | |
| 183 break; | |
| 184 } | |
| 185 } | |
| 186 if (!IsSimpleLoop) | |
| 187 continue; // To next potential loop | |
| 188 | |
| 189 LoopIndexForHeader[Header->getIndex()] = i; | |
| 190 | |
| 191 for (auto NodeIndex : Loop) { | |
| 192 // CfgNode *Cur = Func->getNodes()[NodeIndex]; | |
|
Jim Stichnoth
2016/07/15 22:14:33
Remove this?
manasijm
2016/07/18 22:27:47
Done.
| |
| 193 HeaderForNode[NodeIndex] = Header->getIndex(); | |
| 194 } | |
| 195 | |
| 196 CfgNode *PreHeader = nullptr; | |
| 197 for (auto *Prev : Header->getInEdges()) { | |
| 198 if (Loop.find(Prev->getIndex()) == Loop.end()) { | |
| 199 if (PreHeader == nullptr) { | |
| 200 PreHeader = Prev; | |
| 201 } else { | |
| 202 PreHeader = nullptr; | |
| 203 break; | |
| 204 } | |
| 205 } | |
| 206 } | |
| 207 if (PreHeader != nullptr) { | |
| 208 PreheaderForHeader[Header->getIndex()] = PreHeader->getIndex(); | |
| 209 } | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 CfgNode *LoopAnalyzer::LoopInfo::getHeader(CfgNode *Node) { | |
| 214 return Func->getNodes()[HeaderForNode[Node->getIndex()]]; | |
| 215 } | |
| 216 | |
| 217 CfgNode *LoopAnalyzer::LoopInfo::getPreHeader(CfgNode *Node) { | |
| 218 return Func->getNodes()[PreheaderForHeader[Node->getIndex()]]; | |
| 219 } | |
| 220 const CfgUnorderedSet<SizeT> &LoopAnalyzer::LoopInfo::getBody(CfgNode *Header) { | |
| 221 auto Iter = LoopIndexForHeader.find(Header->getIndex()); | |
| 222 assert(Iter != LoopIndexForHeader.end()); | |
| 223 return Loops[Iter->second]; | |
| 224 } | |
| 158 | 225 |
| 159 } // end of namespace Ice | 226 } // end of namespace Ice |
| OLD | NEW |