Home | History | Annotate | Download | only in optimizing
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      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 
     17 #ifndef ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_
     18 #define ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_
     19 
     20 #include <ostream>
     21 
     22 #include "base/arena_bit_vector.h"
     23 #include "base/bit_vector-inl.h"
     24 #include "base/scoped_arena_allocator.h"
     25 #include "nodes.h"
     26 
     27 namespace art {
     28 
     29 // A control-flow graph visitor performing various checks.
     30 class GraphChecker : public HGraphDelegateVisitor {
     31  public:
     32   explicit GraphChecker(HGraph* graph, const char* dump_prefix = "art::GraphChecker: ")
     33     : HGraphDelegateVisitor(graph),
     34       errors_(graph->GetAllocator()->Adapter(kArenaAllocGraphChecker)),
     35       dump_prefix_(dump_prefix),
     36       allocator_(graph->GetArenaStack()),
     37       seen_ids_(&allocator_, graph->GetCurrentInstructionId(), false, kArenaAllocGraphChecker) {
     38     seen_ids_.ClearAllBits();
     39   }
     40 
     41   // Check the whole graph (in reverse post-order).
     42   void Run() {
     43     // VisitReversePostOrder is used instead of VisitInsertionOrder,
     44     // as the latter might visit dead blocks removed by the dominator
     45     // computation.
     46     VisitReversePostOrder();
     47   }
     48 
     49   void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
     50 
     51   void VisitInstruction(HInstruction* instruction) OVERRIDE;
     52   void VisitPhi(HPhi* phi) OVERRIDE;
     53 
     54   void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE;
     55   void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE;
     56   void VisitBoundType(HBoundType* instruction) OVERRIDE;
     57   void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE;
     58   void VisitCheckCast(HCheckCast* check) OVERRIDE;
     59   void VisitCondition(HCondition* op) OVERRIDE;
     60   void VisitConstant(HConstant* instruction) OVERRIDE;
     61   void VisitDeoptimize(HDeoptimize* instruction) OVERRIDE;
     62   void VisitIf(HIf* instruction) OVERRIDE;
     63   void VisitInstanceOf(HInstanceOf* check) OVERRIDE;
     64   void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE;
     65   void VisitLoadException(HLoadException* load) OVERRIDE;
     66   void VisitNeg(HNeg* instruction) OVERRIDE;
     67   void VisitPackedSwitch(HPackedSwitch* instruction) OVERRIDE;
     68   void VisitReturn(HReturn* ret) OVERRIDE;
     69   void VisitReturnVoid(HReturnVoid* ret) OVERRIDE;
     70   void VisitSelect(HSelect* instruction) OVERRIDE;
     71   void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE;
     72   void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
     73 
     74   void HandleLoop(HBasicBlock* loop_header);
     75   void HandleBooleanInput(HInstruction* instruction, size_t input_index);
     76 
     77   // Was the last visit of the graph valid?
     78   bool IsValid() const {
     79     return errors_.empty();
     80   }
     81 
     82   // Get the list of detected errors.
     83   const ArenaVector<std::string>& GetErrors() const {
     84     return errors_;
     85   }
     86 
     87   // Print detected errors on output stream `os`.
     88   void Dump(std::ostream& os) const {
     89     for (size_t i = 0, e = errors_.size(); i < e; ++i) {
     90       os << dump_prefix_ << errors_[i] << std::endl;
     91     }
     92   }
     93 
     94  protected:
     95   // Report a new error.
     96   void AddError(const std::string& error) {
     97     errors_.push_back(error);
     98   }
     99 
    100   // The block currently visited.
    101   HBasicBlock* current_block_ = nullptr;
    102   // Errors encountered while checking the graph.
    103   ArenaVector<std::string> errors_;
    104 
    105  private:
    106   // String displayed before dumped errors.
    107   const char* const dump_prefix_;
    108   ScopedArenaAllocator allocator_;
    109   ArenaBitVector seen_ids_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(GraphChecker);
    112 };
    113 
    114 }  // namespace art
    115 
    116 #endif  // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_
    117