• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 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_REDUCE_REMOVE_UNREFERENCED_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
16 #define SOURCE_REDUCE_REMOVE_UNREFERENCED_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
17 
18 #include "source/reduce/reduction_opportunity_finder.h"
19 
20 namespace spvtools {
21 namespace reduce {
22 
23 // A finder for opportunities to remove non-control-flow instructions in blocks
24 // in cases where the instruction's id is either not referenced at all, or
25 // referenced only in a trivial manner (for example, we regard a struct type as
26 // unused if it is referenced only by struct layout decorations).  As well as
27 // making the module smaller, removing an instruction that references particular
28 // ids may create opportunities for subsequently removing the instructions that
29 // generated those ids.
30 class RemoveUnusedInstructionReductionOpportunityFinder
31     : public ReductionOpportunityFinder {
32  public:
33   explicit RemoveUnusedInstructionReductionOpportunityFinder(
34       bool remove_constants_and_undefs);
35 
36   ~RemoveUnusedInstructionReductionOpportunityFinder() override = default;
37 
38   std::string GetName() const final;
39 
40   std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities(
41       opt::IRContext* context, uint32_t target_function) const final;
42 
43  private:
44   // Returns true if and only if the only uses of |inst| are by decorations that
45   // relate intimately to the instruction (as opposed to decorations that could
46   // be removed independently), or by interface ids in OpEntryPoint.
47   bool OnlyReferencedByIntimateDecorationOrEntryPointInterface(
48       opt::IRContext* context, const opt::Instruction& inst) const;
49 
50   // Returns true if and only if |inst| is a decoration instruction that can
51   // legitimately be removed on its own (rather than one that has to be removed
52   // simultaneously with other instructions).
53   bool IsIndependentlyRemovableDecoration(const opt::Instruction& inst) const;
54 
55   bool remove_constants_and_undefs_;
56 };
57 
58 }  // namespace reduce
59 }  // namespace spvtools
60 
61 #endif  // SOURCE_REDUCE_REMOVE_UNREFERENCED_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
62