Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1 | /* |
| 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 Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <vector> |
| 22 | |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 23 | #include "base/malloc_arena_pool.h" |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 24 | #include "base/scoped_arena_allocator.h" |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 25 | #include "builder.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 26 | #include "common_compiler_test.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 27 | #include "dex/code_item_accessors-inl.h" |
| 28 | #include "dex/dex_file.h" |
| 29 | #include "dex/dex_instruction.h" |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 30 | #include "dex/standard_dex_file.h" |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 31 | #include "driver/dex_compilation_unit.h" |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 32 | #include "graph_checker.h" |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 33 | #include "handle_scope-inl.h" |
| 34 | #include "mirror/class_loader.h" |
| 35 | #include "mirror/dex_cache.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "nodes.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 37 | #include "scoped_thread_state_change.h" |
| 38 | #include "ssa_builder.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 39 | #include "ssa_liveness_analysis.h" |
| 40 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 41 | #include "gtest/gtest.h" |
| 42 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 43 | namespace art { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 44 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 45 | #define NUM_INSTRUCTIONS(...) \ |
| 46 | (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t)) |
| 47 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 48 | #define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \ |
| 49 | { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 50 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 51 | #define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__) |
| 52 | #define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__) |
| 53 | #define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__) |
| 54 | #define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__) |
| 55 | #define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__) |
| 56 | #define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__) |
| 57 | #define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__) |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 58 | |
David Srbecky | 883c134 | 2020-05-11 23:30:29 +0000 | [diff] [blame] | 59 | inline LiveInterval* BuildInterval(const size_t ranges[][2], |
| 60 | size_t number_of_ranges, |
| 61 | ScopedArenaAllocator* allocator, |
| 62 | int reg = -1, |
| 63 | HInstruction* defined_by = nullptr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 64 | LiveInterval* interval = |
| 65 | LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 66 | if (defined_by != nullptr) { |
| 67 | defined_by->SetLiveInterval(interval); |
| 68 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 69 | for (size_t i = number_of_ranges; i > 0; --i) { |
| 70 | interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]); |
| 71 | } |
| 72 | interval->SetRegister(reg); |
| 73 | return interval; |
| 74 | } |
| 75 | |
David Srbecky | 883c134 | 2020-05-11 23:30:29 +0000 | [diff] [blame] | 76 | inline void RemoveSuspendChecks(HGraph* graph) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 77 | for (HBasicBlock* block : graph->GetBlocks()) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 78 | if (block != nullptr) { |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 79 | if (block->GetLoopInformation() != nullptr) { |
| 80 | block->GetLoopInformation()->SetSuspendCheck(nullptr); |
| 81 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 82 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 83 | HInstruction* current = it.Current(); |
| 84 | if (current->IsSuspendCheck()) { |
| 85 | current->GetBlock()->RemoveInstruction(current); |
| 86 | } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 92 | class ArenaPoolAndAllocator { |
| 93 | public: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 94 | ArenaPoolAndAllocator() |
| 95 | : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 96 | |
| 97 | ArenaAllocator* GetAllocator() { return &allocator_; } |
| 98 | ArenaStack* GetArenaStack() { return &arena_stack_; } |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 99 | ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 100 | |
| 101 | private: |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 102 | MallocArenaPool pool_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 103 | ArenaAllocator allocator_; |
| 104 | ArenaStack arena_stack_; |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 105 | ScopedArenaAllocator scoped_allocator_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 108 | // Have a separate helper so the OptimizingCFITest can inherit it without causing |
| 109 | // multiple inheritance errors from having two gtest as a parent twice. |
| 110 | class OptimizingUnitTestHelper { |
| 111 | public: |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 112 | OptimizingUnitTestHelper() |
| 113 | : pool_and_allocator_(new ArenaPoolAndAllocator()), |
| 114 | graph_(nullptr), |
| 115 | entry_block_(nullptr), |
| 116 | return_block_(nullptr), |
| 117 | exit_block_(nullptr) { } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 118 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 119 | ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); } |
| 120 | ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); } |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 121 | ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 122 | |
| 123 | void ResetPoolAndAllocator() { |
| 124 | pool_and_allocator_.reset(new ArenaPoolAndAllocator()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 125 | } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 126 | |
Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 127 | HGraph* CreateGraph(VariableSizedHandleScope* handles = nullptr) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 128 | ArenaAllocator* const allocator = pool_and_allocator_->GetAllocator(); |
| 129 | |
| 130 | // Reserve a big array of 0s so the dex file constructor can offsets from the header. |
| 131 | static constexpr size_t kDexDataSize = 4 * KB; |
| 132 | const uint8_t* dex_data = reinterpret_cast<uint8_t*>(allocator->Alloc(kDexDataSize)); |
| 133 | |
| 134 | // Create the dex file based on the fake data. Call the constructor so that we can use virtual |
| 135 | // functions. Don't use the arena for the StandardDexFile otherwise the dex location leaks. |
| 136 | dex_files_.emplace_back(new StandardDexFile( |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 137 | dex_data, |
| 138 | sizeof(StandardDexFile::Header), |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 139 | "no_location", |
| 140 | /*location_checksum*/ 0, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 141 | /*oat_dex_file*/ nullptr, |
| 142 | /*container*/ nullptr)); |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 143 | |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 144 | graph_ = new (allocator) HGraph( |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 145 | allocator, |
| 146 | pool_and_allocator_->GetArenaStack(), |
Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 147 | handles, |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 148 | *dex_files_.back(), |
| 149 | /*method_idx*/-1, |
| 150 | kRuntimeISA); |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 151 | return graph_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Create a control-flow graph from Dex instructions. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 155 | HGraph* CreateCFG(const std::vector<uint16_t>& data, |
Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 156 | DataType::Type return_type = DataType::Type::kInt32, |
| 157 | VariableSizedHandleScope* handles = nullptr) { |
| 158 | HGraph* graph = CreateGraph(handles); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 159 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 160 | // The code item data might not aligned to 4 bytes, copy it to ensure that. |
| 161 | const size_t code_item_size = data.size() * sizeof(data.front()); |
| 162 | void* aligned_data = GetAllocator()->Alloc(code_item_size); |
| 163 | memcpy(aligned_data, &data[0], code_item_size); |
| 164 | CHECK_ALIGNED(aligned_data, StandardDexFile::CodeItem::kAlignment); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 165 | const dex::CodeItem* code_item = reinterpret_cast<const dex::CodeItem*>(aligned_data); |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 166 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 167 | { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 168 | const DexCompilationUnit* dex_compilation_unit = |
| 169 | new (graph->GetAllocator()) DexCompilationUnit( |
Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 170 | /* class_loader= */ Handle<mirror::ClassLoader>(), // Invalid handle. |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 171 | /* class_linker= */ nullptr, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 172 | graph->GetDexFile(), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 173 | code_item, |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 174 | /* class_def_index= */ DexFile::kDexNoIndex16, |
| 175 | /* method_idx= */ dex::kDexNoIndex, |
| 176 | /* access_flags= */ 0u, |
| 177 | /* verified_method= */ nullptr, |
Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 178 | /* dex_cache= */ Handle<mirror::DexCache>()); // Invalid handle. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 179 | CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item, /*dex_method_idx*/ 0u); |
Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 180 | HGraphBuilder builder(graph, dex_compilation_unit, accessor, return_type); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 181 | bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); |
| 182 | return graph_built ? graph : nullptr; |
| 183 | } |
| 184 | } |
| 185 | |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 186 | void InitGraph() { |
| 187 | CreateGraph(); |
| 188 | entry_block_ = AddNewBlock(); |
| 189 | return_block_ = AddNewBlock(); |
| 190 | exit_block_ = AddNewBlock(); |
| 191 | |
| 192 | graph_->SetEntryBlock(entry_block_); |
| 193 | graph_->SetExitBlock(exit_block_); |
| 194 | |
| 195 | entry_block_->AddSuccessor(return_block_); |
| 196 | return_block_->AddSuccessor(exit_block_); |
| 197 | |
| 198 | return_block_->AddInstruction(new (GetAllocator()) HReturnVoid()); |
| 199 | exit_block_->AddInstruction(new (GetAllocator()) HExit()); |
| 200 | } |
| 201 | |
| 202 | void AddParameter(HInstruction* parameter) { |
| 203 | entry_block_->AddInstruction(parameter); |
| 204 | parameters_.push_back(parameter); |
| 205 | } |
| 206 | |
| 207 | HBasicBlock* AddNewBlock() { |
| 208 | HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph_); |
| 209 | graph_->AddBlock(block); |
| 210 | return block; |
| 211 | } |
| 212 | |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 213 | // Run GraphChecker with all checks. |
| 214 | // |
| 215 | // Return: the status whether the run is successful. |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 216 | bool CheckGraph(HGraph* graph, std::ostream& oss = std::cerr) { |
| 217 | return CheckGraph(graph, /*check_ref_type_info=*/true, oss); |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 220 | bool CheckGraph(std::ostream& oss = std::cerr) { |
| 221 | return CheckGraph(graph_, oss); |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 224 | // Run GraphChecker with all checks except reference type information checks. |
| 225 | // |
| 226 | // Return: the status whether the run is successful. |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 227 | bool CheckGraphSkipRefTypeInfoChecks(HGraph* graph, std::ostream& oss = std::cerr) { |
| 228 | return CheckGraph(graph, /*check_ref_type_info=*/false, oss); |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 231 | bool CheckGraphSkipRefTypeInfoChecks(std::ostream& oss = std::cerr) { |
| 232 | return CheckGraphSkipRefTypeInfoChecks(graph_, oss); |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | HEnvironment* ManuallyBuildEnvFor(HInstruction* instruction, |
| 236 | ArenaVector<HInstruction*>* current_locals) { |
| 237 | HEnvironment* environment = new (GetAllocator()) HEnvironment( |
| 238 | (GetAllocator()), |
| 239 | current_locals->size(), |
| 240 | graph_->GetArtMethod(), |
| 241 | instruction->GetDexPc(), |
| 242 | instruction); |
| 243 | |
| 244 | environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals)); |
| 245 | instruction->SetRawEnvironment(environment); |
| 246 | return environment; |
| 247 | } |
| 248 | |
| 249 | protected: |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 250 | bool CheckGraph(HGraph* graph, bool check_ref_type_info, std::ostream& oss) { |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 251 | GraphChecker checker(graph); |
| 252 | checker.SetRefTypeInfoCheckEnabled(check_ref_type_info); |
| 253 | checker.Run(); |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 254 | checker.Dump(oss); |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 255 | return checker.IsValid(); |
| 256 | } |
| 257 | |
| 258 | std::vector<std::unique_ptr<const StandardDexFile>> dex_files_; |
| 259 | std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_; |
Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 260 | |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 261 | HGraph* graph_; |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 262 | HBasicBlock* entry_block_; |
| 263 | HBasicBlock* return_block_; |
| 264 | HBasicBlock* exit_block_; |
| 265 | |
Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 266 | std::vector<HInstruction*> parameters_; |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 267 | }; |
| 268 | |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 269 | class OptimizingUnitTest : public CommonArtTest, public OptimizingUnitTestHelper {}; |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 270 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 271 | // Naive string diff data type. |
| 272 | typedef std::list<std::pair<std::string, std::string>> diff_t; |
| 273 | |
| 274 | // An alias for the empty string used to make it clear that a line is |
| 275 | // removed in a diff. |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 276 | static const std::string removed = ""; // NOLINT [runtime/string] [4] |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 277 | |
| 278 | // Naive patch command: apply a diff to a string. |
| 279 | inline std::string Patch(const std::string& original, const diff_t& diff) { |
| 280 | std::string result = original; |
| 281 | for (const auto& p : diff) { |
| 282 | std::string::size_type pos = result.find(p.first); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 283 | DCHECK_NE(pos, std::string::npos) |
| 284 | << "Could not find: \"" << p.first << "\" in \"" << result << "\""; |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 285 | result.replace(pos, p.first.size(), p.second); |
| 286 | } |
| 287 | return result; |
| 288 | } |
| 289 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 290 | // Returns if the instruction is removed from the graph. |
| 291 | inline bool IsRemoved(HInstruction* instruction) { |
| 292 | return instruction->GetBlock() == nullptr; |
| 293 | } |
| 294 | |
Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 295 | class AdjacencyListGraph { |
| 296 | public: |
| 297 | using Edge = std::pair<const std::string_view, const std::string_view>; |
| 298 | AdjacencyListGraph( |
| 299 | HGraph* graph, |
| 300 | ArenaAllocator* alloc, |
| 301 | const std::string_view entry_name, |
| 302 | const std::string_view exit_name, |
| 303 | const std::vector<Edge>& adj) : graph_(graph) { |
| 304 | auto create_block = [&]() { |
| 305 | HBasicBlock* blk = new (alloc) HBasicBlock(graph_); |
| 306 | graph_->AddBlock(blk); |
| 307 | return blk; |
| 308 | }; |
| 309 | HBasicBlock* entry = create_block(); |
| 310 | HBasicBlock* exit = create_block(); |
| 311 | graph_->SetEntryBlock(entry); |
| 312 | graph_->SetExitBlock(exit); |
| 313 | name_to_block_.Put(entry_name, entry); |
| 314 | name_to_block_.Put(exit_name, exit); |
| 315 | for (const auto& [src, dest] : adj) { |
| 316 | HBasicBlock* src_blk = name_to_block_.GetOrCreate(src, create_block); |
| 317 | HBasicBlock* dest_blk = name_to_block_.GetOrCreate(dest, create_block); |
| 318 | src_blk->AddSuccessor(dest_blk); |
| 319 | } |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 320 | graph_->ClearReachabilityInformation(); |
Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 321 | graph_->ComputeDominanceInformation(); |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 322 | graph_->ComputeReachabilityInformation(); |
Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 323 | for (auto [name, blk] : name_to_block_) { |
| 324 | block_to_name_.Put(blk, name); |
| 325 | } |
| 326 | } |
| 327 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 328 | bool HasBlock(const HBasicBlock* blk) const { |
Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 329 | return block_to_name_.find(blk) != block_to_name_.end(); |
| 330 | } |
| 331 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 332 | std::string_view GetName(const HBasicBlock* blk) const { |
Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 333 | return block_to_name_.Get(blk); |
| 334 | } |
| 335 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 336 | HBasicBlock* Get(const std::string_view& sv) const { |
Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 337 | return name_to_block_.Get(sv); |
| 338 | } |
| 339 | |
| 340 | AdjacencyListGraph(AdjacencyListGraph&&) = default; |
| 341 | AdjacencyListGraph(const AdjacencyListGraph&) = default; |
| 342 | AdjacencyListGraph& operator=(AdjacencyListGraph&&) = default; |
| 343 | AdjacencyListGraph& operator=(const AdjacencyListGraph&) = default; |
| 344 | |
| 345 | private: |
| 346 | HGraph* graph_; |
| 347 | SafeMap<const std::string_view, HBasicBlock*> name_to_block_; |
| 348 | SafeMap<const HBasicBlock*, const std::string_view> block_to_name_; |
| 349 | }; |
| 350 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 351 | } // namespace art |
| 352 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 353 | #endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ |