1 // Copyright (c) 2019 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_DEAD_CONTINUE_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_DEAD_CONTINUE_H_
17 
18 #include <vector>
19 
20 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
21 #include "source/fuzz/transformation.h"
22 #include "source/fuzz/transformation_context.h"
23 #include "source/opt/ir_context.h"
24 
25 namespace spvtools {
26 namespace fuzz {
27 
28 class TransformationAddDeadContinue : public Transformation {
29  public:
30   explicit TransformationAddDeadContinue(
31       const protobufs::TransformationAddDeadContinue& message);
32 
33   TransformationAddDeadContinue(uint32_t from_block,
34                                 bool continue_condition_value,
35                                 std::vector<uint32_t> phi_id);
36 
37   // - |message_.from_block| must be the id of a block a in the given module.
38   // - a must be contained in a loop with continue target b
39   // - The continue target b must be dominated by the head of the loop in which
40   //   it is contained
41   // - b must not be the merge block of a selection construct
42   // - if |message_.continue_condition_value| holds (does not hold) then
43   //   OpConstantTrue (OpConstantFalse) must be present in the module
44   // - |message_.phi_ids| must be a list of ids that are all available at
45   //   |message_.from_block|
46   // - a must end with an unconditional branch to some block c.
47   // - replacing this branch with a conditional branch to b or c, with
48   //   the boolean constant associated with |message_.continue_condition_value|
49   //   as the condition, and the ids in |message_.phi_ids| used to extend any
50   //   OpPhi instructions at b as a result of the edge from a, must maintain
51   //   validity of the module.
52   //   In particular, adding an edge from somewhere in the loop to the continue
53   //   target must not prevent uses of ids in the continue target from being
54   //   dominated by the definitions of those ids.
55   bool IsApplicable(
56       opt::IRContext* ir_context,
57       const TransformationContext& transformation_context) const override;
58 
59   // Replaces the terminator of a with a conditional branch to b or c.
60   // The boolean constant associated with |message_.continue_condition_value| is
61   // used as the condition, and the order of b and c is arranged such that
62   // control is guaranteed to jump to c.
63   void Apply(opt::IRContext* ir_context,
64              TransformationContext* transformation_context) const override;
65 
66   std::unordered_set<uint32_t> GetFreshIds() const override;
67 
68   protobufs::Transformation ToMessage() const override;
69 
70  private:
71   // Used by 'Apply' to actually apply the transformation to the module of
72   // interest, and by 'IsApplicable' to do a dry-run of the transformation on a
73   // cloned module, in order to check that the transformation leads to a valid
74   // module.  This is only invoked by 'IsApplicable' after certain basic
75   // applicability checks have been made, ensuring that the invocation of this
76   // method is legal.
77   void ApplyImpl(opt::IRContext* ir_context,
78                  const TransformationContext& transformation_context) const;
79 
80   protobufs::TransformationAddDeadContinue message_;
81 };
82 
83 }  // namespace fuzz
84 }  // namespace spvtools
85 
86 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_DEAD_CONTINUE_H_
87