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 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 45 | // Perform control-flow graph checks on instruction. |
| 46 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE; |
| 47 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 48 | // Check that the HasBoundsChecks() flag is set for bounds checks. |
| 49 | void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE; |
| 50 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 51 | // Check successors of blocks ending in TryBoundary. |
| 52 | void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE; |
| 53 | |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 54 | // Check that HCheckCast and HInstanceOf have HLoadClass as second input. |
| 55 | void VisitCheckCast(HCheckCast* check) OVERRIDE; |
| 56 | void VisitInstanceOf(HInstanceOf* check) OVERRIDE; |
| 57 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 58 | // Check that the Return and ReturnVoid jump to the exit block. |
| 59 | void VisitReturn(HReturn* ret) OVERRIDE; |
| 60 | void VisitReturnVoid(HReturnVoid* ret) OVERRIDE; |
| 61 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 62 | // Was the last visit of the graph valid? |
| 63 | bool IsValid() const { |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 64 | return errors_.empty(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Get the list of detected errors. |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 68 | const std::vector<std::string>& GetErrors() const { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 69 | return errors_; |
| 70 | } |
| 71 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 72 | // Print detected errors on output stream `os`. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 73 | void Dump(std::ostream& os) const { |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 74 | for (size_t i = 0, e = errors_.size(); i < e; ++i) { |
| 75 | os << dump_prefix_ << errors_[i] << std::endl; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 79 | protected: |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 80 | // Report a new error. |
| 81 | void AddError(const std::string& error) { |
| 82 | errors_.push_back(error); |
| 83 | } |
| 84 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 85 | ArenaAllocator* const allocator_; |
| 86 | // The block currently visited. |
| 87 | HBasicBlock* current_block_ = nullptr; |
| 88 | // Errors encountered while checking the graph. |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 89 | std::vector<std::string> errors_; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 90 | |
| 91 | private: |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 92 | // String displayed before dumped errors. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 93 | const char* const dump_prefix_; |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 94 | ArenaBitVector seen_ids_; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 95 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 96 | DISALLOW_COPY_AND_ASSIGN(GraphChecker); |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | // An SSA graph visitor performing various checks. |
| 101 | class SSAChecker : public GraphChecker { |
| 102 | public: |
| 103 | typedef GraphChecker super_type; |
| 104 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 105 | // 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] | 106 | SSAChecker(ArenaAllocator* allocator, HGraph* graph) |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 107 | : GraphChecker(allocator, graph, "art::SSAChecker: ") {} |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 108 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 109 | // Check the whole graph (in reverse post-order). |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 110 | void Run() OVERRIDE { |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 111 | // VisitReversePostOrder is used instead of VisitInsertionOrder, |
| 112 | // as the latter might visit dead blocks removed by the dominator |
| 113 | // computation. |
| 114 | VisitReversePostOrder(); |
| 115 | } |
| 116 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 117 | // Perform SSA form checks on `block`. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 118 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE; |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 119 | // Loop-related checks from block `loop_header`. |
| 120 | void CheckLoop(HBasicBlock* loop_header); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 121 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 122 | // Perform SSA form checks on instructions. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 123 | void VisitInstruction(HInstruction* instruction) OVERRIDE; |
| 124 | void VisitPhi(HPhi* phi) OVERRIDE; |
| 125 | void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE; |
| 126 | void VisitCondition(HCondition* op) OVERRIDE; |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 127 | void VisitIf(HIf* instruction) OVERRIDE; |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 128 | void VisitPackedSwitch(HPackedSwitch* instruction) OVERRIDE; |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 129 | void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 130 | void VisitConstant(HConstant* instruction) OVERRIDE; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 131 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 132 | void HandleBooleanInput(HInstruction* instruction, size_t input_index); |
| 133 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 134 | private: |
| 135 | DISALLOW_COPY_AND_ASSIGN(SSAChecker); |
| 136 | }; |
| 137 | |
| 138 | } // namespace art |
| 139 | |
| 140 | #endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_ |