blob: cf97c4198371d40b827e4466c1bf652d439f3ea6 [file] [log] [blame]
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001/*
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_OPTIMIZING_UNIT_TEST_H_
18#define ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_
19
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080020#include <memory>
Alex Light3a73ffb2021-01-25 14:11:05 +000021#include <string_view>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080022#include <vector>
23
Alex Light3a73ffb2021-01-25 14:11:05 +000024#include "base/indenter.h"
David Sehr3215fff2018-04-03 17:10:12 -070025#include "base/malloc_arena_pool.h"
Vladimir Markoca6fff82017-10-03 14:49:14 +010026#include "base/scoped_arena_allocator.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010027#include "builder.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000028#include "common_compiler_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/code_item_accessors-inl.h"
30#include "dex/dex_file.h"
31#include "dex/dex_instruction.h"
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080032#include "dex/standard_dex_file.h"
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000033#include "driver/dex_compilation_unit.h"
Artem Serov15f95b12018-06-29 15:30:36 +010034#include "graph_checker.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000035#include "gtest/gtest.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010036#include "handle_scope-inl.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000037#include "handle_scope.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010038#include "mirror/class_loader.h"
39#include "mirror/dex_cache.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000041#include "scoped_thread_state_change.h"
42#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010043#include "ssa_liveness_analysis.h"
44
Vladimir Marko0a516052019-10-14 13:00:44 +000045namespace art {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000047#define NUM_INSTRUCTIONS(...) \
48 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
49
Roland Levillain55dcfb52014-10-24 18:09:09 +010050#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
51 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052
Roland Levillain55dcfb52014-10-24 18:09:09 +010053#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
54#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
55#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
56#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
57#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
58#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
59#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000060
David Srbecky883c1342020-05-11 23:30:29 +000061inline LiveInterval* BuildInterval(const size_t ranges[][2],
62 size_t number_of_ranges,
63 ScopedArenaAllocator* allocator,
64 int reg = -1,
65 HInstruction* defined_by = nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 LiveInterval* interval =
67 LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000068 if (defined_by != nullptr) {
69 defined_by->SetLiveInterval(interval);
70 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010071 for (size_t i = number_of_ranges; i > 0; --i) {
72 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
73 }
74 interval->SetRegister(reg);
75 return interval;
76}
77
David Srbecky883c1342020-05-11 23:30:29 +000078inline void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010079 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000080 if (block != nullptr) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010081 if (block->GetLoopInformation() != nullptr) {
82 block->GetLoopInformation()->SetSuspendCheck(nullptr);
83 }
David Brazdilbadd8262016-02-02 16:28:56 +000084 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
85 HInstruction* current = it.Current();
86 if (current->IsSuspendCheck()) {
87 current->GetBlock()->RemoveInstruction(current);
88 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000089 }
90 }
91 }
92}
93
Vladimir Markoca6fff82017-10-03 14:49:14 +010094class ArenaPoolAndAllocator {
95 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010096 ArenaPoolAndAllocator()
97 : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { }
Vladimir Markoca6fff82017-10-03 14:49:14 +010098
99 ArenaAllocator* GetAllocator() { return &allocator_; }
100 ArenaStack* GetArenaStack() { return &arena_stack_; }
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100101 ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100102
103 private:
David Sehr3215fff2018-04-03 17:10:12 -0700104 MallocArenaPool pool_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100105 ArenaAllocator allocator_;
106 ArenaStack arena_stack_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100107 ScopedArenaAllocator scoped_allocator_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100108};
109
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800110// Have a separate helper so the OptimizingCFITest can inherit it without causing
111// multiple inheritance errors from having two gtest as a parent twice.
112class OptimizingUnitTestHelper {
113 public:
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100114 OptimizingUnitTestHelper()
115 : pool_and_allocator_(new ArenaPoolAndAllocator()),
116 graph_(nullptr),
117 entry_block_(nullptr),
118 return_block_(nullptr),
119 exit_block_(nullptr) { }
David Brazdilbadd8262016-02-02 16:28:56 +0000120
Vladimir Markoca6fff82017-10-03 14:49:14 +0100121 ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); }
122 ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); }
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100123 ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100124
125 void ResetPoolAndAllocator() {
126 pool_and_allocator_.reset(new ArenaPoolAndAllocator());
David Brazdilbadd8262016-02-02 16:28:56 +0000127 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100128
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100129 HGraph* CreateGraph(VariableSizedHandleScope* handles = nullptr) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800130 ArenaAllocator* const allocator = pool_and_allocator_->GetAllocator();
131
132 // Reserve a big array of 0s so the dex file constructor can offsets from the header.
133 static constexpr size_t kDexDataSize = 4 * KB;
134 const uint8_t* dex_data = reinterpret_cast<uint8_t*>(allocator->Alloc(kDexDataSize));
135
136 // Create the dex file based on the fake data. Call the constructor so that we can use virtual
137 // functions. Don't use the arena for the StandardDexFile otherwise the dex location leaks.
138 dex_files_.emplace_back(new StandardDexFile(
David Sehr0b426772018-07-03 23:03:42 +0000139 dex_data,
140 sizeof(StandardDexFile::Header),
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800141 "no_location",
142 /*location_checksum*/ 0,
David Sehr0b426772018-07-03 23:03:42 +0000143 /*oat_dex_file*/ nullptr,
144 /*container*/ nullptr));
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800145
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100146 graph_ = new (allocator) HGraph(
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800147 allocator,
148 pool_and_allocator_->GetArenaStack(),
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100149 handles,
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800150 *dex_files_.back(),
151 /*method_idx*/-1,
152 kRuntimeISA);
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100153 return graph_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100154 }
155
156 // Create a control-flow graph from Dex instructions.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800157 HGraph* CreateCFG(const std::vector<uint16_t>& data,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100158 DataType::Type return_type = DataType::Type::kInt32,
159 VariableSizedHandleScope* handles = nullptr) {
160 HGraph* graph = CreateGraph(handles);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100161
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800162 // The code item data might not aligned to 4 bytes, copy it to ensure that.
163 const size_t code_item_size = data.size() * sizeof(data.front());
164 void* aligned_data = GetAllocator()->Alloc(code_item_size);
165 memcpy(aligned_data, &data[0], code_item_size);
166 CHECK_ALIGNED(aligned_data, StandardDexFile::CodeItem::kAlignment);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800167 const dex::CodeItem* code_item = reinterpret_cast<const dex::CodeItem*>(aligned_data);
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800168
Vladimir Markoca6fff82017-10-03 14:49:14 +0100169 {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100170 const DexCompilationUnit* dex_compilation_unit =
171 new (graph->GetAllocator()) DexCompilationUnit(
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100172 /* class_loader= */ Handle<mirror::ClassLoader>(), // Invalid handle.
Andreas Gampe3db70682018-12-26 15:12:03 -0800173 /* class_linker= */ nullptr,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000174 graph->GetDexFile(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100175 code_item,
Andreas Gampe3db70682018-12-26 15:12:03 -0800176 /* class_def_index= */ DexFile::kDexNoIndex16,
177 /* method_idx= */ dex::kDexNoIndex,
178 /* access_flags= */ 0u,
179 /* verified_method= */ nullptr,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100180 /* dex_cache= */ Handle<mirror::DexCache>()); // Invalid handle.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800181 CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item, /*dex_method_idx*/ 0u);
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100182 HGraphBuilder builder(graph, dex_compilation_unit, accessor, return_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100183 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
184 return graph_built ? graph : nullptr;
185 }
186 }
187
Alex Light3a73ffb2021-01-25 14:11:05 +0000188 void InitGraph(VariableSizedHandleScope* handles = nullptr) {
189 CreateGraph(handles);
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100190 entry_block_ = AddNewBlock();
191 return_block_ = AddNewBlock();
192 exit_block_ = AddNewBlock();
193
194 graph_->SetEntryBlock(entry_block_);
195 graph_->SetExitBlock(exit_block_);
196
197 entry_block_->AddSuccessor(return_block_);
198 return_block_->AddSuccessor(exit_block_);
199
200 return_block_->AddInstruction(new (GetAllocator()) HReturnVoid());
201 exit_block_->AddInstruction(new (GetAllocator()) HExit());
202 }
203
204 void AddParameter(HInstruction* parameter) {
205 entry_block_->AddInstruction(parameter);
206 parameters_.push_back(parameter);
207 }
208
209 HBasicBlock* AddNewBlock() {
210 HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph_);
211 graph_->AddBlock(block);
212 return block;
213 }
214
Evgeny Astigeevich7ee34a12019-12-10 11:36:33 +0000215 // Run GraphChecker with all checks.
216 //
217 // Return: the status whether the run is successful.
Alex Light86fe9b82020-11-16 16:54:01 +0000218 bool CheckGraph(HGraph* graph, std::ostream& oss = std::cerr) {
219 return CheckGraph(graph, /*check_ref_type_info=*/true, oss);
Evgeny Astigeevich7ee34a12019-12-10 11:36:33 +0000220 }
221
Alex Light86fe9b82020-11-16 16:54:01 +0000222 bool CheckGraph(std::ostream& oss = std::cerr) {
223 return CheckGraph(graph_, oss);
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100224 }
225
Evgeny Astigeevich7ee34a12019-12-10 11:36:33 +0000226 // Run GraphChecker with all checks except reference type information checks.
227 //
228 // Return: the status whether the run is successful.
Alex Light86fe9b82020-11-16 16:54:01 +0000229 bool CheckGraphSkipRefTypeInfoChecks(HGraph* graph, std::ostream& oss = std::cerr) {
230 return CheckGraph(graph, /*check_ref_type_info=*/false, oss);
Evgeny Astigeevich7ee34a12019-12-10 11:36:33 +0000231 }
232
Alex Light86fe9b82020-11-16 16:54:01 +0000233 bool CheckGraphSkipRefTypeInfoChecks(std::ostream& oss = std::cerr) {
234 return CheckGraphSkipRefTypeInfoChecks(graph_, oss);
Artem Serov15f95b12018-06-29 15:30:36 +0100235 }
236
237 HEnvironment* ManuallyBuildEnvFor(HInstruction* instruction,
238 ArenaVector<HInstruction*>* current_locals) {
239 HEnvironment* environment = new (GetAllocator()) HEnvironment(
240 (GetAllocator()),
241 current_locals->size(),
242 graph_->GetArtMethod(),
243 instruction->GetDexPc(),
244 instruction);
245
246 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals));
247 instruction->SetRawEnvironment(environment);
248 return environment;
249 }
250
Alex Light3a73ffb2021-01-25 14:11:05 +0000251 void EnsurePredecessorOrder(HBasicBlock* target, std::initializer_list<HBasicBlock*> preds) {
252 // Make sure the given preds and block predecessors have the same blocks.
253 BitVector bv(preds.size(), false, Allocator::GetMallocAllocator());
254 auto preds_and_idx = ZipCount(MakeIterationRange(target->GetPredecessors()));
255 bool correct_preds = preds.size() == target->GetPredecessors().size() &&
256 std::all_of(preds.begin(), preds.end(), [&](HBasicBlock* pred) {
257 return std::any_of(preds_and_idx.begin(),
258 preds_and_idx.end(),
259 // Make sure every target predecessor is used only
260 // once.
261 [&](std::pair<HBasicBlock*, uint32_t> cur) {
262 if (cur.first == pred && !bv.IsBitSet(cur.second)) {
263 bv.SetBit(cur.second);
264 return true;
265 } else {
266 return false;
267 }
268 });
269 }) &&
270 bv.NumSetBits() == preds.size();
271 auto dump_list = [](auto it) {
272 std::ostringstream oss;
273 oss << "[";
274 bool first = true;
275 for (HBasicBlock* b : it) {
276 if (!first) {
277 oss << ", ";
278 }
279 first = false;
280 oss << b->GetBlockId();
281 }
282 oss << "]";
283 return oss.str();
284 };
285 ASSERT_TRUE(correct_preds) << "Predecessors of " << target->GetBlockId() << " are "
286 << dump_list(target->GetPredecessors()) << " not "
287 << dump_list(preds);
288 if (correct_preds) {
289 std::copy(preds.begin(), preds.end(), target->predecessors_.begin());
290 }
291 }
292
Artem Serov15f95b12018-06-29 15:30:36 +0100293 protected:
Alex Light86fe9b82020-11-16 16:54:01 +0000294 bool CheckGraph(HGraph* graph, bool check_ref_type_info, std::ostream& oss) {
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100295 GraphChecker checker(graph);
296 checker.SetRefTypeInfoCheckEnabled(check_ref_type_info);
297 checker.Run();
Alex Light86fe9b82020-11-16 16:54:01 +0000298 checker.Dump(oss);
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100299 return checker.IsValid();
300 }
301
302 std::vector<std::unique_ptr<const StandardDexFile>> dex_files_;
303 std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_;
Evgeny Astigeevich52506e22019-12-04 15:59:37 +0000304
Artem Serov15f95b12018-06-29 15:30:36 +0100305 HGraph* graph_;
Artem Serov15f95b12018-06-29 15:30:36 +0100306 HBasicBlock* entry_block_;
307 HBasicBlock* return_block_;
308 HBasicBlock* exit_block_;
309
Evgeny Astigeevich52506e22019-12-04 15:59:37 +0000310 std::vector<HInstruction*> parameters_;
Artem Serov15f95b12018-06-29 15:30:36 +0100311};
312
Vladimir Markof91fc122020-05-13 09:21:00 +0100313class OptimizingUnitTest : public CommonArtTest, public OptimizingUnitTestHelper {};
Vladimir Marko5d2311a2020-05-13 17:30:32 +0100314
Roland Levillain72bceff2014-09-15 18:29:00 +0100315// Naive string diff data type.
316typedef std::list<std::pair<std::string, std::string>> diff_t;
317
318// An alias for the empty string used to make it clear that a line is
319// removed in a diff.
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800320static const std::string removed = ""; // NOLINT [runtime/string] [4]
Roland Levillain72bceff2014-09-15 18:29:00 +0100321
322// Naive patch command: apply a diff to a string.
323inline std::string Patch(const std::string& original, const diff_t& diff) {
324 std::string result = original;
325 for (const auto& p : diff) {
326 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000327 DCHECK_NE(pos, std::string::npos)
328 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100329 result.replace(pos, p.first.size(), p.second);
330 }
331 return result;
332}
333
Mingyao Yangf384f882014-10-22 16:08:18 -0700334// Returns if the instruction is removed from the graph.
335inline bool IsRemoved(HInstruction* instruction) {
336 return instruction->GetBlock() == nullptr;
337}
338
Alex Light9dec90a2020-09-14 17:58:28 -0700339class AdjacencyListGraph {
340 public:
341 using Edge = std::pair<const std::string_view, const std::string_view>;
342 AdjacencyListGraph(
343 HGraph* graph,
344 ArenaAllocator* alloc,
345 const std::string_view entry_name,
346 const std::string_view exit_name,
347 const std::vector<Edge>& adj) : graph_(graph) {
348 auto create_block = [&]() {
349 HBasicBlock* blk = new (alloc) HBasicBlock(graph_);
350 graph_->AddBlock(blk);
351 return blk;
352 };
353 HBasicBlock* entry = create_block();
354 HBasicBlock* exit = create_block();
355 graph_->SetEntryBlock(entry);
356 graph_->SetExitBlock(exit);
357 name_to_block_.Put(entry_name, entry);
358 name_to_block_.Put(exit_name, exit);
359 for (const auto& [src, dest] : adj) {
360 HBasicBlock* src_blk = name_to_block_.GetOrCreate(src, create_block);
361 HBasicBlock* dest_blk = name_to_block_.GetOrCreate(dest, create_block);
362 src_blk->AddSuccessor(dest_blk);
363 }
Alex Light86fe9b82020-11-16 16:54:01 +0000364 graph_->ClearReachabilityInformation();
Alex Light9dec90a2020-09-14 17:58:28 -0700365 graph_->ComputeDominanceInformation();
Alex Light86fe9b82020-11-16 16:54:01 +0000366 graph_->ComputeReachabilityInformation();
Alex Light9dec90a2020-09-14 17:58:28 -0700367 for (auto [name, blk] : name_to_block_) {
368 block_to_name_.Put(blk, name);
369 }
370 }
371
Alex Light86fe9b82020-11-16 16:54:01 +0000372 bool HasBlock(const HBasicBlock* blk) const {
Alex Light9dec90a2020-09-14 17:58:28 -0700373 return block_to_name_.find(blk) != block_to_name_.end();
374 }
375
Alex Light86fe9b82020-11-16 16:54:01 +0000376 std::string_view GetName(const HBasicBlock* blk) const {
Alex Light9dec90a2020-09-14 17:58:28 -0700377 return block_to_name_.Get(blk);
378 }
379
Alex Light86fe9b82020-11-16 16:54:01 +0000380 HBasicBlock* Get(const std::string_view& sv) const {
Alex Light9dec90a2020-09-14 17:58:28 -0700381 return name_to_block_.Get(sv);
382 }
383
384 AdjacencyListGraph(AdjacencyListGraph&&) = default;
385 AdjacencyListGraph(const AdjacencyListGraph&) = default;
386 AdjacencyListGraph& operator=(AdjacencyListGraph&&) = default;
387 AdjacencyListGraph& operator=(const AdjacencyListGraph&) = default;
388
Alex Light3a73ffb2021-01-25 14:11:05 +0000389 std::ostream& Dump(std::ostream& os) const {
390 struct Namer : public BlockNamer {
391 public:
392 explicit Namer(const AdjacencyListGraph& alg) : BlockNamer(), alg_(alg) {}
393 std::ostream& PrintName(std::ostream& os, HBasicBlock* blk) const override {
394 if (alg_.HasBlock(blk)) {
395 return os << alg_.GetName(blk) << " (" << blk->GetBlockId() << ")";
396 } else {
397 return os << "<Unnamed B" << blk->GetBlockId() << ">";
398 }
399 }
400
401 const AdjacencyListGraph& alg_;
402 };
403 Namer namer(*this);
404 return graph_->Dump(os, namer);
405 }
406
Alex Light9dec90a2020-09-14 17:58:28 -0700407 private:
408 HGraph* graph_;
409 SafeMap<const std::string_view, HBasicBlock*> name_to_block_;
410 SafeMap<const HBasicBlock*, const std::string_view> block_to_name_;
411};
412
Alex Light3a73ffb2021-01-25 14:11:05 +0000413inline std::ostream& operator<<(std::ostream& oss, const AdjacencyListGraph& alg) {
414 return alg.Dump(oss);
415}
416
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100417} // namespace art
418
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000419#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_