| 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 |
iterator& operator++() { |
| 65 |
utils::IntrusiveList<Instruction>::iterator::operator++(); |
| 66 |
return *this; |
| 67 |
} |
| 68 |
|
| 69 |
iterator& operator--() { |
| 70 |
utils::IntrusiveList<Instruction>::iterator::operator--(); |
| 71 |
return *this; |
| 72 |
} |
| 73 |
|
| 74 |
// DEPRECATED: Please use MoveBefore with an InstructionList instead. |
| 75 |
// |
| 76 |
// Moves the nodes in |list| to the list that |this| points to. The |
| 77 |
// positions of the nodes will be immediately before the element pointed to |
| 78 |
// by the iterator. The return value will be an iterator pointing to the |
| 79 |
// first of the newly inserted elements. Ownership of the elements in |
| 80 |
// |list| is now passed on to |*this|. |
| 81 |
iterator InsertBefore(std::vector<std::unique_ptr<Instruction>>&& list); |
| 82 |
|
| 83 |
// The node |i| will be inserted immediately before |this|. The return value |
| 84 |
// will be an iterator pointing to the newly inserted node. The owner of |
| 85 |
// |*i| becomes |*this| |
| 86 |
iterator InsertBefore(std::unique_ptr<Instruction>&& i); |
| 87 |
|
| 88 |
// Removes the node from the list, and deletes the storage. Returns a valid |
| 89 |
// iterator to the next node. |
| 90 |
iterator Erase() { |
| 91 |
iterator_template next_node = *this; |
| 92 |
++next_node; |
| 93 |
node_->RemoveFromList(); |
| 94 |
delete node_; |
| 95 |
return next_node; |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
iterator begin() { return utils::IntrusiveList<Instruction>::begin(); } |
| 100 |
iterator end() { return utils::IntrusiveList<Instruction>::end(); } |
| 101 |
const_iterator begin() const { |
| 102 |
return utils::IntrusiveList<Instruction>::begin(); |
| 103 |
} |
| 104 |
const_iterator end() const { |
| 105 |
return utils::IntrusiveList<Instruction>::end(); |
| 106 |
} |
| 107 |
|
| 108 |
void push_back(std::unique_ptr<Instruction>&& inst) { |
| 109 |
utils::IntrusiveList<Instruction>::push_back(inst.release()); |
| 110 |
} |
| 111 |
|
| 112 |
// Same as in the base class, except it will delete the data as well. |
| 113 |
inline void clear(); |
| 114 |
|
| 115 |
// Runs the given function |f| on the instructions in the list and optionally |
| 116 |
// on the preceding debug line instructions. |
| 117 |
inline void ForEachInst(const std::function<void(Instruction*)>& f, |
| 118 |
bool run_on_debug_line_insts) { |
| 119 |
auto next = begin(); |
| 120 |
for (auto i = next; i != end(); i = next) { |
| 121 |
++next; |
| 122 |
i->ForEachInst(f, run_on_debug_line_insts); |
| 123 |
} |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
InstructionList::~InstructionList() { clear(); } |
| 128 |
|
| 129 |
void InstructionList::clear() { |
| 130 |
while (!empty()) { |
| 131 |
Instruction* inst = &front(); |
| 132 |
inst->RemoveFromList(); |
| 133 |
delete inst; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace opt |
| 138 |
} // namespace spvtools |
| 139 |
|
| 140 |
#endif // SOURCE_OPT_INSTRUCTION_LIST_H_ |