blob: 3429c11cbdee8647c0c4c7e633dd0ea0c03c3eee [file] [log] [blame]
Nicolas Geoffrayf635e632014-05-14 09:43:38 +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_VISUALIZER_H_
18#define ART_COMPILER_OPTIMIZING_GRAPH_VISUALIZER_H_
19
Alex Lightdc281e72021-01-06 12:35:31 -080020#include <functional>
Ian Rogerscf7f1912014-10-22 22:06:39 -070021#include <ostream>
22
Alexandre Rameseb7b7392015-06-19 14:47:01 +010023#include "arch/instruction_set.h"
24#include "base/arena_containers.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070025#include "base/value_object.h"
Alex Lightdc281e72021-01-06 12:35:31 -080026#include "block_namer.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010027
Vladimir Marko0a516052019-10-14 13:00:44 +000028namespace art {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010029
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010030class CodeGenerator;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010031class DexCompilationUnit;
32class HGraph;
Alexandre Rameseb7b7392015-06-19 14:47:01 +010033class HInstruction;
34class SlowPathCode;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010035
36/**
David Brazdilee690a32014-12-01 17:04:16 +000037 * This class outputs the HGraph in the C1visualizer format.
38 * Note: Currently only works if the compiler is single threaded.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010039 */
Alexandre Rameseb7b7392015-06-19 14:47:01 +010040struct GeneratedCodeInterval {
41 size_t start;
42 size_t end;
43};
44
45struct SlowPathCodeInfo {
46 const SlowPathCode* slow_path;
47 GeneratedCodeInterval code_interval;
48};
49
50// This information is filled by the code generator. It will be used by the
51// graph visualizer to associate disassembly of the generated code with the
52// instructions and slow paths. We assume that the generated code follows the
53// following structure:
54// - frame entry
55// - instructions
56// - slow paths
57class DisassemblyInformation {
58 public:
59 explicit DisassemblyInformation(ArenaAllocator* allocator)
60 : frame_entry_interval_({0, 0}),
61 instruction_intervals_(std::less<const HInstruction*>(), allocator->Adapter()),
62 slow_path_intervals_(allocator->Adapter()) {}
63
64 void SetFrameEntryInterval(size_t start, size_t end) {
65 frame_entry_interval_ = {start, end};
66 }
67
68 void AddInstructionInterval(HInstruction* instr, size_t start, size_t end) {
69 instruction_intervals_.Put(instr, {start, end});
70 }
71
72 void AddSlowPathInterval(SlowPathCode* slow_path, size_t start, size_t end) {
73 slow_path_intervals_.push_back({slow_path, {start, end}});
74 }
75
76 GeneratedCodeInterval GetFrameEntryInterval() const {
77 return frame_entry_interval_;
78 }
79
80 GeneratedCodeInterval* GetFrameEntryInterval() {
81 return &frame_entry_interval_;
82 }
83
84 const ArenaSafeMap<const HInstruction*, GeneratedCodeInterval>& GetInstructionIntervals() const {
85 return instruction_intervals_;
86 }
87
88 ArenaSafeMap<const HInstruction*, GeneratedCodeInterval>* GetInstructionIntervals() {
89 return &instruction_intervals_;
90 }
91
92 const ArenaVector<SlowPathCodeInfo>& GetSlowPathIntervals() const { return slow_path_intervals_; }
93
94 ArenaVector<SlowPathCodeInfo>* GetSlowPathIntervals() { return &slow_path_intervals_; }
95
96 private:
97 GeneratedCodeInterval frame_entry_interval_;
98 ArenaSafeMap<const HInstruction*, GeneratedCodeInterval> instruction_intervals_;
99 ArenaVector<SlowPathCodeInfo> slow_path_intervals_;
100};
101
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100102class HGraphVisualizer : public ValueObject {
103 public:
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100104 HGraphVisualizer(std::ostream* output,
105 HGraph* graph,
Alex Lightdc281e72021-01-06 12:35:31 -0800106 const CodeGenerator* codegen,
107 std::optional<std::reference_wrapper<const BlockNamer>> namer = std::nullopt);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100108
David Brazdil62e074f2015-04-07 18:09:37 +0100109 void PrintHeader(const char* method_name) const;
David Brazdilffee3d32015-07-06 11:48:53 +0100110 void DumpGraph(const char* pass_name, bool is_after_pass, bool graph_in_bad_state) const;
Alex Lightdc281e72021-01-06 12:35:31 -0800111 void DumpGraphDebug() const;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100112 void DumpGraphWithDisassembly() const;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100113
Fabio Rinaldi52d53542020-02-10 17:28:06 +0000114 // C1visualizer file format does not support inserting arbitrary metadata into a cfg
115 // file. As a workaround a fake compilation block with the metadata in the name and the
116 // method attributes is used. Such empty blocks don't break the c1visualizer parser.
117 static std::string InsertMetaDataAsCompilationBlock(const std::string& meta_data);
118
Vladimir Markoc9fcfd02021-01-05 16:57:30 +0000119 static void DumpInstruction(std::ostream* output, HGraph* graph, HInstruction* instruction);
120
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100121 private:
Alex Lightdc281e72021-01-06 12:35:31 -0800122 class OptionalDefaultNamer final : public BlockNamer {
123 public:
124 explicit OptionalDefaultNamer(std::optional<std::reference_wrapper<const BlockNamer>> inner)
125 : namer_(inner) {}
126
127 std::ostream& PrintName(std::ostream& os, HBasicBlock* blk) const override;
128
129 private:
130 std::optional<std::reference_wrapper<const BlockNamer>> namer_;
131 };
132
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100133 std::ostream* const output_;
134 HGraph* const graph_;
Vladimir Markoc9fcfd02021-01-05 16:57:30 +0000135 const CodeGenerator* codegen_;
Alex Lightdc281e72021-01-06 12:35:31 -0800136 OptionalDefaultNamer namer_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100137
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100138 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizer);
139};
140
141} // namespace art
142
143#endif // ART_COMPILER_OPTIMIZING_GRAPH_VISUALIZER_H_