blob: ba60cb99c69a38ba536b45e257f2dcbc64e39e64 [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.
Nicolas Geoffray31596742014-11-24 15:28:45 +000027class GraphChecker : public HGraphDelegateVisitor {
Roland Levillainccc07a92014-09-16 14:48:16 +010028 public:
Roland Levillain75be2832014-10-17 17:02:00 +010029 GraphChecker(ArenaAllocator* allocator, HGraph* graph,
30 const char* dump_prefix = "art::GraphChecker: ")
Nicolas Geoffray31596742014-11-24 15:28:45 +000031 : HGraphDelegateVisitor(graph),
Roland Levillainccc07a92014-09-16 14:48:16 +010032 allocator_(allocator),
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000033 dump_prefix_(dump_prefix),
34 seen_ids_(allocator, graph->GetCurrentInstructionId(), false) {}
Roland Levillainccc07a92014-09-16 14:48:16 +010035
Roland Levillain633021e2014-10-01 14:12:25 +010036 // Check the whole graph (in insertion order).
37 virtual void Run() { VisitInsertionOrder(); }
38
Roland Levillainccc07a92014-09-16 14:48:16 +010039 // Check `block`.
Nicolas Geoffray31596742014-11-24 15:28:45 +000040 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010041
42 // Check `instruction`.
Nicolas Geoffray31596742014-11-24 15:28:45 +000043 void VisitInstruction(HInstruction* instruction) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010044
45 // Was the last visit of the graph valid?
46 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080047 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010048 }
49
50 // Get the list of detected errors.
Andreas Gampe91356c02014-11-07 10:34:36 -080051 const std::vector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010052 return errors_;
53 }
54
Roland Levillain75be2832014-10-17 17:02:00 +010055 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070056 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080057 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
58 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010059 }
60 }
61
Roland Levillainccc07a92014-09-16 14:48:16 +010062 protected:
63 ArenaAllocator* const allocator_;
64 // The block currently visited.
65 HBasicBlock* current_block_ = nullptr;
66 // Errors encountered while checking the graph.
Andreas Gampe91356c02014-11-07 10:34:36 -080067 std::vector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +010068
69 private:
Roland Levillain75be2832014-10-17 17:02:00 +010070 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -070071 const char* const dump_prefix_;
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000072 ArenaBitVector seen_ids_;
Roland Levillain75be2832014-10-17 17:02:00 +010073
Roland Levillainccc07a92014-09-16 14:48:16 +010074 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
75};
76
77
78// An SSA graph visitor performing various checks.
79class SSAChecker : public GraphChecker {
80 public:
81 typedef GraphChecker super_type;
82
83 SSAChecker(ArenaAllocator* allocator, HGraph* graph)
Roland Levillain75be2832014-10-17 17:02:00 +010084 : GraphChecker(allocator, graph, "art::SSAChecker: ") {}
Roland Levillainccc07a92014-09-16 14:48:16 +010085
Roland Levillain633021e2014-10-01 14:12:25 +010086 // Check the whole graph (in reverse post-order).
Nicolas Geoffray31596742014-11-24 15:28:45 +000087 void Run() OVERRIDE {
Roland Levillain633021e2014-10-01 14:12:25 +010088 // VisitReversePostOrder is used instead of VisitInsertionOrder,
89 // as the latter might visit dead blocks removed by the dominator
90 // computation.
91 VisitReversePostOrder();
92 }
93
Roland Levillainccc07a92014-09-16 14:48:16 +010094 // Perform SSA form checks on `block`.
Nicolas Geoffray31596742014-11-24 15:28:45 +000095 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +010096 // Loop-related checks from block `loop_header`.
97 void CheckLoop(HBasicBlock* loop_header);
Roland Levillainccc07a92014-09-16 14:48:16 +010098
Roland Levillain6b879dd2014-09-22 17:13:44 +010099 // Perform SSA form checks on instructions.
Nicolas Geoffray31596742014-11-24 15:28:45 +0000100 void VisitInstruction(HInstruction* instruction) OVERRIDE;
101 void VisitPhi(HPhi* phi) OVERRIDE;
102 void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE;
103 void VisitCondition(HCondition* op) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +0100104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(SSAChecker);
107};
108
109} // namespace art
110
111#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_