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