blob: 4fb07e0ae421f4a1a5f0458c178e615fa729f411 [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
20#include "nodes.h"
21
Roland Levillainbf9cd7b2014-09-30 16:15:14 +010022#include <ostream>
23
Roland Levillainccc07a92014-09-16 14:48:16 +010024namespace art {
25
26// A control-flow graph visitor performing various checks.
27class GraphChecker : public HGraphVisitor {
28 public:
Roland Levillainbf9cd7b2014-09-30 16:15:14 +010029 GraphChecker(ArenaAllocator* allocator, HGraph* graph,
30 const char* dump_prefix = "art::GraphChecker: ")
Roland Levillainccc07a92014-09-16 14:48:16 +010031 : HGraphVisitor(graph),
32 allocator_(allocator),
Roland Levillainbf9cd7b2014-09-30 16:15:14 +010033 errors_(allocator, 0),
34 dump_prefix_(dump_prefix) {}
Roland Levillainccc07a92014-09-16 14:48:16 +010035
36 // Check `block`.
37 virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
38
39 // Check `instruction`.
40 virtual void VisitInstruction(HInstruction* instruction) OVERRIDE;
41
42 // Was the last visit of the graph valid?
43 bool IsValid() const {
44 return errors_.IsEmpty();
45 }
46
47 // Get the list of detected errors.
48 const GrowableArray<std::string>& GetErrors() const {
49 return errors_;
50 }
51
Roland Levillainbf9cd7b2014-09-30 16:15:14 +010052 // Print detected errors on output stream `os`.
53 void Dump(std::ostream& os) {
54 for (size_t i = 0, e = errors_.Size(); i < e; ++i) {
55 os << dump_prefix_ << errors_.Get(i) << std::endl;
56 }
57 }
58
Roland Levillainccc07a92014-09-16 14:48:16 +010059 protected:
60 ArenaAllocator* const allocator_;
61 // The block currently visited.
62 HBasicBlock* current_block_ = nullptr;
63 // Errors encountered while checking the graph.
64 GrowableArray<std::string> errors_;
65
66 private:
Roland Levillainbf9cd7b2014-09-30 16:15:14 +010067 // String displayed before dumped errors.
68 const char* dump_prefix_;
69
Roland Levillainccc07a92014-09-16 14:48:16 +010070 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
71};
72
73
74// An SSA graph visitor performing various checks.
75class SSAChecker : public GraphChecker {
76 public:
77 typedef GraphChecker super_type;
78
79 SSAChecker(ArenaAllocator* allocator, HGraph* graph)
Roland Levillainbf9cd7b2014-09-30 16:15:14 +010080 : GraphChecker(allocator, graph, "art::SSAChecker: ") {}
Roland Levillainccc07a92014-09-16 14:48:16 +010081
82 // Perform SSA form checks on `block`.
83 virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010084 // Loop-related checks from block `loop_header`.
85 void CheckLoop(HBasicBlock* loop_header);
Roland Levillainccc07a92014-09-16 14:48:16 +010086
Roland Levillain6b879dd2014-09-22 17:13:44 +010087 // Perform SSA form checks on instructions.
Roland Levillainccc07a92014-09-16 14:48:16 +010088 virtual void VisitInstruction(HInstruction* instruction) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010089 virtual void VisitPhi(HPhi* phi) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010090
91 private:
92 DISALLOW_COPY_AND_ASSIGN(SSAChecker);
93};
94
95} // namespace art
96
97#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_