blob: d085609197dfda94c54a6e677303f574dc7d8155 [file] [log] [blame]
Roland Levillainccc07a92014-09-16 14:48:16 +01001/*
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 Levillain75be2832014-10-17 17:02:00 +010020#include <ostream>
21
Vladimir Marko009d1662017-10-10 13:21:15 +010022#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 Marko0a516052019-10-14 13:00:44 +000027namespace art {
Roland Levillainccc07a92014-09-16 14:48:16 +010028
29// A control-flow graph visitor performing various checks.
Nicolas Geoffray31596742014-11-24 15:28:45 +000030class GraphChecker : public HGraphDelegateVisitor {
Roland Levillainccc07a92014-09-16 14:48:16 +010031 public:
Vladimir Marko655e5852015-10-12 10:38:28 +010032 explicit GraphChecker(HGraph* graph, const char* dump_prefix = "art::GraphChecker: ")
Nicolas Geoffray31596742014-11-24 15:28:45 +000033 : HGraphDelegateVisitor(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +010034 errors_(graph->GetAllocator()->Adapter(kArenaAllocGraphChecker)),
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000035 dump_prefix_(dump_prefix),
Vladimir Marko009d1662017-10-10 13:21:15 +010036 allocator_(graph->GetArenaStack()),
37 seen_ids_(&allocator_, graph->GetCurrentInstructionId(), false, kArenaAllocGraphChecker) {
38 seen_ids_.ClearAllBits();
39 }
Roland Levillainccc07a92014-09-16 14:48:16 +010040
Aart Bika8360cd2018-05-02 16:07:51 -070041 // Check the whole graph. The pass_change parameter indicates whether changes
42 // may have occurred during the just executed pass. The default value is
43 // conservatively "true" (something may have changed). The last_size parameter
44 // and return value pass along the observed graph sizes.
45 size_t Run(bool pass_change = true, size_t last_size = 0);
Roland Levillain633021e2014-10-01 14:12:25 +010046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010047 void VisitBasicBlock(HBasicBlock* block) override;
Roland Levillainccc07a92014-09-16 14:48:16 +010048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010049 void VisitInstruction(HInstruction* instruction) override;
50 void VisitPhi(HPhi* phi) override;
Roland Levillainccc07a92014-09-16 14:48:16 +010051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010052 void VisitBinaryOperation(HBinaryOperation* op) override;
53 void VisitBooleanNot(HBooleanNot* instruction) override;
54 void VisitBoundType(HBoundType* instruction) override;
55 void VisitBoundsCheck(HBoundsCheck* check) override;
56 void VisitCheckCast(HCheckCast* check) override;
57 void VisitCondition(HCondition* op) override;
58 void VisitConstant(HConstant* instruction) override;
59 void VisitDeoptimize(HDeoptimize* instruction) override;
60 void VisitIf(HIf* instruction) override;
61 void VisitInstanceOf(HInstanceOf* check) override;
62 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) override;
63 void VisitLoadException(HLoadException* load) override;
64 void VisitNeg(HNeg* instruction) override;
65 void VisitPackedSwitch(HPackedSwitch* instruction) override;
66 void VisitReturn(HReturn* ret) override;
67 void VisitReturnVoid(HReturnVoid* ret) override;
68 void VisitSelect(HSelect* instruction) override;
69 void VisitTryBoundary(HTryBoundary* try_boundary) override;
70 void VisitTypeConversion(HTypeConversion* instruction) override;
David Brazdilbadd8262016-02-02 16:28:56 +000071
Vladimir Marko175e7862018-03-27 09:03:13 +000072 void CheckTypeCheckBitstringInput(HTypeCheckInstruction* check,
73 size_t input_pos,
74 bool check_value,
75 uint32_t expected_value,
76 const char* name);
77 void HandleTypeCheckInstruction(HTypeCheckInstruction* instruction);
David Brazdilbadd8262016-02-02 16:28:56 +000078 void HandleLoop(HBasicBlock* loop_header);
79 void HandleBooleanInput(HInstruction* instruction, size_t input_index);
David Brazdilfc6a86a2015-06-26 10:33:45 +000080
Roland Levillainccc07a92014-09-16 14:48:16 +010081 // Was the last visit of the graph valid?
82 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080083 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010084 }
85
86 // Get the list of detected errors.
Vladimir Marko655e5852015-10-12 10:38:28 +010087 const ArenaVector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010088 return errors_;
89 }
90
Roland Levillain75be2832014-10-17 17:02:00 +010091 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070092 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080093 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
94 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010095 }
96 }
97
Roland Levillainccc07a92014-09-16 14:48:16 +010098 protected:
Roland Levillain5c4405e2015-01-21 11:39:58 +000099 // Report a new error.
100 void AddError(const std::string& error) {
101 errors_.push_back(error);
102 }
103
Roland Levillainccc07a92014-09-16 14:48:16 +0100104 // The block currently visited.
105 HBasicBlock* current_block_ = nullptr;
106 // Errors encountered while checking the graph.
Vladimir Marko655e5852015-10-12 10:38:28 +0100107 ArenaVector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +0100108
109 private:
Roland Levillain75be2832014-10-17 17:02:00 +0100110 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700111 const char* const dump_prefix_;
Vladimir Marko009d1662017-10-10 13:21:15 +0100112 ScopedArenaAllocator allocator_;
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000113 ArenaBitVector seen_ids_;
Roland Levillain75be2832014-10-17 17:02:00 +0100114
Roland Levillainccc07a92014-09-16 14:48:16 +0100115 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
116};
117
Roland Levillainccc07a92014-09-16 14:48:16 +0100118} // namespace art
119
120#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_