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. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 27 | class GraphChecker : public HGraphDelegateVisitor { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 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: ") |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 31 | : HGraphDelegateVisitor(graph), |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 32 | allocator_(allocator), |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 33 | dump_prefix_(dump_prefix), |
| 34 | seen_ids_(allocator, graph->GetCurrentInstructionId(), false) {} |
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`. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 40 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 41 | |
| 42 | // Check `instruction`. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 43 | void VisitInstruction(HInstruction* instruction) OVERRIDE; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 44 | |
| 45 | // Was the last visit of the graph valid? |
| 46 | bool IsValid() const { |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 47 | return errors_.empty(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Get the list of detected errors. |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 51 | const std::vector<std::string>& GetErrors() const { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 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 { |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 57 | for (size_t i = 0, e = errors_.size(); i < e; ++i) { |
| 58 | os << dump_prefix_ << errors_[i] << std::endl; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 62 | protected: |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 63 | // Report a new error. |
| 64 | void AddError(const std::string& error) { |
| 65 | errors_.push_back(error); |
| 66 | } |
| 67 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 68 | ArenaAllocator* const allocator_; |
| 69 | // The block currently visited. |
| 70 | HBasicBlock* current_block_ = nullptr; |
| 71 | // Errors encountered while checking the graph. |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 72 | std::vector<std::string> errors_; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 73 | |
| 74 | private: |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 75 | // String displayed before dumped errors. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 76 | const char* const dump_prefix_; |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 77 | ArenaBitVector seen_ids_; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 78 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(GraphChecker); |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | // An SSA graph visitor performing various checks. |
| 84 | class SSAChecker : public GraphChecker { |
| 85 | public: |
| 86 | typedef GraphChecker super_type; |
| 87 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 88 | // TODO: There's no need to pass a separate allocator as we could get it from the graph. |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 89 | SSAChecker(ArenaAllocator* allocator, HGraph* graph) |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 90 | : GraphChecker(allocator, graph, "art::SSAChecker: ") {} |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 91 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 92 | // Check the whole graph (in reverse post-order). |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 93 | void Run() OVERRIDE { |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 94 | // VisitReversePostOrder is used instead of VisitInsertionOrder, |
| 95 | // as the latter might visit dead blocks removed by the dominator |
| 96 | // computation. |
| 97 | VisitReversePostOrder(); |
| 98 | } |
| 99 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 100 | // Perform SSA form checks on `block`. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 101 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE; |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 102 | // Loop-related checks from block `loop_header`. |
| 103 | void CheckLoop(HBasicBlock* loop_header); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 104 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 105 | // Perform SSA form checks on instructions. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 106 | void VisitInstruction(HInstruction* instruction) OVERRIDE; |
| 107 | void VisitPhi(HPhi* phi) OVERRIDE; |
| 108 | void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE; |
| 109 | void VisitCondition(HCondition* op) OVERRIDE; |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 110 | void VisitIf(HIf* instruction) OVERRIDE; |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 111 | void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 112 | void VisitConstant(HConstant* instruction) OVERRIDE; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 113 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 114 | void HandleBooleanInput(HInstruction* instruction, size_t input_index); |
| 115 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 116 | private: |
| 117 | DISALLOW_COPY_AND_ASSIGN(SSAChecker); |
| 118 | }; |
| 119 | |
| 120 | } // namespace art |
| 121 | |
| 122 | #endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_ |