loop_descriptor.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_LOOP_DESCRIPTOR_H_
  15. #define SOURCE_OPT_LOOP_DESCRIPTOR_H_
  16. #include <algorithm>
  17. #include <cstdint>
  18. #include <map>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <vector>
  24. #include "source/opt/basic_block.h"
  25. #include "source/opt/dominator_analysis.h"
  26. #include "source/opt/module.h"
  27. #include "source/opt/tree_iterator.h"
  28. #include "source/util/status.h"
  29. namespace spvtools {
  30. namespace opt {
  31. class IRContext;
  32. class CFG;
  33. class LoopDescriptor;
  34. // A class to represent and manipulate a loop in structured control flow.
  35. class Loop {
  36. // The type used to represent nested child loops.
  37. using ChildrenList = std::vector<Loop*>;
  38. public:
  39. using iterator = ChildrenList::iterator;
  40. using const_iterator = ChildrenList::const_iterator;
  41. using BasicBlockListTy = std::unordered_set<uint32_t>;
  42. explicit Loop(IRContext* context)
  43. : context_(context),
  44. loop_header_(nullptr),
  45. loop_continue_(nullptr),
  46. loop_merge_(nullptr),
  47. loop_preheader_(nullptr),
  48. loop_latch_(nullptr),
  49. parent_(nullptr),
  50. loop_is_marked_for_removal_(false) {}
  51. Loop(IRContext* context, DominatorAnalysis* analysis, BasicBlock* header,
  52. BasicBlock* continue_target, BasicBlock* merge_target);
  53. // Iterators over the immediate sub-loops.
  54. inline iterator begin() { return nested_loops_.begin(); }
  55. inline iterator end() { return nested_loops_.end(); }
  56. inline const_iterator begin() const { return cbegin(); }
  57. inline const_iterator end() const { return cend(); }
  58. inline const_iterator cbegin() const { return nested_loops_.begin(); }
  59. inline const_iterator cend() const { return nested_loops_.end(); }
  60. // Returns the header (first basic block of the loop). This block contains the
  61. // OpLoopMerge instruction.
  62. inline BasicBlock* GetHeaderBlock() { return loop_header_; }
  63. inline const BasicBlock* GetHeaderBlock() const { return loop_header_; }
  64. inline void SetHeaderBlock(BasicBlock* header) { loop_header_ = header; }
  65. // Updates the OpLoopMerge instruction to reflect the current state of the
  66. // loop.
  67. inline void UpdateLoopMergeInst() {
  68. assert(GetHeaderBlock()->GetLoopMergeInst() &&
  69. "The loop is not structured");
  70. Instruction* merge_inst = GetHeaderBlock()->GetLoopMergeInst();
  71. merge_inst->SetInOperand(0, {GetMergeBlock()->id()});
  72. }
  73. // Returns the continue target basic block. This is the block designated as
  74. // the continue target by the OpLoopMerge instruction.
  75. inline BasicBlock* GetContinueBlock() { return loop_continue_; }
  76. inline const BasicBlock* GetContinueBlock() const { return loop_continue_; }
  77. // Returns the latch basic block (basic block that holds the back-edge).
  78. // These functions return nullptr if the loop is not structured (i.e. if it
  79. // has more than one backedge).
  80. inline BasicBlock* GetLatchBlock() { return loop_latch_; }
  81. inline const BasicBlock* GetLatchBlock() const { return loop_latch_; }
  82. // Sets |latch| as the loop unique block branching back to the header.
  83. // A latch block must have the following properties:
  84. // - |latch| must be in the loop;
  85. // - must be the only block branching back to the header block.
  86. void SetLatchBlock(BasicBlock* latch);
  87. // Sets |continue_block| as the continue block of the loop. This should be the
  88. // continue target of the OpLoopMerge and should dominate the latch block.
  89. void SetContinueBlock(BasicBlock* continue_block);
  90. // Returns the basic block which marks the end of the loop.
  91. // These functions return nullptr if the loop is not structured.
  92. inline BasicBlock* GetMergeBlock() { return loop_merge_; }
  93. inline const BasicBlock* GetMergeBlock() const { return loop_merge_; }
  94. // Sets |merge| as the loop merge block. A merge block must have the following
  95. // properties:
  96. // - |merge| must not be in the loop;
  97. // - all its predecessors must be in the loop.
  98. // - it must not be already used as merge block.
  99. // If the loop has an OpLoopMerge in its header, this instruction is also
  100. // updated.
  101. void SetMergeBlock(BasicBlock* merge);
  102. // Returns the loop pre-header, nullptr means that the loop predecessor does
  103. // not qualify as a preheader.
  104. // The preheader is the unique predecessor that:
  105. // - Dominates the loop header;
  106. // - Has only the loop header as successor.
  107. inline BasicBlock* GetPreHeaderBlock() { return loop_preheader_; }
  108. // Returns the loop pre-header.
  109. inline const BasicBlock* GetPreHeaderBlock() const { return loop_preheader_; }
  110. // Sets |preheader| as the loop preheader block. A preheader block must have
  111. // the following properties:
  112. // - |merge| must not be in the loop;
  113. // - have an unconditional branch to the loop header.
  114. void SetPreHeaderBlock(BasicBlock* preheader);
  115. // Returns the loop pre-header, if there is no suitable preheader it will be
  116. // created. Returns |nullptr| if it fails to create the preheader.
  117. BasicBlock* GetOrCreatePreHeaderBlock();
  118. // Returns true if this loop contains any nested loops.
  119. inline bool HasNestedLoops() const { return nested_loops_.size() != 0; }
  120. // Clears and fills |exit_blocks| with all basic blocks that are not in the
  121. // loop and has at least one predecessor in the loop.
  122. void GetExitBlocks(std::unordered_set<uint32_t>* exit_blocks) const;
  123. // Clears and fills |merging_blocks| with all basic blocks that are
  124. // post-dominated by the merge block. The merge block must exist.
  125. // The set |merging_blocks| will only contain the merge block if it is
  126. // unreachable.
  127. void GetMergingBlocks(std::unordered_set<uint32_t>* merging_blocks) const;
  128. // Returns true if the loop is in a Loop Closed SSA form.
  129. // In LCSSA form, all in-loop definitions are used in the loop or in phi
  130. // instructions in the loop exit blocks.
  131. bool IsLCSSA() const;
  132. // Returns the depth of this loop in the loop nest.
  133. // The outer-most loop has a depth of 1.
  134. inline size_t GetDepth() const {
  135. size_t lvl = 1;
  136. for (const Loop* loop = GetParent(); loop; loop = loop->GetParent()) lvl++;
  137. return lvl;
  138. }
  139. inline size_t NumImmediateChildren() const { return nested_loops_.size(); }
  140. inline bool HasChildren() const { return !nested_loops_.empty(); }
  141. // Adds |nested| as a nested loop of this loop. Automatically register |this|
  142. // as the parent of |nested|.
  143. inline void AddNestedLoop(Loop* nested) {
  144. assert(!nested->GetParent() && "The loop has another parent.");
  145. nested_loops_.push_back(nested);
  146. nested->SetParent(this);
  147. }
  148. inline Loop* GetParent() { return parent_; }
  149. inline const Loop* GetParent() const { return parent_; }
  150. inline bool HasParent() const { return parent_; }
  151. // Returns true if this loop is itself nested within another loop.
  152. inline bool IsNested() const { return parent_ != nullptr; }
  153. // Returns the set of all basic blocks contained within the loop. Will be all
  154. // BasicBlocks dominated by the header which are not also dominated by the
  155. // loop merge block.
  156. inline const BasicBlockListTy& GetBlocks() const {
  157. return loop_basic_blocks_;
  158. }
  159. // Returns true if the basic block |bb| is inside this loop.
  160. inline bool IsInsideLoop(const BasicBlock* bb) const {
  161. return IsInsideLoop(bb->id());
  162. }
  163. // Returns true if the basic block id |bb_id| is inside this loop.
  164. inline bool IsInsideLoop(uint32_t bb_id) const {
  165. return loop_basic_blocks_.count(bb_id);
  166. }
  167. // Returns true if the instruction |inst| is inside this loop.
  168. bool IsInsideLoop(Instruction* inst) const;
  169. // Adds the Basic Block |bb| to this loop and its parents.
  170. void AddBasicBlock(const BasicBlock* bb) { AddBasicBlock(bb->id()); }
  171. // Adds the Basic Block with |id| to this loop and its parents.
  172. void AddBasicBlock(uint32_t id) {
  173. for (Loop* loop = this; loop != nullptr; loop = loop->parent_) {
  174. loop->loop_basic_blocks_.insert(id);
  175. }
  176. }
  177. // Removes the Basic Block id |bb_id| from this loop and its parents.
  178. // It the user responsibility to make sure the removed block is not a merge,
  179. // header or continue block.
  180. void RemoveBasicBlock(uint32_t bb_id) {
  181. for (Loop* loop = this; loop != nullptr; loop = loop->parent_) {
  182. loop->loop_basic_blocks_.erase(bb_id);
  183. }
  184. }
  185. // Removes all the basic blocks from the set of basic blocks within the loop.
  186. // This does not affect any of the stored pointers to the header, preheader,
  187. // merge, or continue blocks.
  188. void ClearBlocks() { loop_basic_blocks_.clear(); }
  189. // Adds the Basic Block |bb| this loop and its parents.
  190. void AddBasicBlockToLoop(const BasicBlock* bb) {
  191. assert(IsBasicBlockInLoopSlow(bb) &&
  192. "Basic block does not belong to the loop");
  193. AddBasicBlock(bb);
  194. }
  195. // Returns the list of induction variables within the loop.
  196. void GetInductionVariables(std::vector<Instruction*>& inductions) const;
  197. // This function uses the |condition| to find the induction variable which is
  198. // used by the loop condition within the loop. This only works if the loop is
  199. // bound by a single condition and single induction variable.
  200. Instruction* FindConditionVariable(const BasicBlock* condition) const;
  201. // Returns the number of iterations within a loop when given the |induction|
  202. // variable and the loop |condition| check. It stores the found number of
  203. // iterations in the output parameter |iterations| and optionally, the step
  204. // value in |step_value| and the initial value of the induction variable in
  205. // |init_value|.
  206. bool FindNumberOfIterations(const Instruction* induction,
  207. const Instruction* condition, size_t* iterations,
  208. int64_t* step_amount = nullptr,
  209. int64_t* init_value = nullptr) const;
  210. // Returns the value of the OpLoopMerge control operand as a bool. Loop
  211. // control can be None(0), Unroll(1), or DontUnroll(2). This function returns
  212. // true if it is set to Unroll.
  213. inline bool HasUnrollLoopControl() const {
  214. assert(loop_header_);
  215. if (!loop_header_->GetLoopMergeInst()) return false;
  216. return loop_header_->GetLoopMergeInst()->GetSingleWordOperand(2) == 1;
  217. }
  218. // Finds the conditional block with a branch to the merge and continue blocks
  219. // within the loop body.
  220. BasicBlock* FindConditionBlock() const;
  221. // Remove the child loop form this loop.
  222. inline void RemoveChildLoop(Loop* loop) {
  223. nested_loops_.erase(
  224. std::find(nested_loops_.begin(), nested_loops_.end(), loop));
  225. loop->SetParent(nullptr);
  226. }
  227. // Mark this loop to be removed later by a call to
  228. // LoopDescriptor::PostModificationCleanup.
  229. inline void MarkLoopForRemoval() { loop_is_marked_for_removal_ = true; }
  230. // Returns whether or not this loop has been marked for removal.
  231. inline bool IsMarkedForRemoval() const { return loop_is_marked_for_removal_; }
  232. // Returns true if all nested loops have been marked for removal.
  233. inline bool AreAllChildrenMarkedForRemoval() const {
  234. for (const Loop* child : nested_loops_) {
  235. if (!child->IsMarkedForRemoval()) {
  236. return false;
  237. }
  238. }
  239. return true;
  240. }
  241. // Checks if the loop contains any instruction that will prevent it from being
  242. // cloned. If the loop is structured, the merge construct is also considered.
  243. bool IsSafeToClone() const;
  244. // Sets the parent loop of this loop, that is, a loop which contains this loop
  245. // as a nested child loop.
  246. inline void SetParent(Loop* parent) { parent_ = parent; }
  247. // Returns true is the instruction is invariant and safe to move wrt loop.
  248. bool ShouldHoistInstruction(const Instruction& inst) const;
  249. // Returns true if all operands of inst are in basic blocks not contained in
  250. // loop.
  251. bool AreAllOperandsOutsideLoop(const Instruction& inst) const;
  252. // Extract the initial value from the |induction| variable and store it in
  253. // |value|. If the function couldn't find the initial value of |induction|
  254. // return false.
  255. bool GetInductionInitValue(const Instruction* induction,
  256. int64_t* value) const;
  257. // Takes in a phi instruction |induction| and the loop |header| and returns
  258. // the step operation of the loop.
  259. Instruction* GetInductionStepOperation(const Instruction* induction) const;
  260. // Returns true if we can deduce the number of loop iterations in the step
  261. // operation |step|. IsSupportedCondition must also be true for the condition
  262. // instruction.
  263. bool IsSupportedStepOp(spv::Op step) const;
  264. // Returns true if we can deduce the number of loop iterations in the
  265. // condition operation |condition|. IsSupportedStepOp must also be true for
  266. // the step instruction.
  267. bool IsSupportedCondition(spv::Op condition) const;
  268. // Creates the list of the loop's basic block in structured order and store
  269. // the result in |ordered_loop_blocks|. If |include_pre_header| is true, the
  270. // pre-header block will also be included at the beginning of the list if it
  271. // exist. If |include_merge| is true, the merge block will also be included at
  272. // the end of the list if it exist.
  273. void ComputeLoopStructuredOrder(std::vector<BasicBlock*>* ordered_loop_blocks,
  274. bool include_pre_header = false,
  275. bool include_merge = false) const;
  276. // Given the loop |condition|, |initial_value|, |step_value|, the trip count
  277. // |number_of_iterations|, and the |unroll_factor| requested, get the new
  278. // condition value for the residual loop.
  279. static int64_t GetResidualConditionValue(spv::Op condition,
  280. int64_t initial_value,
  281. int64_t step_value,
  282. size_t number_of_iterations,
  283. size_t unroll_factor);
  284. // Returns the condition instruction for entry into the loop
  285. // Returns nullptr if it can't be found.
  286. Instruction* GetConditionInst() const;
  287. // Returns the context associated this loop.
  288. IRContext* GetContext() const { return context_; }
  289. // Looks at all the blocks with a branch to the header block to find one
  290. // which is also dominated by the loop continue block. This block is the latch
  291. // block. The specification mandates that this block should exist, therefore
  292. // this function will assert if it is not found.
  293. BasicBlock* FindLatchBlock();
  294. private:
  295. IRContext* context_;
  296. // The block which marks the start of the loop.
  297. BasicBlock* loop_header_;
  298. // The block which begins the body of the loop.
  299. BasicBlock* loop_continue_;
  300. // The block which marks the end of the loop.
  301. BasicBlock* loop_merge_;
  302. // The block immediately before the loop header.
  303. BasicBlock* loop_preheader_;
  304. // The block containing the backedge to the loop header.
  305. BasicBlock* loop_latch_;
  306. // A parent of a loop is the loop which contains it as a nested child loop.
  307. Loop* parent_;
  308. // Nested child loops of this loop.
  309. ChildrenList nested_loops_;
  310. // A set of all the basic blocks which comprise the loop structure. Will be
  311. // computed only when needed on demand.
  312. BasicBlockListTy loop_basic_blocks_;
  313. // Check that |bb| is inside the loop using domination property.
  314. // Note: this is for assertion purposes only, IsInsideLoop should be used
  315. // instead.
  316. bool IsBasicBlockInLoopSlow(const BasicBlock* bb);
  317. // Returns the loop preheader if it exists, returns nullptr otherwise.
  318. BasicBlock* FindLoopPreheader(DominatorAnalysis* dom_analysis);
  319. // Sets |latch| as the loop unique latch block. No checks are performed
  320. // here.
  321. inline void SetLatchBlockImpl(BasicBlock* latch) { loop_latch_ = latch; }
  322. // Sets |merge| as the loop merge block. No checks are performed here.
  323. inline void SetMergeBlockImpl(BasicBlock* merge) { loop_merge_ = merge; }
  324. // Each different loop |condition| affects how we calculate the number of
  325. // iterations using the |condition_value|, |init_value|, and |step_values| of
  326. // the induction variable. This method will return the number of iterations in
  327. // a loop with those values for a given |condition|. Returns 0 if the number
  328. // of iterations could not be computed.
  329. int64_t GetIterations(spv::Op condition, int64_t condition_value,
  330. int64_t init_value, int64_t step_value) const;
  331. // This is to allow for loops to be removed mid iteration without invalidating
  332. // the iterators.
  333. bool loop_is_marked_for_removal_;
  334. // This is only to allow LoopDescriptor::placeholder_top_loop_ to add top
  335. // level loops as child.
  336. friend class LoopDescriptor;
  337. friend class LoopUtils;
  338. };
  339. // Loop descriptions class for a given function.
  340. // For a given function, the class builds loop nests information.
  341. // The analysis expects a structured control flow.
  342. class LoopDescriptor {
  343. public:
  344. // Iterator interface (depth first postorder traversal).
  345. using iterator = PostOrderTreeDFIterator<Loop>;
  346. using const_iterator = PostOrderTreeDFIterator<const Loop>;
  347. using pre_iterator = TreeDFIterator<Loop>;
  348. using const_pre_iterator = TreeDFIterator<const Loop>;
  349. // The status of processing a module.
  350. using Status = utils::Status;
  351. // Creates a loop object for all loops found in |f|.
  352. LoopDescriptor(IRContext* context, const Function* f);
  353. // Disable copy constructor, to avoid double-free on destruction.
  354. LoopDescriptor(const LoopDescriptor&) = delete;
  355. // Move constructor.
  356. LoopDescriptor(LoopDescriptor&& other) : placeholder_top_loop_(nullptr) {
  357. // We need to take ownership of the Loop objects in the other
  358. // LoopDescriptor, to avoid double-free.
  359. loops_ = std::move(other.loops_);
  360. other.loops_.clear();
  361. basic_block_to_loop_ = std::move(other.basic_block_to_loop_);
  362. other.basic_block_to_loop_.clear();
  363. placeholder_top_loop_ = std::move(other.placeholder_top_loop_);
  364. }
  365. // Destructor
  366. ~LoopDescriptor();
  367. // Returns the number of loops found in the function.
  368. inline size_t NumLoops() const { return loops_.size(); }
  369. // Returns the loop at a particular |index|. The |index| must be in bounds,
  370. // check with NumLoops before calling.
  371. inline Loop& GetLoopByIndex(size_t index) const {
  372. assert(loops_.size() > index &&
  373. "Index out of range (larger than loop count)");
  374. return *loops_[index];
  375. }
  376. // Returns the loops in |this| in the order their headers appear in the
  377. // binary.
  378. std::vector<Loop*> GetLoopsInBinaryLayoutOrder();
  379. // Returns the inner most loop that contains the basic block id |block_id|.
  380. inline Loop* operator[](uint32_t block_id) const {
  381. return FindLoopForBasicBlock(block_id);
  382. }
  383. // Returns the inner most loop that contains the basic block |bb|.
  384. inline Loop* operator[](const BasicBlock* bb) const {
  385. return (*this)[bb->id()];
  386. }
  387. // Iterators for post order depth first traversal of the loops.
  388. // Inner most loops will be visited first.
  389. inline iterator begin() { return iterator::begin(&placeholder_top_loop_); }
  390. inline iterator end() { return iterator::end(&placeholder_top_loop_); }
  391. inline const_iterator begin() const { return cbegin(); }
  392. inline const_iterator end() const { return cend(); }
  393. inline const_iterator cbegin() const {
  394. return const_iterator::begin(&placeholder_top_loop_);
  395. }
  396. inline const_iterator cend() const {
  397. return const_iterator::end(&placeholder_top_loop_);
  398. }
  399. // Iterators for pre-order depth first traversal of the loops.
  400. // Inner most loops will be visited first.
  401. inline pre_iterator pre_begin() {
  402. return ++pre_iterator(&placeholder_top_loop_);
  403. }
  404. inline pre_iterator pre_end() { return pre_iterator(); }
  405. inline const_pre_iterator pre_begin() const { return pre_cbegin(); }
  406. inline const_pre_iterator pre_end() const { return pre_cend(); }
  407. inline const_pre_iterator pre_cbegin() const {
  408. return ++const_pre_iterator(&placeholder_top_loop_);
  409. }
  410. inline const_pre_iterator pre_cend() const { return const_pre_iterator(); }
  411. // Returns the inner most loop that contains the basic block |bb|.
  412. inline void SetBasicBlockToLoop(uint32_t bb_id, Loop* loop) {
  413. basic_block_to_loop_[bb_id] = loop;
  414. }
  415. // Mark the loop |loop_to_add| as needing to be added when the user calls
  416. // PostModificationCleanup. |parent| may be null.
  417. inline void AddLoop(std::unique_ptr<Loop>&& loop_to_add, Loop* parent) {
  418. loops_to_add_.emplace_back(std::make_pair(parent, std::move(loop_to_add)));
  419. }
  420. // Creates pre-header blocks for all loops in the function that do not have
  421. // one. Returns `LoopDescriptor::Status::kSuccessWithChange` if any change is
  422. // made, `LoopDescriptor::Status::kSuccessWithoutChange` if no change is made,
  423. // and `LoopDescriptor::Status::kFailure` if it fails to create a pre-header.
  424. Status CreatePreHeaderBlocksIfMissing();
  425. // Should be called to preserve the LoopAnalysis after loops have been marked
  426. // for addition with AddLoop or MarkLoopForRemoval.
  427. void PostModificationCleanup();
  428. // Removes the basic block id |bb_id| from the block to loop mapping.
  429. inline void ForgetBasicBlock(uint32_t bb_id) {
  430. basic_block_to_loop_.erase(bb_id);
  431. }
  432. // Adds the loop |new_loop| and all its nested loops to the descriptor set.
  433. // The object takes ownership of all the loops.
  434. Loop* AddLoopNest(std::unique_ptr<Loop> new_loop);
  435. // Remove the loop |loop|.
  436. void RemoveLoop(Loop* loop);
  437. void SetAsTopLoop(Loop* loop) {
  438. assert(std::find(placeholder_top_loop_.begin(), placeholder_top_loop_.end(),
  439. loop) == placeholder_top_loop_.end() &&
  440. "already registered");
  441. placeholder_top_loop_.nested_loops_.push_back(loop);
  442. }
  443. Loop* GetPlaceholderRootLoop() { return &placeholder_top_loop_; }
  444. const Loop* GetPlaceholderRootLoop() const { return &placeholder_top_loop_; }
  445. private:
  446. // TODO(dneto): This should be a vector of unique_ptr. But VisualStudio 2013
  447. // is unable to compile it.
  448. using LoopContainerType = std::vector<Loop*>;
  449. using LoopsToAddContainerType =
  450. std::vector<std::pair<Loop*, std::unique_ptr<Loop>>>;
  451. // Creates loop descriptors for the function |f|.
  452. void PopulateList(IRContext* context, const Function* f);
  453. // Returns the inner most loop that contains the basic block id |block_id|.
  454. inline Loop* FindLoopForBasicBlock(uint32_t block_id) const {
  455. std::unordered_map<uint32_t, Loop*>::const_iterator it =
  456. basic_block_to_loop_.find(block_id);
  457. return it != basic_block_to_loop_.end() ? it->second : nullptr;
  458. }
  459. // Erase all the loop information.
  460. void ClearLoops();
  461. // A list of all the loops in the function. This variable owns the Loop
  462. // objects.
  463. LoopContainerType loops_;
  464. // Placeholder root: this "loop" is only there to help iterators creation.
  465. Loop placeholder_top_loop_;
  466. std::unordered_map<uint32_t, Loop*> basic_block_to_loop_;
  467. // List of the loops marked for addition when PostModificationCleanup is
  468. // called.
  469. LoopsToAddContainerType loops_to_add_;
  470. };
  471. } // namespace opt
  472. } // namespace spvtools
  473. #endif // SOURCE_OPT_LOOP_DESCRIPTOR_H_