1 // Copyright (c) 2017 Pierre Moreau
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_OPT_REMOVE_DUPLICATES_PASS_H_
16 #define SOURCE_OPT_REMOVE_DUPLICATES_PASS_H_
17 
18 #include <unordered_map>
19 #include <vector>
20 
21 #include "source/opt/decoration_manager.h"
22 #include "source/opt/def_use_manager.h"
23 #include "source/opt/ir_context.h"
24 #include "source/opt/module.h"
25 #include "source/opt/pass.h"
26 
27 namespace spvtools {
28 namespace opt {
29 
30 using IdDecorationsList =
31     std::unordered_map<uint32_t, std::vector<Instruction*>>;
32 
33 // See optimizer.hpp for documentation.
34 class RemoveDuplicatesPass : public Pass {
35  public:
name()36   const char* name() const override { return "remove-duplicates"; }
37   Status Process() override;
38 
39   // TODO(pierremoreau): Move this function somewhere else (e.g. pass.h or
40   // within the type manager)
41   // Returns whether two types are equal, and have the same decorations.
42   static bool AreTypesEqual(const Instruction& inst1, const Instruction& inst2,
43                             IRContext* context);
44 
45  private:
46   // Remove duplicate capabilities from the module
47   //
48   // Returns true if the module was modified, false otherwise.
49   bool RemoveDuplicateCapabilities() const;
50   // Remove duplicate extended instruction imports from the module
51   //
52   // Returns true if the module was modified, false otherwise.
53   bool RemoveDuplicatesExtInstImports() const;
54   // Remove duplicate types from the module
55   //
56   // Returns true if the module was modified, false otherwise.
57   bool RemoveDuplicateTypes() const;
58   // Remove duplicate decorations from the module
59   //
60   // Returns true if the module was modified, false otherwise.
61   bool RemoveDuplicateDecorations() const;
62 };
63 
64 }  // namespace opt
65 }  // namespace spvtools
66 
67 #endif  // SOURCE_OPT_REMOVE_DUPLICATES_PASS_H_
68