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 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 20 | #include <ostream> |
| 21 | |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 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 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 27 | namespace art { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 28 | |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 29 | class CodeGenerator; |
| 30 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 31 | // A control-flow graph visitor performing various checks. |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 32 | class GraphChecker : public HGraphDelegateVisitor { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 33 | public: |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 34 | explicit GraphChecker(HGraph* graph, |
| 35 | CodeGenerator* codegen = nullptr, |
| 36 | const char* dump_prefix = "art::GraphChecker: ") |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 37 | : HGraphDelegateVisitor(graph), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 38 | errors_(graph->GetAllocator()->Adapter(kArenaAllocGraphChecker)), |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 39 | dump_prefix_(dump_prefix), |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 40 | allocator_(graph->GetArenaStack()), |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 41 | seen_ids_(&allocator_, graph->GetCurrentInstructionId(), false, kArenaAllocGraphChecker), |
| 42 | codegen_(codegen) { |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 43 | seen_ids_.ClearAllBits(); |
| 44 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 45 | |
Aart Bik | a8360cd | 2018-05-02 16:07:51 -0700 | [diff] [blame] | 46 | // Check the whole graph. The pass_change parameter indicates whether changes |
| 47 | // may have occurred during the just executed pass. The default value is |
| 48 | // conservatively "true" (something may have changed). The last_size parameter |
| 49 | // and return value pass along the observed graph sizes. |
| 50 | size_t Run(bool pass_change = true, size_t last_size = 0); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 51 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 52 | void VisitBasicBlock(HBasicBlock* block) override; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 53 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 54 | void VisitInstruction(HInstruction* instruction) override; |
| 55 | void VisitPhi(HPhi* phi) override; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 56 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 57 | void VisitBinaryOperation(HBinaryOperation* op) override; |
| 58 | void VisitBooleanNot(HBooleanNot* instruction) override; |
| 59 | void VisitBoundType(HBoundType* instruction) override; |
| 60 | void VisitBoundsCheck(HBoundsCheck* check) override; |
| 61 | void VisitCheckCast(HCheckCast* check) override; |
| 62 | void VisitCondition(HCondition* op) override; |
| 63 | void VisitConstant(HConstant* instruction) override; |
| 64 | void VisitDeoptimize(HDeoptimize* instruction) override; |
| 65 | void VisitIf(HIf* instruction) override; |
| 66 | void VisitInstanceOf(HInstanceOf* check) override; |
| 67 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) override; |
| 68 | void VisitLoadException(HLoadException* load) override; |
| 69 | void VisitNeg(HNeg* instruction) override; |
| 70 | void VisitPackedSwitch(HPackedSwitch* instruction) override; |
| 71 | void VisitReturn(HReturn* ret) override; |
| 72 | void VisitReturnVoid(HReturnVoid* ret) override; |
| 73 | void VisitSelect(HSelect* instruction) override; |
| 74 | void VisitTryBoundary(HTryBoundary* try_boundary) override; |
| 75 | void VisitTypeConversion(HTypeConversion* instruction) override; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 76 | |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 77 | void VisitVecOperation(HVecOperation* instruction) override; |
| 78 | |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 79 | void CheckTypeCheckBitstringInput(HTypeCheckInstruction* check, |
| 80 | size_t input_pos, |
| 81 | bool check_value, |
| 82 | uint32_t expected_value, |
| 83 | const char* name); |
| 84 | void HandleTypeCheckInstruction(HTypeCheckInstruction* instruction); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 85 | void HandleLoop(HBasicBlock* loop_header); |
| 86 | void HandleBooleanInput(HInstruction* instruction, size_t input_index); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 87 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 88 | // Was the last visit of the graph valid? |
| 89 | bool IsValid() const { |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 90 | return errors_.empty(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Get the list of detected errors. |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 94 | const ArenaVector<std::string>& GetErrors() const { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 95 | return errors_; |
| 96 | } |
| 97 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 98 | // Print detected errors on output stream `os`. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 99 | void Dump(std::ostream& os) const { |
Andreas Gampe | 91356c0 | 2014-11-07 10:34:36 -0800 | [diff] [blame] | 100 | for (size_t i = 0, e = errors_.size(); i < e; ++i) { |
| 101 | os << dump_prefix_ << errors_[i] << std::endl; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 105 | // Enable/Disable the reference type info check. |
| 106 | // |
| 107 | // Return: the previous status of the check. |
| 108 | bool SetRefTypeInfoCheckEnabled(bool value = true) { |
| 109 | bool old_value = check_reference_type_info_; |
| 110 | check_reference_type_info_ = value; |
| 111 | return old_value; |
| 112 | } |
| 113 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 114 | protected: |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 115 | // Report a new error. |
| 116 | void AddError(const std::string& error) { |
| 117 | errors_.push_back(error); |
| 118 | } |
| 119 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 120 | // The block currently visited. |
| 121 | HBasicBlock* current_block_ = nullptr; |
| 122 | // Errors encountered while checking the graph. |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 123 | ArenaVector<std::string> errors_; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 124 | |
| 125 | private: |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 126 | // String displayed before dumped errors. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 127 | const char* const dump_prefix_; |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 128 | ScopedArenaAllocator allocator_; |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 129 | ArenaBitVector seen_ids_; |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 130 | // Whether to perform the reference type info check for instructions which use or produce |
| 131 | // object references, e.g. HNewInstance, HLoadClass. |
| 132 | // The default value is true. |
| 133 | bool check_reference_type_info_ = true; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 134 | |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 135 | // Used to access target information. |
| 136 | CodeGenerator* codegen_; |
| 137 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(GraphChecker); |
| 139 | }; |
| 140 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 141 | } // namespace art |
| 142 | |
| 143 | #endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_ |