blob: 2e16bfe24536d91affef3baf8ea006fa5f749773 [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:
Vladimir Marko655e5852015-10-12 10:38:28 +010029 explicit GraphChecker(HGraph* graph, const char* dump_prefix = "art::GraphChecker: ")
Nicolas Geoffray31596742014-11-24 15:28:45 +000030 : HGraphDelegateVisitor(graph),
Vladimir Marko655e5852015-10-12 10:38:28 +010031 errors_(graph->GetArena()->Adapter(kArenaAllocGraphChecker)),
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000032 dump_prefix_(dump_prefix),
Vladimir Marko655e5852015-10-12 10:38:28 +010033 seen_ids_(graph->GetArena(), graph->GetCurrentInstructionId(), false) {}
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`.
Nicolas Geoffray31596742014-11-24 15:28:45 +000039 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010040
41 // Check `instruction`.
Nicolas Geoffray31596742014-11-24 15:28:45 +000042 void VisitInstruction(HInstruction* instruction) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010043
Roland Levillain4c0eb422015-04-24 16:43:49 +010044 // Perform control-flow graph checks on instruction.
45 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE;
46
Mark Mendell1152c922015-04-24 17:06:35 -040047 // Check that the HasBoundsChecks() flag is set for bounds checks.
48 void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE;
49
David Brazdilffee3d32015-07-06 11:48:53 +010050 // Check successors of blocks ending in TryBoundary.
51 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE;
52
David Brazdil9bc43612015-11-05 21:25:24 +000053 // Check that LoadException is the first instruction in a catch block.
54 void VisitLoadException(HLoadException* load) OVERRIDE;
55
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +010056 // Check that HCheckCast and HInstanceOf have HLoadClass as second input.
57 void VisitCheckCast(HCheckCast* check) OVERRIDE;
58 void VisitInstanceOf(HInstanceOf* check) OVERRIDE;
59
David Brazdilfc6a86a2015-06-26 10:33:45 +000060 // Check that the Return and ReturnVoid jump to the exit block.
61 void VisitReturn(HReturn* ret) OVERRIDE;
62 void VisitReturnVoid(HReturnVoid* ret) OVERRIDE;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Was the last visit of the graph valid?
65 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080066 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010067 }
68
69 // Get the list of detected errors.
Vladimir Marko655e5852015-10-12 10:38:28 +010070 const ArenaVector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010071 return errors_;
72 }
73
Roland Levillain75be2832014-10-17 17:02:00 +010074 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070075 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080076 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
77 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010078 }
79 }
80
Roland Levillainccc07a92014-09-16 14:48:16 +010081 protected:
Roland Levillain5c4405e2015-01-21 11:39:58 +000082 // Report a new error.
83 void AddError(const std::string& error) {
84 errors_.push_back(error);
85 }
86
Roland Levillainccc07a92014-09-16 14:48:16 +010087 // The block currently visited.
88 HBasicBlock* current_block_ = nullptr;
89 // Errors encountered while checking the graph.
Vladimir Marko655e5852015-10-12 10:38:28 +010090 ArenaVector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +010091
92 private:
Roland Levillain75be2832014-10-17 17:02:00 +010093 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -070094 const char* const dump_prefix_;
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000095 ArenaBitVector seen_ids_;
Roland Levillain75be2832014-10-17 17:02:00 +010096
Roland Levillainccc07a92014-09-16 14:48:16 +010097 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
98};
99
100
101// An SSA graph visitor performing various checks.
102class SSAChecker : public GraphChecker {
103 public:
104 typedef GraphChecker super_type;
105
Vladimir Marko655e5852015-10-12 10:38:28 +0100106 explicit SSAChecker(HGraph* graph)
107 : GraphChecker(graph, "art::SSAChecker: ") {}
Roland Levillainccc07a92014-09-16 14:48:16 +0100108
Roland Levillain633021e2014-10-01 14:12:25 +0100109 // Check the whole graph (in reverse post-order).
Nicolas Geoffray31596742014-11-24 15:28:45 +0000110 void Run() OVERRIDE {
Roland Levillain633021e2014-10-01 14:12:25 +0100111 // VisitReversePostOrder is used instead of VisitInsertionOrder,
112 // as the latter might visit dead blocks removed by the dominator
113 // computation.
114 VisitReversePostOrder();
115 }
116
Roland Levillainccc07a92014-09-16 14:48:16 +0100117 // Perform SSA form checks on `block`.
Nicolas Geoffray31596742014-11-24 15:28:45 +0000118 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillain6b879dd2014-09-22 17:13:44 +0100119 // Loop-related checks from block `loop_header`.
120 void CheckLoop(HBasicBlock* loop_header);
Roland Levillainccc07a92014-09-16 14:48:16 +0100121
Roland Levillain6b879dd2014-09-22 17:13:44 +0100122 // Perform SSA form checks on instructions.
Nicolas Geoffray31596742014-11-24 15:28:45 +0000123 void VisitInstruction(HInstruction* instruction) OVERRIDE;
124 void VisitPhi(HPhi* phi) OVERRIDE;
125 void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE;
126 void VisitCondition(HCondition* op) OVERRIDE;
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000127 void VisitIf(HIf* instruction) OVERRIDE;
Mark Mendellfe57faa2015-09-18 09:26:15 -0400128 void VisitPackedSwitch(HPackedSwitch* instruction) OVERRIDE;
David Brazdil13b47182015-04-15 16:29:32 +0100129 void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000130 void VisitConstant(HConstant* instruction) OVERRIDE;
David Brazdilf5552582015-12-27 13:36:12 +0000131 void VisitBoundType(HBoundType* instruction) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +0100132
David Brazdil13b47182015-04-15 16:29:32 +0100133 void HandleBooleanInput(HInstruction* instruction, size_t input_index);
134
Roland Levillainccc07a92014-09-16 14:48:16 +0100135 private:
136 DISALLOW_COPY_AND_ASSIGN(SSAChecker);
137};
138
139} // namespace art
140
141#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_