blob: 3060c80073a4450da7c7446245d2ff18dba16e36 [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 Markof6a35de2016-03-21 12:01:50 +000033 seen_ids_(graph->GetArena(),
34 graph->GetCurrentInstructionId(),
35 false,
Vladimir Marko947eb702016-03-25 15:31:35 +000036 kArenaAllocGraphChecker),
37 blocks_storage_(graph->GetArena()->Adapter(kArenaAllocGraphChecker)),
38 visited_storage_(graph->GetArena(), 0u, true, kArenaAllocGraphChecker) {}
Roland Levillainccc07a92014-09-16 14:48:16 +010039
David Brazdilbadd8262016-02-02 16:28:56 +000040 // Check the whole graph (in reverse post-order).
41 void Run() {
42 // VisitReversePostOrder is used instead of VisitInsertionOrder,
43 // as the latter might visit dead blocks removed by the dominator
44 // computation.
45 VisitReversePostOrder();
46 }
Roland Levillain633021e2014-10-01 14:12:25 +010047
Nicolas Geoffray31596742014-11-24 15:28:45 +000048 void VisitBasicBlock(HBasicBlock* block) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010049
Nicolas Geoffray31596742014-11-24 15:28:45 +000050 void VisitInstruction(HInstruction* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000051 void VisitPhi(HPhi* phi) OVERRIDE;
Roland Levillainccc07a92014-09-16 14:48:16 +010052
David Brazdilbadd8262016-02-02 16:28:56 +000053 void VisitBinaryOperation(HBinaryOperation* op) OVERRIDE;
54 void VisitBooleanNot(HBooleanNot* instruction) OVERRIDE;
55 void VisitBoundType(HBoundType* instruction) OVERRIDE;
Mark Mendell1152c922015-04-24 17:06:35 -040056 void VisitBoundsCheck(HBoundsCheck* check) OVERRIDE;
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +010057 void VisitCheckCast(HCheckCast* check) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000058 void VisitCondition(HCondition* op) OVERRIDE;
59 void VisitConstant(HConstant* instruction) OVERRIDE;
Nicolas Geoffray93a18c52016-04-22 13:16:14 +010060 void VisitDeoptimize(HDeoptimize* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000061 void VisitIf(HIf* instruction) OVERRIDE;
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +010062 void VisitInstanceOf(HInstanceOf* check) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000063 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE;
64 void VisitLoadException(HLoadException* load) OVERRIDE;
Roland Levillain937e6cd2016-03-22 11:54:37 +000065 void VisitNeg(HNeg* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000066 void VisitPackedSwitch(HPackedSwitch* instruction) OVERRIDE;
David Brazdilfc6a86a2015-06-26 10:33:45 +000067 void VisitReturn(HReturn* ret) OVERRIDE;
68 void VisitReturnVoid(HReturnVoid* ret) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000069 void VisitSelect(HSelect* instruction) OVERRIDE;
70 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE;
Roland Levillainf355c3f2016-03-30 19:09:03 +010071 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
David Brazdilbadd8262016-02-02 16:28:56 +000072
73 void HandleLoop(HBasicBlock* loop_header);
74 void HandleBooleanInput(HInstruction* instruction, size_t input_index);
David Brazdilfc6a86a2015-06-26 10:33:45 +000075
Roland Levillainccc07a92014-09-16 14:48:16 +010076 // Was the last visit of the graph valid?
77 bool IsValid() const {
Andreas Gampe91356c02014-11-07 10:34:36 -080078 return errors_.empty();
Roland Levillainccc07a92014-09-16 14:48:16 +010079 }
80
81 // Get the list of detected errors.
Vladimir Marko655e5852015-10-12 10:38:28 +010082 const ArenaVector<std::string>& GetErrors() const {
Roland Levillainccc07a92014-09-16 14:48:16 +010083 return errors_;
84 }
85
Roland Levillain75be2832014-10-17 17:02:00 +010086 // Print detected errors on output stream `os`.
Ian Rogersc7dd2952014-10-21 23:31:19 -070087 void Dump(std::ostream& os) const {
Andreas Gampe91356c02014-11-07 10:34:36 -080088 for (size_t i = 0, e = errors_.size(); i < e; ++i) {
89 os << dump_prefix_ << errors_[i] << std::endl;
Roland Levillain75be2832014-10-17 17:02:00 +010090 }
91 }
92
Roland Levillainccc07a92014-09-16 14:48:16 +010093 protected:
Roland Levillain5c4405e2015-01-21 11:39:58 +000094 // Report a new error.
95 void AddError(const std::string& error) {
96 errors_.push_back(error);
97 }
98
Roland Levillainccc07a92014-09-16 14:48:16 +010099 // The block currently visited.
100 HBasicBlock* current_block_ = nullptr;
101 // Errors encountered while checking the graph.
Vladimir Marko655e5852015-10-12 10:38:28 +0100102 ArenaVector<std::string> errors_;
Roland Levillainccc07a92014-09-16 14:48:16 +0100103
104 private:
Roland Levillain75be2832014-10-17 17:02:00 +0100105 // String displayed before dumped errors.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700106 const char* const dump_prefix_;
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000107 ArenaBitVector seen_ids_;
Roland Levillain75be2832014-10-17 17:02:00 +0100108
Vladimir Marko947eb702016-03-25 15:31:35 +0000109 // To reduce the total arena memory allocation, we reuse the same storage.
110 ArenaVector<HBasicBlock*> blocks_storage_;
111 ArenaBitVector visited_storage_;
112
Roland Levillainccc07a92014-09-16 14:48:16 +0100113 DISALLOW_COPY_AND_ASSIGN(GraphChecker);
114};
115
Roland Levillainccc07a92014-09-16 14:48:16 +0100116} // namespace art
117
118#endif // ART_COMPILER_OPTIMIZING_GRAPH_CHECKER_H_