1 // Copyright (c) 2016 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
15 #ifndef SOURCE_OPT_FUNCTION_H_
16 #define SOURCE_OPT_FUNCTION_H_
17
18 #include <algorithm>
19 #include <functional>
20 #include <iterator>
21 #include <memory>
22 #include <string>
23 #include <unordered_set>
24 #include <utility>
25 #include <vector>
26
27 #include "source/opt/basic_block.h"
28 #include "source/opt/instruction.h"
29 #include "source/opt/iterator.h"
30
31 namespace spvtools {
32 namespace opt {
33
34 class CFG;
35 class IRContext;
36 class Module;
37
38 // A SPIR-V function.
39 class Function {
40 public:
41 using iterator = UptrVectorIterator<BasicBlock>;
42 using const_iterator = UptrVectorIterator<BasicBlock, true>;
43 using ParamList = std::vector<std::unique_ptr<Instruction>>;
44
45 // Creates a function instance declared by the given OpFunction instruction
46 // |def_inst|.
47 inline explicit Function(std::unique_ptr<Instruction> def_inst);
48
49 explicit Function(const Function& f) = delete;
50
51 // Creates a clone of the instruction in the given |context|
52 //
53 // The parent module will default to null and needs to be explicitly set by
54 // the user.
55 Function* Clone(IRContext*) const;
56 // The OpFunction instruction that begins the definition of this function.
DefInst()57 Instruction& DefInst() { return *def_inst_; }
DefInst()58 const Instruction& DefInst() const { return *def_inst_; }
59
60 // Appends a parameter to this function.
61 inline void AddParameter(std::unique_ptr<Instruction> p);
62 // Appends a debug instruction in function header to this function.
63 inline void AddDebugInstructionInHeader(std::unique_ptr<Instruction> p);
64 // Appends a basic block to this function.
65 inline void AddBasicBlock(std::unique_ptr<BasicBlock> b);
66 // Appends a basic block to this function at the position |ip|.
67 inline void AddBasicBlock(std::unique_ptr<BasicBlock> b, iterator ip);
68 template <typename T>
69 inline void AddBasicBlocks(T begin, T end, iterator ip);
70
71 // Move basic block with |id| to the position after |ip|. Both have to be
72 // contained in this function.
73 inline void MoveBasicBlockToAfter(uint32_t id, BasicBlock* ip);
74
75 // Delete all basic blocks that contain no instructions.
76 inline void RemoveEmptyBlocks();
77
78 // Removes a parameter from the function with result id equal to |id|.
79 // Does nothing if the function doesn't have such a parameter.
80 inline void RemoveParameter(uint32_t id);
81
82 // Rewrites the function parameters by calling a replacer callback.
83 // The replacer takes two parameters: an expiring unique pointer to a current
84 // instruction, and a back-inserter into a new list of unique pointers to
85 // instructions. The replacer is called for each current parameter, in order.
86 // Not valid to call while also iterating through the parameter list, e.g.
87 // within the ForEachParam method.
88 using RewriteParamFn = std::function<void(
89 std::unique_ptr<Instruction>&&, std::back_insert_iterator<ParamList>&)>;
RewriteParams(RewriteParamFn & replacer)90 void RewriteParams(RewriteParamFn& replacer) {
91 ParamList new_params;
92 auto appender = std::back_inserter(new_params);
93 for (auto& param : params_) {
94 replacer(std::move(param), appender);
95 }
96 params_ = std::move(new_params);
97 }
98
99 // Saves the given function end instruction.
100 inline void SetFunctionEnd(std::unique_ptr<Instruction> end_inst);
101
102 // Add a non-semantic instruction that succeeds this function in the module.
103 // These instructions are maintained in the order they are added.
104 inline void AddNonSemanticInstruction(
105 std::unique_ptr<Instruction> non_semantic);
106
107 // Returns the given function end instruction.
EndInst()108 inline Instruction* EndInst() { return end_inst_.get(); }
EndInst()109 inline const Instruction* EndInst() const { return end_inst_.get(); }
110
111 // Returns function's id
result_id()112 inline uint32_t result_id() const { return def_inst_->result_id(); }
113
114 // Returns function's return type id
type_id()115 inline uint32_t type_id() const { return def_inst_->type_id(); }
116
117 // Returns the function's control mask
control_mask()118 inline uint32_t control_mask() const { return def_inst_->GetSingleWordInOperand(0); }
119
120 // Returns the entry basic block for this function.
entry()121 const std::unique_ptr<BasicBlock>& entry() const { return blocks_.front(); }
122
123 // Returns the last basic block in this function.
tail()124 BasicBlock* tail() { return blocks_.back().get(); }
tail()125 const BasicBlock* tail() const { return blocks_.back().get(); }
126
begin()127 iterator begin() { return iterator(&blocks_, blocks_.begin()); }
end()128 iterator end() { return iterator(&blocks_, blocks_.end()); }
begin()129 const_iterator begin() const { return cbegin(); }
end()130 const_iterator end() const { return cend(); }
cbegin()131 const_iterator cbegin() const {
132 return const_iterator(&blocks_, blocks_.cbegin());
133 }
cend()134 const_iterator cend() const {
135 return const_iterator(&blocks_, blocks_.cend());
136 }
137
138 // Returns an iterator to the basic block |id|.
FindBlock(uint32_t bb_id)139 iterator FindBlock(uint32_t bb_id) {
140 return std::find_if(begin(), end(), [bb_id](const BasicBlock& it_bb) {
141 return bb_id == it_bb.id();
142 });
143 }
144
145 // Runs the given function |f| on instructions in this function, in order,
146 // and optionally on debug line instructions that might precede them and
147 // non-semantic instructions that succceed the function.
148 void ForEachInst(const std::function<void(Instruction*)>& f,
149 bool run_on_debug_line_insts = false,
150 bool run_on_non_semantic_insts = false);
151 void ForEachInst(const std::function<void(const Instruction*)>& f,
152 bool run_on_debug_line_insts = false,
153 bool run_on_non_semantic_insts = false) const;
154 // Runs the given function |f| on instructions in this function, in order,
155 // and optionally on debug line instructions that might precede them and
156 // non-semantic instructions that succeed the function. If |f| returns
157 // false, iteration is terminated and this function returns false.
158 bool WhileEachInst(const std::function<bool(Instruction*)>& f,
159 bool run_on_debug_line_insts = false,
160 bool run_on_non_semantic_insts = false);
161 bool WhileEachInst(const std::function<bool(const Instruction*)>& f,
162 bool run_on_debug_line_insts = false,
163 bool run_on_non_semantic_insts = false) const;
164
165 // Runs the given function |f| on each parameter instruction in this function,
166 // in order, and optionally on debug line instructions that might precede
167 // them.
168 void ForEachParam(const std::function<void(const Instruction*)>& f,
169 bool run_on_debug_line_insts = false) const;
170 void ForEachParam(const std::function<void(Instruction*)>& f,
171 bool run_on_debug_line_insts = false);
172
173 // Runs the given function |f| on each debug instruction in this function's
174 // header in order.
175 void ForEachDebugInstructionsInHeader(
176 const std::function<void(Instruction*)>& f);
177
178 BasicBlock* InsertBasicBlockAfter(std::unique_ptr<BasicBlock>&& new_block,
179 BasicBlock* position);
180
181 BasicBlock* InsertBasicBlockBefore(std::unique_ptr<BasicBlock>&& new_block,
182 BasicBlock* position);
183
184 // Returns true if the function has a return block other than the exit block.
185 bool HasEarlyReturn() const;
186
187 // Returns true if the function calls itself either directly or indirectly.
188 bool IsRecursive() const;
189
190 // Pretty-prints all the basic blocks in this function into a std::string.
191 //
192 // |options| are the disassembly options. SPV_BINARY_TO_TEXT_OPTION_NO_HEADER
193 // is always added to |options|.
194 std::string PrettyPrint(uint32_t options = 0u) const;
195
196 // Dump this function on stderr. Useful when running interactive
197 // debuggers.
198 void Dump() const;
199
200 // Returns true is a function declaration and not a function definition.
IsDeclaration()201 bool IsDeclaration() { return begin() == end(); }
202
203 // Reorders the basic blocks in the function to match the structured order.
204 void ReorderBasicBlocksInStructuredOrder();
205
206 private:
207 // Reorders the basic blocks in the function to match the order given by the
208 // range |{begin,end}|. The range must contain every basic block in the
209 // function, and no extras.
210 template <class It>
211 void ReorderBasicBlocks(It begin, It end);
212
213 template <class It>
214 bool ContainsAllBlocksInTheFunction(It begin, It end);
215
216 // The OpFunction instruction that begins the definition of this function.
217 std::unique_ptr<Instruction> def_inst_;
218 // All parameters to this function.
219 ParamList params_;
220 // All debug instructions in this function's header.
221 InstructionList debug_insts_in_header_;
222 // All basic blocks inside this function in specification order
223 std::vector<std::unique_ptr<BasicBlock>> blocks_;
224 // The OpFunctionEnd instruction.
225 std::unique_ptr<Instruction> end_inst_;
226 // Non-semantic instructions succeeded by this function.
227 std::vector<std::unique_ptr<Instruction>> non_semantic_;
228 };
229
230 // Pretty-prints |func| to |str|. Returns |str|.
231 std::ostream& operator<<(std::ostream& str, const Function& func);
232
Function(std::unique_ptr<Instruction> def_inst)233 inline Function::Function(std::unique_ptr<Instruction> def_inst)
234 : def_inst_(std::move(def_inst)), end_inst_() {}
235
AddParameter(std::unique_ptr<Instruction> p)236 inline void Function::AddParameter(std::unique_ptr<Instruction> p) {
237 params_.emplace_back(std::move(p));
238 }
239
AddDebugInstructionInHeader(std::unique_ptr<Instruction> p)240 inline void Function::AddDebugInstructionInHeader(
241 std::unique_ptr<Instruction> p) {
242 debug_insts_in_header_.push_back(std::move(p));
243 }
244
AddBasicBlock(std::unique_ptr<BasicBlock> b)245 inline void Function::AddBasicBlock(std::unique_ptr<BasicBlock> b) {
246 AddBasicBlock(std::move(b), end());
247 }
248
AddBasicBlock(std::unique_ptr<BasicBlock> b,iterator ip)249 inline void Function::AddBasicBlock(std::unique_ptr<BasicBlock> b,
250 iterator ip) {
251 b->SetParent(this);
252 ip.InsertBefore(std::move(b));
253 }
254
255 template <typename T>
AddBasicBlocks(T src_begin,T src_end,iterator ip)256 inline void Function::AddBasicBlocks(T src_begin, T src_end, iterator ip) {
257 blocks_.insert(ip.Get(), std::make_move_iterator(src_begin),
258 std::make_move_iterator(src_end));
259 }
260
MoveBasicBlockToAfter(uint32_t id,BasicBlock * ip)261 inline void Function::MoveBasicBlockToAfter(uint32_t id, BasicBlock* ip) {
262 std::unique_ptr<BasicBlock> block_to_move = std::move(*FindBlock(id).Get());
263 blocks_.erase(std::find(std::begin(blocks_), std::end(blocks_), nullptr));
264
265 assert(block_to_move->GetParent() == ip->GetParent() &&
266 "Both blocks have to be in the same function.");
267
268 InsertBasicBlockAfter(std::move(block_to_move), ip);
269 }
270
RemoveEmptyBlocks()271 inline void Function::RemoveEmptyBlocks() {
272 auto first_empty =
273 std::remove_if(std::begin(blocks_), std::end(blocks_),
274 [](const std::unique_ptr<BasicBlock>& bb) -> bool {
275 return bb->GetLabelInst()->opcode() == spv::Op::OpNop;
276 });
277 blocks_.erase(first_empty, std::end(blocks_));
278 }
279
RemoveParameter(uint32_t id)280 inline void Function::RemoveParameter(uint32_t id) {
281 params_.erase(std::remove_if(params_.begin(), params_.end(),
282 [id](const std::unique_ptr<Instruction>& param) {
283 return param->result_id() == id;
284 }),
285 params_.end());
286 }
287
SetFunctionEnd(std::unique_ptr<Instruction> end_inst)288 inline void Function::SetFunctionEnd(std::unique_ptr<Instruction> end_inst) {
289 end_inst_ = std::move(end_inst);
290 }
291
AddNonSemanticInstruction(std::unique_ptr<Instruction> non_semantic)292 inline void Function::AddNonSemanticInstruction(
293 std::unique_ptr<Instruction> non_semantic) {
294 non_semantic_.emplace_back(std::move(non_semantic));
295 }
296
297 template <class It>
ReorderBasicBlocks(It begin,It end)298 void Function::ReorderBasicBlocks(It begin, It end) {
299 // Asserts to make sure every node in the function is in new_order.
300 assert(ContainsAllBlocksInTheFunction(begin, end));
301
302 // We have a pointer to all the elements in order, so we can release all
303 // pointers in |block_|, and then create the new unique pointers from |{begin,
304 // end}|.
305 std::for_each(blocks_.begin(), blocks_.end(),
306 [](std::unique_ptr<BasicBlock>& bb) { bb.release(); });
307 std::transform(begin, end, blocks_.begin(), [](BasicBlock* bb) {
308 return std::unique_ptr<BasicBlock>(bb);
309 });
310 }
311
312 template <class It>
ContainsAllBlocksInTheFunction(It begin,It end)313 bool Function::ContainsAllBlocksInTheFunction(It begin, It end) {
314 std::unordered_multiset<BasicBlock*> range(begin, end);
315 if (range.size() != blocks_.size()) {
316 return false;
317 }
318
319 for (auto& bb : blocks_) {
320 if (range.count(bb.get()) == 0) return false;
321 }
322 return true;
323 }
324
325 } // namespace opt
326 } // namespace spvtools
327
328 #endif // SOURCE_OPT_FUNCTION_H_
329