Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 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 "nodes.h" |
| 21 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 22 | #include <ostream> |
| 23 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 24 | namespace art { |
| 25 | |
| 26 | // A control-flow graph visitor performing various checks. |
| 27 | class GraphChecker : public HGraphVisitor { |
| 28 | public: |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 29 | GraphChecker(ArenaAllocator* allocator, HGraph* graph, |
| 30 | const char* dump_prefix = "art::GraphChecker: ") |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 31 | : HGraphVisitor(graph), |
| 32 | allocator_(allocator), |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 33 | errors_(allocator, 0), |
| 34 | dump_prefix_(dump_prefix) {} |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 35 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 36 | // Check the whole graph (in insertion order). |
| 37 | virtual void Run() { VisitInsertionOrder(); } |
| 38 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 39 | // Check `block`. |
| 40 | virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE; |
| 41 | |
| 42 | // Check `instruction`. |
| 43 | virtual void VisitInstruction(HInstruction* instruction) OVERRIDE; |
| 44 | |
| 45 | // Was the last visit of the graph valid? |
| 46 | bool IsValid() const { |
| 47 | return errors_.IsEmpty(); |
| 48 | } |
| 49 | |
| 50 | // Get the list of detected errors. |
| 51 | const GrowableArray<std::string>& GetErrors() const { |
| 52 | return errors_; |
| 53 | } |
| 54 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 55 | // Print detected errors on output stream `os`. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 56 | void Dump(std::ostream& os) const { |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 57 | for (size_t i = 0, e = errors_.Size(); i < e; ++i) { |
| 58 | os << dump_prefix_ << errors_.Get(i) << std::endl; |
| 59 | } |
| 60 | } |
| 61 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 62 | protected: |
| 63 | ArenaAllocator* const allocator_; |
| 64 | // The block currently visited. |
| 65 | HBasicBlock* current_block_ = nullptr; |
| 66 | // Errors encountered while checking the graph. |
| 67 | GrowableArray<std::string> errors_; |
| 68 | |
| 69 | private: |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 70 | // String displayed before dumped errors. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 71 | const char* const dump_prefix_; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 72 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(GraphChecker); |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | // An SSA graph visitor performing various checks. |
| 78 | class SSAChecker : public GraphChecker { |
| 79 | public: |
| 80 | typedef GraphChecker super_type; |
| 81 | |
| 82 | SSAChecker(ArenaAllocator* allocator, HGraph* graph) |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 83 | : GraphChecker(allocator, graph, "art::SSAChecker: ") {} |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 84 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 85 | // Check the whole graph (in reverse post-order). |
| 86 | virtual void Run() { |
| 87 | // VisitReversePostOrder is used instead of VisitInsertionOrder, |
| 88 | // as the latter might visit dead blocks removed by the dominator |
| 89 | // computation. |
| 90 | VisitReversePostOrder(); |
| 91 | } |
| 92 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 93 | // Perform SSA form checks on `block`. |
| 94 | virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE; |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 95 | // Loop-related checks from block `loop_header`. |
| 96 | void CheckLoop(HBasicBlock* loop_header); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 97 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 98 | // Perform SSA form checks on instructions. |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 99 | virtual void VisitInstruction(HInstruction* instruction) OVERRIDE; |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 100 | virtual void VisitPhi(HPhi* phi) OVERRIDE; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | DISALLOW_COPY_AND_ASSIGN(SSAChecker); |
| 104 | }; |
| 105 | |
| 106 | } // namespace art |
| 107 | |
| 108 | #endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_ |