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_REPLACE_IRRELEVANT_ID_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_REPLACE_IRRELEVANT_ID_H_
17 
18 #include "source/fuzz/transformation.h"
19 
20 namespace spvtools {
21 namespace fuzz {
22 
23 class TransformationReplaceIrrelevantId : public Transformation {
24  public:
25   explicit TransformationReplaceIrrelevantId(
26       const protobufs::TransformationReplaceIrrelevantId& message);
27 
28   TransformationReplaceIrrelevantId(
29       const protobufs::IdUseDescriptor& id_use_descriptor,
30       uint32_t replacement_id);
31 
32   // - The id of interest in |message_.id_use_descriptor| is irrelevant
33   //   according to the fact manager.
34   // - The types of the original id and of the replacement ids are the same.
35   // - The replacement must not be the result id of an OpFunction instruction.
36   // - |message_.replacement_id| is available to use at the enclosing
37   //   instruction of |message_.id_use_descriptor|.
38   // - The original id is in principle replaceable with any other id of the same
39   //   type. See fuzzerutil::IdUseCanBeReplaced for details.
40   bool IsApplicable(
41       opt::IRContext* ir_context,
42       const TransformationContext& transformation_context) const override;
43 
44   // Replaces the use of an irrelevant id identified by
45   // |message_.id_use_descriptor| with the id |message_.replacement_id|, which
46   // has the same type as the id of interest.
47   void Apply(opt::IRContext* ir_context,
48              TransformationContext* transformation_context) const override;
49 
50   std::unordered_set<uint32_t> GetFreshIds() const override;
51 
52   protobufs::Transformation ToMessage() const override;
53 
54   // Returns true if and only if |use_instruction| is OpVariable and
55   // |replacement_for_use| is not a constant instruction - i.e., if it would be
56   // illegal to replace the variable's initializer with the given instruction.
57   static bool AttemptsToReplaceVariableInitializerWithNonConstant(
58       const opt::Instruction& use_instruction,
59       const opt::Instruction& replacement_for_use);
60 
61  private:
62   protobufs::TransformationReplaceIrrelevantId message_;
63 };
64 
65 }  // namespace fuzz
66 }  // namespace spvtools
67 
68 #endif  // SOURCE_FUZZ_TRANSFORMATION_REPLACE_IRRELEVANT_ID_H_
69