blob: 8ba8cb16b1f5d98c32192f9eb1ee3c8134998665 [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 Levillain75be2832014-10-17 17:02:00 +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 Levillain75be2832014-10-17 17:02:00 +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 Levillain75be2832014-10-17 17:02:00 +010033 dump_prefix_(dump_prefix) {}
Roland Levillainccc07a92014-09-16 14:48:16 +010034
Roland Levillain633021e2014-10-01 14:12:25 +010035 // Check the whole graph (in insertion order).
36 virtual void Run() { VisitInsertionOrder(); }
37
Roland Levillainccc07a92014-09-16 14:48:16 +010038 // Check `block`.
39 virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
40
41 // Check `instruction`.
42 virtual void VisitInstruction(HInstruction* instruction) OVERRIDE;
43
44 // Was the last visit of the graph valid?
45 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080046 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010047 }
48
49 // Get the list of detected errors.
Andreas Gampe91356c02014-11-07 10:34:36 -080050 const std::vector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010051 return errors_;
52 }
53
Roland Levillain75be2832014-10-17 17:02:00 +010054 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070055 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080056 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
57 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010058 }
59 }
60
Roland Levillainccc07a92014-09-16 14:48:16 +010061 protected:
62 ArenaAllocator* const allocator_;
63 // The block currently visited.
64 HBasicBlock* current_block_ = nullptr;
65 // Errors encountered while checking the graph.
Andreas Gampe91356c02014-11-07 10:34:36 -080066 std::vector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +010067
68 private:
Roland Levillain75be2832014-10-17 17:02:00 +010069 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -070070 const char* const dump_prefix_;
Roland Levillain75be2832014-10-17 17:02:00 +010071
Roland Levillainccc07a92014-09-16 14:48:16 +010072 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
73};
74
75
76// An SSA graph visitor performing various checks.
77class SSAChecker : public GraphChecker {
78 public:
79 typedef GraphChecker super_type;
80
81 SSAChecker(ArenaAllocator* allocator, HGraph* graph)
Roland Levillain75be2832014-10-17 17:02:00 +010082 : GraphChecker(allocator, graph, "art::SSAChecker: ") {}
Roland Levillainccc07a92014-09-16 14:48:16 +010083
Roland Levillain633021e2014-10-01 14:12:25 +010084 // Check the whole graph (in reverse post-order).
85 virtual void Run() {
86 // VisitReversePostOrder is used instead of VisitInsertionOrder,
87 // as the latter might visit dead blocks removed by the dominator
88 // computation.
89 VisitReversePostOrder();
90 }
91
Roland Levillainccc07a92014-09-16 14:48:16 +010092 // Perform SSA form checks on `block`.
93 virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010094 // Loop-related checks from block `loop_header`.
95 void CheckLoop(HBasicBlock* loop_header);
Roland Levillainccc07a92014-09-16 14:48:16 +010096
Roland Levillain6b879dd2014-09-22 17:13:44 +010097 // Perform SSA form checks on instructions.
Roland Levillainccc07a92014-09-16 14:48:16 +010098 virtual void VisitInstruction(HInstruction* instruction) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010099 virtual void VisitPhi(HPhi* phi) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +0100100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(SSAChecker);
103};
104
105} // namespace art
106
107#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_