1 // Copyright (c) 2020 Google LLC
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_FUZZ_TRANSFORMATION_ADD_LOOP_PREHEADER_H
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_LOOP_PREHEADER_H
17 
18 #include "source/fuzz/transformation.h"
19 
20 namespace spvtools {
21 namespace fuzz {
22 
23 class TransformationAddLoopPreheader : public Transformation {
24  public:
25   explicit TransformationAddLoopPreheader(
26       const protobufs::TransformationAddLoopPreheader& message);
27 
28   TransformationAddLoopPreheader(uint32_t loop_header_block, uint32_t fresh_id,
29                                  std::vector<uint32_t> phi_id);
30 
31   // - |message_.loop_header_block| must be the id of a loop header block in
32   //   the given module.
33   // - |message_.fresh_id| must be an available id.
34   // - |message_.phi_ids| must be a list of available ids.
35   //   It can be empty if the loop header only has one predecessor outside of
36   //   the loop. Otherwise, it must contain at least as many ids as OpPhi
37   //   instructions in the loop header block.
38   bool IsApplicable(
39       opt::IRContext* ir_context,
40       const TransformationContext& transformation_context) const override;
41 
42   // Adds a preheader block as the unique out-of-loop predecessor of the given
43   // loop header block. All of the existing out-of-loop predecessors of the
44   // header are changed so that they branch to the preheader instead.
45   void Apply(opt::IRContext* ir_context,
46              TransformationContext* transformation_context) const override;
47 
48   std::unordered_set<uint32_t> GetFreshIds() const override;
49 
50   protobufs::Transformation ToMessage() const override;
51 
52  private:
53   protobufs::TransformationAddLoopPreheader message_;
54 };
55 
56 }  // namespace fuzz
57 }  // namespace spvtools
58 
59 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_LOOP_PREHEADER_H
60