blob: 862f1b600b7c820a027a1f23867d1f784f2bb22d [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
22namespace art {
23
24// A control-flow graph visitor performing various checks.
25class GraphChecker : public HGraphVisitor {
26 public:
Nicolas Geoffray1ddbf6d2014-10-01 14:59:23 +000027 GraphChecker(ArenaAllocator* allocator, HGraph* graph)
Roland Levillainccc07a92014-09-16 14:48:16 +010028 : HGraphVisitor(graph),
29 allocator_(allocator),
Nicolas Geoffray1ddbf6d2014-10-01 14:59:23 +000030 errors_(allocator, 0) {}
Roland Levillainccc07a92014-09-16 14:48:16 +010031
Roland Levillain633021e2014-10-01 14:12:25 +010032 // Check the whole graph (in insertion order).
33 virtual void Run() { VisitInsertionOrder(); }
34
Roland Levillainccc07a92014-09-16 14:48:16 +010035 // Check `block`.
36 virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
37
38 // Check `instruction`.
39 virtual void VisitInstruction(HInstruction* instruction) OVERRIDE;
40
41 // Was the last visit of the graph valid?
42 bool IsValid() const {
43 return errors_.IsEmpty();
44 }
45
46 // Get the list of detected errors.
47 const GrowableArray<std::string>& GetErrors() const {
48 return errors_;
49 }
50
51 protected:
52 ArenaAllocator* const allocator_;
53 // The block currently visited.
54 HBasicBlock* current_block_ = nullptr;
55 // Errors encountered while checking the graph.
56 GrowableArray<std::string> errors_;
57
58 private:
59 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
60};
61
62
63// An SSA graph visitor performing various checks.
64class SSAChecker : public GraphChecker {
65 public:
66 typedef GraphChecker super_type;
67
68 SSAChecker(ArenaAllocator* allocator, HGraph* graph)
Nicolas Geoffray1ddbf6d2014-10-01 14:59:23 +000069 : GraphChecker(allocator, graph) {}
Roland Levillainccc07a92014-09-16 14:48:16 +010070
Roland Levillain633021e2014-10-01 14:12:25 +010071 // Check the whole graph (in reverse post-order).
72 virtual void Run() {
73 // VisitReversePostOrder is used instead of VisitInsertionOrder,
74 // as the latter might visit dead blocks removed by the dominator
75 // computation.
76 VisitReversePostOrder();
77 }
78
Roland Levillainccc07a92014-09-16 14:48:16 +010079 // Perform SSA form checks on `block`.
80 virtual void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010081 // Loop-related checks from block `loop_header`.
82 void CheckLoop(HBasicBlock* loop_header);
Roland Levillainccc07a92014-09-16 14:48:16 +010083
Roland Levillain6b879dd2014-09-22 17:13:44 +010084 // Perform SSA form checks on instructions.
Roland Levillainccc07a92014-09-16 14:48:16 +010085 virtual void VisitInstruction(HInstruction* instruction) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010086 virtual void VisitPhi(HPhi* phi) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010087
88 private:
89 DISALLOW_COPY_AND_ASSIGN(SSAChecker);
90};
91
92} // namespace art
93
94#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_