Home | History | Annotate | Download | only in opt
      1 // Copyright (c) 2017 Google Inc.
      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_DEAD_VARIABLE_ELIMINATION_H_
     16 #define SOURCE_OPT_DEAD_VARIABLE_ELIMINATION_H_
     17 
     18 #include <climits>
     19 #include <unordered_map>
     20 
     21 #include "source/opt/decoration_manager.h"
     22 #include "source/opt/mem_pass.h"
     23 
     24 namespace spvtools {
     25 namespace opt {
     26 
     27 class DeadVariableElimination : public MemPass {
     28  public:
     29   const char* name() const override { return "eliminate-dead-variables"; }
     30   Status Process() override;
     31 
     32   IRContext::Analysis GetPreservedAnalyses() override {
     33     return IRContext::kAnalysisDefUse;
     34   }
     35 
     36  private:
     37   // Deletes the OpVariable instruction who result id is |result_id|.
     38   void DeleteVariable(uint32_t result_id);
     39 
     40   // Keeps track of the number of references of an id.  Once that value is 0, it
     41   // is safe to remove the corresponding instruction.
     42   //
     43   // Note that the special value kMustKeep is used to indicate that the
     44   // instruction cannot be deleted for reasons other that is being explicitly
     45   // referenced.
     46   std::unordered_map<uint32_t, size_t> reference_count_;
     47 
     48   // Special value used to indicate that an id cannot be safely deleted.
     49   enum { kMustKeep = INT_MAX };
     50 };
     51 
     52 }  // namespace opt
     53 }  // namespace spvtools
     54 
     55 #endif  // SOURCE_OPT_DEAD_VARIABLE_ELIMINATION_H_
     56