instruction_list.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_INSTRUCTION_LIST_H_
  15. #define SOURCE_OPT_INSTRUCTION_LIST_H_
  16. #include <cassert>
  17. #include <functional>
  18. #include <memory>
  19. #include <utility>
  20. #include <vector>
  21. #include "source/latest_version_spirv_header.h"
  22. #include "source/operand.h"
  23. #include "source/opt/instruction.h"
  24. #include "source/util/ilist.h"
  25. #include "spirv-tools/libspirv.h"
  26. namespace spvtools {
  27. namespace opt {
  28. // This class is intended to be the container for Instructions. This container
  29. // owns the instructions that are in it. When removing an Instruction from the
  30. // list, the caller is assuming responsibility for deleting the storage.
  31. //
  32. // TODO: Because there are a number of other data structures that will want
  33. // pointers to instruction, ownership should probably be moved to the module.
  34. // Because of that I have not made the ownership passing in this class fully
  35. // explicit. For example, RemoveFromList takes ownership from the list, but
  36. // does not return an std::unique_ptr to signal that. When we fully decide on
  37. // ownership, this will have to be fixed up one way or the other.
  38. class InstructionList : public utils::IntrusiveList<Instruction> {
  39. public:
  40. InstructionList() = default;
  41. InstructionList(InstructionList&& that)
  42. : utils::IntrusiveList<Instruction>(std::move(that)) {}
  43. InstructionList& operator=(InstructionList&& that) {
  44. auto p = static_cast<utils::IntrusiveList<Instruction>*>(this);
  45. *p = std::move(that);
  46. return *this;
  47. }
  48. // Destroy this list and any instructions in the list.
  49. inline ~InstructionList() override;
  50. class iterator : public utils::IntrusiveList<Instruction>::iterator {
  51. public:
  52. iterator(const utils::IntrusiveList<Instruction>::iterator& i)
  53. : utils::IntrusiveList<Instruction>::iterator(i) {}
  54. iterator(Instruction* i) : utils::IntrusiveList<Instruction>::iterator(i) {}
  55. iterator& operator++() {
  56. utils::IntrusiveList<Instruction>::iterator::operator++();
  57. return *this;
  58. }
  59. iterator& operator--() {
  60. utils::IntrusiveList<Instruction>::iterator::operator--();
  61. return *this;
  62. }
  63. // DEPRECATED: Please use MoveBefore with an InstructionList instead.
  64. //
  65. // Moves the nodes in |list| to the list that |this| points to. The
  66. // positions of the nodes will be immediately before the element pointed to
  67. // by the iterator. The return value will be an iterator pointing to the
  68. // first of the newly inserted elements. Ownership of the elements in
  69. // |list| is now passed on to |*this|.
  70. iterator InsertBefore(std::vector<std::unique_ptr<Instruction>>&& list);
  71. // The node |i| will be inserted immediately before |this|. The return value
  72. // will be an iterator pointing to the newly inserted node. The owner of
  73. // |*i| becomes |*this|
  74. iterator InsertBefore(std::unique_ptr<Instruction>&& i);
  75. // Removes the node from the list, and deletes the storage. Returns a valid
  76. // iterator to the next node.
  77. iterator Erase() {
  78. iterator_template next_node = *this;
  79. ++next_node;
  80. node_->RemoveFromList();
  81. delete node_;
  82. return next_node;
  83. }
  84. };
  85. iterator begin() { return utils::IntrusiveList<Instruction>::begin(); }
  86. iterator end() { return utils::IntrusiveList<Instruction>::end(); }
  87. const_iterator begin() const {
  88. return utils::IntrusiveList<Instruction>::begin();
  89. }
  90. const_iterator end() const {
  91. return utils::IntrusiveList<Instruction>::end();
  92. }
  93. void push_back(std::unique_ptr<Instruction>&& inst) {
  94. utils::IntrusiveList<Instruction>::push_back(inst.release());
  95. }
  96. // Same as in the base class, except it will delete the data as well.
  97. inline void clear();
  98. // Runs the given function |f| on the instructions in the list and optionally
  99. // on the preceding debug line instructions.
  100. inline void ForEachInst(const std::function<void(Instruction*)>& f,
  101. bool run_on_debug_line_insts) {
  102. auto next = begin();
  103. for (auto i = next; i != end(); i = next) {
  104. ++next;
  105. i->ForEachInst(f, run_on_debug_line_insts);
  106. }
  107. }
  108. };
  109. InstructionList::~InstructionList() { clear(); }
  110. void InstructionList::clear() {
  111. while (!empty()) {
  112. Instruction* inst = &front();
  113. inst->RemoveFromList();
  114. delete inst;
  115. }
  116. }
  117. } // namespace opt
  118. } // namespace spvtools
  119. #endif // SOURCE_OPT_INSTRUCTION_LIST_H_