blob: 7c72e23e2d6b09b04e6eaf810d060b282095f920 [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
Roland Levillain4c0eb422015-04-24 16:43:49 +010045 // Perform control-flow graph checks on instruction.
46 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE;
47
Mark Mendell1152c922015-04-24 17:06:35 -040048 // Check that the HasBoundsChecks() flag is set for bounds checks.
49 void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE;
50
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +010051 // Check that HCheckCast and HInstanceOf have HLoadClass as second input.
52 void VisitCheckCast(HCheckCast* check) OVERRIDE;
53 void VisitInstanceOf(HInstanceOf* check) OVERRIDE;
54
David Brazdilfc6a86a2015-06-26 10:33:45 +000055 // Check that the Return and ReturnVoid jump to the exit block.
56 void VisitReturn(HReturn* ret) OVERRIDE;
57 void VisitReturnVoid(HReturnVoid* ret) OVERRIDE;
58
Roland Levillainccc07a92014-09-16 14:48:16 +010059 // Was the last visit of the graph valid?
60 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080061 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010062 }
63
64 // Get the list of detected errors.
Andreas Gampe91356c02014-11-07 10:34:36 -080065 const std::vector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010066 return errors_;
67 }
68
Roland Levillain75be2832014-10-17 17:02:00 +010069 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070070 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080071 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
72 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010073 }
74 }
75
Roland Levillainccc07a92014-09-16 14:48:16 +010076 protected:
Roland Levillain5c4405e2015-01-21 11:39:58 +000077 // Report a new error.
78 void AddError(const std::string& error) {
79 errors_.push_back(error);
80 }
81
Roland Levillainccc07a92014-09-16 14:48:16 +010082 ArenaAllocator* const allocator_;
83 // The block currently visited.
84 HBasicBlock* current_block_ = nullptr;
85 // Errors encountered while checking the graph.
Andreas Gampe91356c02014-11-07 10:34:36 -080086 std::vector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +010087
88 private:
Roland Levillain75be2832014-10-17 17:02:00 +010089 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -070090 const char* const dump_prefix_;
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000091 ArenaBitVector seen_ids_;
Roland Levillain75be2832014-10-17 17:02:00 +010092
Roland Levillainccc07a92014-09-16 14:48:16 +010093 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
94};
95
96
97// An SSA graph visitor performing various checks.
98class SSAChecker : public GraphChecker {
99 public:
100 typedef GraphChecker super_type;
101
Calin Juravlea4f88312015-04-16 12:57:19 +0100102 // TODO: There's no need to pass a separate allocator as we could get it from the graph.
Roland Levillainccc07a92014-09-16 14:48:16 +0100103 SSAChecker(ArenaAllocator* allocator, HGraph* graph)
Roland Levillain75be2832014-10-17 17:02:00 +0100104 : GraphChecker(allocator, graph, "art::SSAChecker: ") {}
Roland Levillainccc07a92014-09-16 14:48:16 +0100105
Roland Levillain633021e2014-10-01 14:12:25 +0100106 // Check the whole graph (in reverse post-order).
Nicolas Geoffray31596742014-11-24 15:28:45 +0000107 void Run() OVERRIDE {
Roland Levillain633021e2014-10-01 14:12:25 +0100108 // VisitReversePostOrder is used instead of VisitInsertionOrder,
109 // as the latter might visit dead blocks removed by the dominator
110 // computation.
111 VisitReversePostOrder();
112 }
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Perform SSA form checks on `block`.
Nicolas Geoffray31596742014-11-24 15:28:45 +0000115 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +0100116 // Loop-related checks from block `loop_header`.
117 void CheckLoop(HBasicBlock* loop_header);
Roland Levillainccc07a92014-09-16 14:48:16 +0100118
Roland Levillain6b879dd2014-09-22 17:13:44 +0100119 // Perform SSA form checks on instructions.
Nicolas Geoffray31596742014-11-24 15:28:45 +0000120 void VisitInstruction(HInstruction* instruction) OVERRIDE;
121 void VisitPhi(HPhi* phi) OVERRIDE;
122 void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE;
123 void VisitCondition(HCondition* op) OVERRIDE;
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000124 void VisitIf(HIf* instruction) OVERRIDE;
David Brazdil13b47182015-04-15 16:29:32 +0100125 void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000126 void VisitConstant(HConstant* instruction) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +0100127
David Brazdil13b47182015-04-15 16:29:32 +0100128 void HandleBooleanInput(HInstruction* instruction, size_t input_index);
129
Roland Levillainccc07a92014-09-16 14:48:16 +0100130 private:
131 DISALLOW_COPY_AND_ASSIGN(SSAChecker);
132};
133
134} // namespace art
135
136#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_