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_BREAK_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_DEAD_BREAK_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 TransformationAddDeadBreak : public Transformation {
29  public:
30   explicit TransformationAddDeadBreak(
31       const protobufs::TransformationAddDeadBreak& message);
32 
33   TransformationAddDeadBreak(uint32_t from_block, uint32_t to_block,
34                              bool break_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   // - |message_.to_block| must be the id of a block b in the given module.
39   // - if |message_.break_condition_value| holds (does not hold) then
40   //   OpConstantTrue (OpConstantFalse) must be present in the module
41   // - |message_.phi_ids| must be a list of ids that are all available at
42   //   |message_.from_block|
43   // - a and b must be in the same function.
44   // - b must be a merge block.
45   // - a must end with an unconditional branch to some block c.
46   // - replacing this branch with a conditional branch to b or c, with
47   //   the boolean constant associated with |message_.break_condition_value| as
48   //   the condition, and the ids in |message_.phi_ids| used to extend
49   //   any OpPhi instructions at b as a result of the edge from a, must
50   //   maintain validity of the module.
51   //   In particular, the new branch must not lead to violations of the rule
52   //   that a use must be dominated by its definition.
53   bool IsApplicable(
54       opt::IRContext* ir_context,
55       const TransformationContext& transformation_context) const override;
56 
57   // Replaces the terminator of a with a conditional branch to b or c.
58   // The boolean constant associated with |message_.break_condition_value| is
59   // used as the condition, and the order of b and c is arranged such that
60   // control is guaranteed to jump to c.
61   void Apply(opt::IRContext* ir_context,
62              TransformationContext* transformation_context) const override;
63 
64   std::unordered_set<uint32_t> GetFreshIds() const override;
65 
66   protobufs::Transformation ToMessage() const override;
67 
68  private:
69   // Returns true if and only if adding an edge from |bb_from| to
70   // |message_.to_block| respects structured control flow.
71   bool AddingBreakRespectsStructuredControlFlow(opt::IRContext* ir_context,
72                                                 opt::BasicBlock* bb_from) const;
73 
74   // Used by 'Apply' to actually apply the transformation to the module of
75   // interest, and by 'IsApplicable' to do a dry-run of the transformation on a
76   // cloned module, in order to check that the transformation leads to a valid
77   // module.  This is only invoked by 'IsApplicable' after certain basic
78   // applicability checks have been made, ensuring that the invocation of this
79   // method is legal.
80   void ApplyImpl(opt::IRContext* ir_context,
81                  const TransformationContext& transformation_context) const;
82 
83   protobufs::TransformationAddDeadBreak message_;
84 };
85 
86 }  // namespace fuzz
87 }  // namespace spvtools
88 
89 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_DEAD_BREAK_H_
90