| 1 |  | 
|---|
| 2 | // Copyright (c) 2017 Google Inc. | 
|---|
| 3 | // | 
|---|
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|---|
| 5 | // you may not use this file except in compliance with the License. | 
|---|
| 6 | // You may obtain a copy of the License at | 
|---|
| 7 | // | 
|---|
| 8 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|---|
| 9 | // | 
|---|
| 10 | // Unless required by applicable law or agreed to in writing, software | 
|---|
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|---|
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|---|
| 13 | // See the License for the specific language governing permissions and | 
|---|
| 14 | // limitations under the License. | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef SOURCE_OPT_INSTRUCTION_LIST_H_ | 
|---|
| 17 | #define SOURCE_OPT_INSTRUCTION_LIST_H_ | 
|---|
| 18 |  | 
|---|
| 19 | #include <cassert> | 
|---|
| 20 | #include <functional> | 
|---|
| 21 | #include <memory> | 
|---|
| 22 | #include <utility> | 
|---|
| 23 | #include <vector> | 
|---|
| 24 |  | 
|---|
| 25 | #include "source/latest_version_spirv_header.h" | 
|---|
| 26 | #include "source/operand.h" | 
|---|
| 27 | #include "source/opt/instruction.h" | 
|---|
| 28 | #include "source/util/ilist.h" | 
|---|
| 29 | #include "spirv-tools/libspirv.h" | 
|---|
| 30 |  | 
|---|
| 31 | namespace spvtools { | 
|---|
| 32 | namespace opt { | 
|---|
| 33 |  | 
|---|
| 34 | // This class is intended to be the container for Instructions.  This container | 
|---|
| 35 | // owns the instructions that are in it.  When removing an Instruction from the | 
|---|
| 36 | // list, the caller is assuming responsibility for deleting the storage. | 
|---|
| 37 | // | 
|---|
| 38 | // TODO: Because there are a number of other data structures that will want | 
|---|
| 39 | // pointers to instruction, ownership should probably be moved to the module. | 
|---|
| 40 | // Because of that I have not made the ownership passing in this class fully | 
|---|
| 41 | // explicit.  For example, RemoveFromList takes ownership from the list, but | 
|---|
| 42 | // does not return an std::unique_ptr to signal that.  When we fully decide on | 
|---|
| 43 | // ownership, this will have to be fixed up one way or the other. | 
|---|
| 44 | class InstructionList : public utils::IntrusiveList<Instruction> { | 
|---|
| 45 | public: | 
|---|
| 46 | InstructionList() = default; | 
|---|
| 47 | InstructionList(InstructionList&& that) | 
|---|
| 48 | : utils::IntrusiveList<Instruction>(std::move(that)) {} | 
|---|
| 49 | InstructionList& operator=(InstructionList&& that) { | 
|---|
| 50 | auto p = static_cast<utils::IntrusiveList<Instruction>*>(this); | 
|---|
| 51 | *p = std::move(that); | 
|---|
| 52 | return *this; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | // Destroy this list and any instructions in the list. | 
|---|
| 56 | inline virtual ~InstructionList(); | 
|---|
| 57 |  | 
|---|
| 58 | class iterator : public utils::IntrusiveList<Instruction>::iterator { | 
|---|
| 59 | public: | 
|---|
| 60 | iterator(const utils::IntrusiveList<Instruction>::iterator& i) | 
|---|
| 61 | : utils::IntrusiveList<Instruction>::iterator(i) {} | 
|---|
| 62 | iterator(Instruction* i) : utils::IntrusiveList<Instruction>::iterator(i) {} | 
|---|
| 63 |  | 
|---|
| 64 | // DEPRECATED: Please use MoveBefore with an InstructionList instead. | 
|---|
| 65 | // | 
|---|
| 66 | // Moves the nodes in |list| to the list that |this| points to.  The | 
|---|
| 67 | // positions of the nodes will be immediately before the element pointed to | 
|---|
| 68 | // by the iterator.  The return value will be an iterator pointing to the | 
|---|
| 69 | // first of the newly inserted elements.  Ownership of the elements in | 
|---|
| 70 | // |list| is now passed on to |*this|. | 
|---|
| 71 | iterator InsertBefore(std::vector<std::unique_ptr<Instruction>>&& list); | 
|---|
| 72 |  | 
|---|
| 73 | // The node |i| will be inserted immediately before |this|. The return value | 
|---|
| 74 | // will be an iterator pointing to the newly inserted node.  The owner of | 
|---|
| 75 | // |*i| becomes |*this| | 
|---|
| 76 | iterator InsertBefore(std::unique_ptr<Instruction>&& i); | 
|---|
| 77 |  | 
|---|
| 78 | // Removes the node from the list, and deletes the storage.  Returns a valid | 
|---|
| 79 | // iterator to the next node. | 
|---|
| 80 | iterator Erase() { | 
|---|
| 81 | iterator_template next_node = *this; | 
|---|
| 82 | ++next_node; | 
|---|
| 83 | node_->RemoveFromList(); | 
|---|
| 84 | delete node_; | 
|---|
| 85 | return next_node; | 
|---|
| 86 | } | 
|---|
| 87 | }; | 
|---|
| 88 |  | 
|---|
| 89 | iterator begin() { return utils::IntrusiveList<Instruction>::begin(); } | 
|---|
| 90 | iterator end() { return utils::IntrusiveList<Instruction>::end(); } | 
|---|
| 91 | const_iterator begin() const { | 
|---|
| 92 | return utils::IntrusiveList<Instruction>::begin(); | 
|---|
| 93 | } | 
|---|
| 94 | const_iterator end() const { | 
|---|
| 95 | return utils::IntrusiveList<Instruction>::end(); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void push_back(std::unique_ptr<Instruction>&& inst) { | 
|---|
| 99 | utils::IntrusiveList<Instruction>::push_back(inst.release()); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | // Same as in the base class, except it will delete the data as well. | 
|---|
| 103 | inline void clear(); | 
|---|
| 104 |  | 
|---|
| 105 | // Runs the given function |f| on the instructions in the list and optionally | 
|---|
| 106 | // on the preceding debug line instructions. | 
|---|
| 107 | inline void ForEachInst(const std::function<void(Instruction*)>& f, | 
|---|
| 108 | bool run_on_debug_line_insts) { | 
|---|
| 109 | auto next = begin(); | 
|---|
| 110 | for (auto i = next; i != end(); i = next) { | 
|---|
| 111 | ++next; | 
|---|
| 112 | i->ForEachInst(f, run_on_debug_line_insts); | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 | }; | 
|---|
| 116 |  | 
|---|
| 117 | InstructionList::~InstructionList() { clear(); } | 
|---|
| 118 |  | 
|---|
| 119 | void InstructionList::clear() { | 
|---|
| 120 | while (!empty()) { | 
|---|
| 121 | Instruction* inst = &front(); | 
|---|
| 122 | inst->RemoveFromList(); | 
|---|
| 123 | delete inst; | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | }  // namespace opt | 
|---|
| 128 | }  // namespace spvtools | 
|---|
| 129 |  | 
|---|
| 130 | #endif  // SOURCE_OPT_INSTRUCTION_LIST_H_ | 
|---|
| 131 |  | 
|---|