blob: 9878917739e910db42184986ab953c547c76b4de [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"
Vladimír Marko434d9682022-11-04 14:04:17 +000025#include "base/macros.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070026#include "base/value_object.h"
Alex Lightdc281e72021-01-06 12:35:31 -080027#include "block_namer.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010028
Vladimír Marko434d9682022-11-04 14:04:17 +000029namespace art HIDDEN {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010030
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010031class CodeGenerator;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010032class DexCompilationUnit;
33class HGraph;
Alexandre Rameseb7b7392015-06-19 14:47:01 +010034class HInstruction;
35class SlowPathCode;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010036
37/**
David Brazdilee690a32014-12-01 17:04:16 +000038 * This class outputs the HGraph in the C1visualizer format.
39 * Note: Currently only works if the compiler is single threaded.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010040 */
Alexandre Rameseb7b7392015-06-19 14:47:01 +010041struct GeneratedCodeInterval {
42 size_t start;
43 size_t end;
44};
45
46struct SlowPathCodeInfo {
47 const SlowPathCode* slow_path;
48 GeneratedCodeInterval code_interval;
49};
50
51// This information is filled by the code generator. It will be used by the
52// graph visualizer to associate disassembly of the generated code with the
53// instructions and slow paths. We assume that the generated code follows the
54// following structure:
55// - frame entry
56// - instructions
57// - slow paths
58class DisassemblyInformation {
59 public:
60 explicit DisassemblyInformation(ArenaAllocator* allocator)
61 : frame_entry_interval_({0, 0}),
62 instruction_intervals_(std::less<const HInstruction*>(), allocator->Adapter()),
63 slow_path_intervals_(allocator->Adapter()) {}
64
65 void SetFrameEntryInterval(size_t start, size_t end) {
66 frame_entry_interval_ = {start, end};
67 }
68
69 void AddInstructionInterval(HInstruction* instr, size_t start, size_t end) {
70 instruction_intervals_.Put(instr, {start, end});
71 }
72
73 void AddSlowPathInterval(SlowPathCode* slow_path, size_t start, size_t end) {
74 slow_path_intervals_.push_back({slow_path, {start, end}});
75 }
76
77 GeneratedCodeInterval GetFrameEntryInterval() const {
78 return frame_entry_interval_;
79 }
80
81 GeneratedCodeInterval* GetFrameEntryInterval() {
82 return &frame_entry_interval_;
83 }
84
85 const ArenaSafeMap<const HInstruction*, GeneratedCodeInterval>& GetInstructionIntervals() const {
86 return instruction_intervals_;
87 }
88
89 ArenaSafeMap<const HInstruction*, GeneratedCodeInterval>* GetInstructionIntervals() {
90 return &instruction_intervals_;
91 }
92
93 const ArenaVector<SlowPathCodeInfo>& GetSlowPathIntervals() const { return slow_path_intervals_; }
94
95 ArenaVector<SlowPathCodeInfo>* GetSlowPathIntervals() { return &slow_path_intervals_; }
96
97 private:
98 GeneratedCodeInterval frame_entry_interval_;
99 ArenaSafeMap<const HInstruction*, GeneratedCodeInterval> instruction_intervals_;
100 ArenaVector<SlowPathCodeInfo> slow_path_intervals_;
101};
102
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100103class HGraphVisualizer : public ValueObject {
104 public:
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100105 HGraphVisualizer(std::ostream* output,
106 HGraph* graph,
Alex Lightdc281e72021-01-06 12:35:31 -0800107 const CodeGenerator* codegen,
108 std::optional<std::reference_wrapper<const BlockNamer>> namer = std::nullopt);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100109
David Brazdil62e074f2015-04-07 18:09:37 +0100110 void PrintHeader(const char* method_name) const;
David Brazdilffee3d32015-07-06 11:48:53 +0100111 void DumpGraph(const char* pass_name, bool is_after_pass, bool graph_in_bad_state) const;
Alex Lightdc281e72021-01-06 12:35:31 -0800112 void DumpGraphDebug() const;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100113 void DumpGraphWithDisassembly() const;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100114
Fabio Rinaldi52d53542020-02-10 17:28:06 +0000115 // C1visualizer file format does not support inserting arbitrary metadata into a cfg
116 // file. As a workaround a fake compilation block with the metadata in the name and the
117 // method attributes is used. Such empty blocks don't break the c1visualizer parser.
118 static std::string InsertMetaDataAsCompilationBlock(const std::string& meta_data);
119
Vladimir Markoc9fcfd02021-01-05 16:57:30 +0000120 static void DumpInstruction(std::ostream* output, HGraph* graph, HInstruction* instruction);
121
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100122 private:
Alex Lightdc281e72021-01-06 12:35:31 -0800123 class OptionalDefaultNamer final : public BlockNamer {
124 public:
125 explicit OptionalDefaultNamer(std::optional<std::reference_wrapper<const BlockNamer>> inner)
126 : namer_(inner) {}
127
128 std::ostream& PrintName(std::ostream& os, HBasicBlock* blk) const override;
129
130 private:
131 std::optional<std::reference_wrapper<const BlockNamer>> namer_;
132 };
133
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100134 std::ostream* const output_;
135 HGraph* const graph_;
Vladimir Markoc9fcfd02021-01-05 16:57:30 +0000136 const CodeGenerator* codegen_;
Alex Lightdc281e72021-01-06 12:35:31 -0800137 OptionalDefaultNamer namer_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100138
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100139 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizer);
140};
141
142} // namespace art
143
144#endif // ART_COMPILER_OPTIMIZING_GRAPH_VISUALIZER_H_