1 // Copyright (c) 2020 Stefano Milizia
2 // Copyright (c) 2020 Google LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SOURCE_FUZZ_FUZZER_PASS_INTERCHANGE_ZERO_LIKE_CONSTANTS_H_
17 #define SOURCE_FUZZ_FUZZER_PASS_INTERCHANGE_ZERO_LIKE_CONSTANTS_H_
18 
19 #include "source/fuzz/fuzzer_pass.h"
20 
21 namespace spvtools {
22 namespace fuzz {
23 
24 // A pass that:
25 // - Finds all the zero-like constant definitions in the module and adds the
26 //   definitions of the corresponding synonym, recording the fact that they
27 //   are synonymous. If the synonym is already in the module, it does not
28 //   add a new one.
29 // - For each use of a zero-like constant, decides whether to change it to the
30 //   id of the toggled constant.
31 class FuzzerPassInterchangeZeroLikeConstants : public FuzzerPass {
32  public:
33   FuzzerPassInterchangeZeroLikeConstants(
34       opt::IRContext* ir_context, TransformationContext* transformation_context,
35       FuzzerContext* fuzzer_context,
36       protobufs::TransformationSequence* transformations);
37 
38   ~FuzzerPassInterchangeZeroLikeConstants() override;
39 
40   void Apply() override;
41 
42  private:
43   // Given the declaration of a zero-like constant, it finds or creates the
44   // corresponding toggled constant (a scalar constant of value 0 becomes a
45   // null constant of the same type and vice versa).
46   // Returns the id of the toggled instruction if the constant is zero-like,
47   // 0 otherwise.
48   uint32_t FindOrCreateToggledConstant(opt::Instruction* declaration);
49 };
50 
51 }  // namespace fuzz
52 }  // namespace spvtools
53 #endif  // SOURCE_FUZZ_FUZZER_PASS_INTERCHANGE_ZERO_LIKE_CONSTANTS_H_
54