blob: 0f0b49d240a8e64790bbad4699ee2a84c142283b [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
Roland Levillainccc07a92014-09-16 14:48:16 +010027namespace art {
28
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
David Brazdilbadd8262016-02-02 16:28:56 +000041 // Check the whole graph (in reverse post-order).
42 void Run() {
43 // VisitReversePostOrder is used instead of VisitInsertionOrder,
44 // as the latter might visit dead blocks removed by the dominator
45 // computation.
46 VisitReversePostOrder();
47 }
Roland Levillain633021e2014-10-01 14:12:25 +010048
Nicolas Geoffray31596742014-11-24 15:28:45 +000049 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010050
Nicolas Geoffray31596742014-11-24 15:28:45 +000051 void VisitInstruction(HInstruction* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000052 void VisitPhi(HPhi* phi) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010053
David Brazdilbadd8262016-02-02 16:28:56 +000054 void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE;
55 void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE;
56 void VisitBoundType(HBoundType* instruction) OVERRIDE;
Mark Mendell1152c922015-04-24 17:06:35 -040057 void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE;
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +010058 void VisitCheckCast(HCheckCast* check) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000059 void VisitCondition(HCondition* op) OVERRIDE;
60 void VisitConstant(HConstant* instruction) OVERRIDE;
Nicolas Geoffray93a18c52016-04-22 13:16:14 +010061 void VisitDeoptimize(HDeoptimize* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000062 void VisitIf(HIf* instruction) OVERRIDE;
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +010063 void VisitInstanceOf(HInstanceOf* check) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000064 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE;
65 void VisitLoadException(HLoadException* load) OVERRIDE;
Roland Levillain937e6cd2016-03-22 11:54:37 +000066 void VisitNeg(HNeg* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000067 void VisitPackedSwitch(HPackedSwitch* instruction) OVERRIDE;
David Brazdilfc6a86a2015-06-26 10:33:45 +000068 void VisitReturn(HReturn* ret) OVERRIDE;
69 void VisitReturnVoid(HReturnVoid* ret) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000070 void VisitSelect(HSelect* instruction) OVERRIDE;
71 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE;
Roland Levillainf355c3f2016-03-30 19:09:03 +010072 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000073
74 void HandleLoop(HBasicBlock* loop_header);
75 void HandleBooleanInput(HInstruction* instruction, size_t input_index);
David Brazdilfc6a86a2015-06-26 10:33:45 +000076
Roland Levillainccc07a92014-09-16 14:48:16 +010077 // Was the last visit of the graph valid?
78 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080079 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010080 }
81
82 // Get the list of detected errors.
Vladimir Marko655e5852015-10-12 10:38:28 +010083 const ArenaVector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010084 return errors_;
85 }
86
Roland Levillain75be2832014-10-17 17:02:00 +010087 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070088 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080089 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
90 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010091 }
92 }
93
Roland Levillainccc07a92014-09-16 14:48:16 +010094 protected:
Roland Levillain5c4405e2015-01-21 11:39:58 +000095 // Report a new error.
96 void AddError(const std::string& error) {
97 errors_.push_back(error);
98 }
99
Roland Levillainccc07a92014-09-16 14:48:16 +0100100 // The block currently visited.
101 HBasicBlock* current_block_ = nullptr;
102 // Errors encountered while checking the graph.
Vladimir Marko655e5852015-10-12 10:38:28 +0100103 ArenaVector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +0100104
105 private:
Roland Levillain75be2832014-10-17 17:02:00 +0100106 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700107 const char* const dump_prefix_;
Vladimir Marko009d1662017-10-10 13:21:15 +0100108 ScopedArenaAllocator allocator_;
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000109 ArenaBitVector seen_ids_;
Roland Levillain75be2832014-10-17 17:02:00 +0100110
Roland Levillainccc07a92014-09-16 14:48:16 +0100111 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
112};
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114} // namespace art
115
116#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_