| 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> |
| Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 21 | #include <string_view> |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 24 | #include "base/indenter.h" |
| David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 25 | #include "base/malloc_arena_pool.h" |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 26 | #include "base/scoped_arena_allocator.h" |
| Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 27 | #include "builder.h" |
| David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 28 | #include "common_compiler_test.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/code_item_accessors-inl.h" |
| 30 | #include "dex/dex_file.h" |
| 31 | #include "dex/dex_instruction.h" |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 32 | #include "dex/standard_dex_file.h" |
| Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 33 | #include "driver/dex_compilation_unit.h" |
| Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 34 | #include "graph_checker.h" |
| Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 35 | #include "gtest/gtest.h" |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 36 | #include "handle_scope-inl.h" |
| Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 37 | #include "handle_scope.h" |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 38 | #include "mirror/class_loader.h" |
| 39 | #include "mirror/dex_cache.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 40 | #include "nodes.h" |
| David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 41 | #include "scoped_thread_state_change.h" |
| 42 | #include "ssa_builder.h" |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 43 | #include "ssa_liveness_analysis.h" |
| 44 | |
| Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 45 | namespace art { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 46 | |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 47 | #define NUM_INSTRUCTIONS(...) \ |
| 48 | (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t)) |
| 49 | |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 50 | #define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \ |
| 51 | { 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] | 52 | |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 53 | #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 Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 60 | |
| David Srbecky | 883c134 | 2020-05-11 23:30:29 +0000 | [diff] [blame] | 61 | inline 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 Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 66 | LiveInterval* interval = |
| 67 | LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by); |
| Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 68 | if (defined_by != nullptr) { |
| 69 | defined_by->SetLiveInterval(interval); |
| 70 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 71 | 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 Srbecky | 883c134 | 2020-05-11 23:30:29 +0000 | [diff] [blame] | 78 | inline void RemoveSuspendChecks(HGraph* graph) { |
| Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 79 | for (HBasicBlock* block : graph->GetBlocks()) { |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 80 | if (block != nullptr) { |
| Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 81 | if (block->GetLoopInformation() != nullptr) { |
| 82 | block->GetLoopInformation()->SetSuspendCheck(nullptr); |
| 83 | } |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 84 | 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 Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 94 | class ArenaPoolAndAllocator { |
| 95 | public: |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 96 | ArenaPoolAndAllocator() |
| 97 | : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 98 | |
| 99 | ArenaAllocator* GetAllocator() { return &allocator_; } |
| 100 | ArenaStack* GetArenaStack() { return &arena_stack_; } |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 101 | ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 102 | |
| 103 | private: |
| David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 104 | MallocArenaPool pool_; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 105 | ArenaAllocator allocator_; |
| 106 | ArenaStack arena_stack_; |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 107 | ScopedArenaAllocator scoped_allocator_; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 108 | }; |
| 109 | |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 110 | // Have a separate helper so the OptimizingCFITest can inherit it without causing |
| 111 | // multiple inheritance errors from having two gtest as a parent twice. |
| 112 | class OptimizingUnitTestHelper { |
| 113 | public: |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 114 | OptimizingUnitTestHelper() |
| 115 | : pool_and_allocator_(new ArenaPoolAndAllocator()), |
| 116 | graph_(nullptr), |
| 117 | entry_block_(nullptr), |
| 118 | return_block_(nullptr), |
| 119 | exit_block_(nullptr) { } |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 120 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 121 | ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); } |
| 122 | ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); } |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 123 | ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 124 | |
| 125 | void ResetPoolAndAllocator() { |
| 126 | pool_and_allocator_.reset(new ArenaPoolAndAllocator()); |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 127 | } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 128 | |
| Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 129 | HGraph* CreateGraph(VariableSizedHandleScope* handles = nullptr) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 130 | 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 Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 139 | dex_data, |
| 140 | sizeof(StandardDexFile::Header), |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 141 | "no_location", |
| 142 | /*location_checksum*/ 0, |
| David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 143 | /*oat_dex_file*/ nullptr, |
| 144 | /*container*/ nullptr)); |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 145 | |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 146 | graph_ = new (allocator) HGraph( |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 147 | allocator, |
| 148 | pool_and_allocator_->GetArenaStack(), |
| Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 149 | handles, |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 150 | *dex_files_.back(), |
| 151 | /*method_idx*/-1, |
| 152 | kRuntimeISA); |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 153 | return graph_; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Create a control-flow graph from Dex instructions. |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 157 | HGraph* CreateCFG(const std::vector<uint16_t>& data, |
| Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 158 | DataType::Type return_type = DataType::Type::kInt32, |
| 159 | VariableSizedHandleScope* handles = nullptr) { |
| 160 | HGraph* graph = CreateGraph(handles); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 161 | |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 162 | // 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 167 | const dex::CodeItem* code_item = reinterpret_cast<const dex::CodeItem*>(aligned_data); |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 168 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 169 | { |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 170 | const DexCompilationUnit* dex_compilation_unit = |
| 171 | new (graph->GetAllocator()) DexCompilationUnit( |
| Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 172 | /* class_loader= */ Handle<mirror::ClassLoader>(), // Invalid handle. |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 173 | /* class_linker= */ nullptr, |
| Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 174 | graph->GetDexFile(), |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 175 | code_item, |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 176 | /* class_def_index= */ DexFile::kDexNoIndex16, |
| 177 | /* method_idx= */ dex::kDexNoIndex, |
| 178 | /* access_flags= */ 0u, |
| 179 | /* verified_method= */ nullptr, |
| Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 180 | /* dex_cache= */ Handle<mirror::DexCache>()); // Invalid handle. |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 181 | CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item, /*dex_method_idx*/ 0u); |
| Vladimir Marko | 02ca05a | 2020-05-12 13:58:51 +0100 | [diff] [blame] | 182 | HGraphBuilder builder(graph, dex_compilation_unit, accessor, return_type); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 183 | bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); |
| 184 | return graph_built ? graph : nullptr; |
| 185 | } |
| 186 | } |
| 187 | |
| Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 188 | void InitGraph(VariableSizedHandleScope* handles = nullptr) { |
| 189 | CreateGraph(handles); |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 190 | 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 Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 215 | // Run GraphChecker with all checks. |
| 216 | // |
| 217 | // Return: the status whether the run is successful. |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 218 | bool CheckGraph(HGraph* graph, std::ostream& oss = std::cerr) { |
| 219 | return CheckGraph(graph, /*check_ref_type_info=*/true, oss); |
| Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 222 | bool CheckGraph(std::ostream& oss = std::cerr) { |
| 223 | return CheckGraph(graph_, oss); |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 226 | // Run GraphChecker with all checks except reference type information checks. |
| 227 | // |
| 228 | // Return: the status whether the run is successful. |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 229 | bool CheckGraphSkipRefTypeInfoChecks(HGraph* graph, std::ostream& oss = std::cerr) { |
| 230 | return CheckGraph(graph, /*check_ref_type_info=*/false, oss); |
| Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 233 | bool CheckGraphSkipRefTypeInfoChecks(std::ostream& oss = std::cerr) { |
| 234 | return CheckGraphSkipRefTypeInfoChecks(graph_, oss); |
| Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 235 | } |
| 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 Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 251 | 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 Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 293 | protected: |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 294 | bool CheckGraph(HGraph* graph, bool check_ref_type_info, std::ostream& oss) { |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 295 | GraphChecker checker(graph); |
| 296 | checker.SetRefTypeInfoCheckEnabled(check_ref_type_info); |
| 297 | checker.Run(); |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 298 | checker.Dump(oss); |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 299 | return checker.IsValid(); |
| 300 | } |
| 301 | |
| 302 | std::vector<std::unique_ptr<const StandardDexFile>> dex_files_; |
| 303 | std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_; |
| Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 304 | |
| Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 305 | HGraph* graph_; |
| Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 306 | HBasicBlock* entry_block_; |
| 307 | HBasicBlock* return_block_; |
| 308 | HBasicBlock* exit_block_; |
| 309 | |
| Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 310 | std::vector<HInstruction*> parameters_; |
| Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
| Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 313 | class OptimizingUnitTest : public CommonArtTest, public OptimizingUnitTestHelper {}; |
| Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 314 | |
| Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 315 | // Naive string diff data type. |
| 316 | typedef 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 Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 320 | static const std::string removed = ""; // NOLINT [runtime/string] [4] |
| Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 321 | |
| 322 | // Naive patch command: apply a diff to a string. |
| 323 | inline 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 Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 327 | DCHECK_NE(pos, std::string::npos) |
| 328 | << "Could not find: \"" << p.first << "\" in \"" << result << "\""; |
| Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 329 | result.replace(pos, p.first.size(), p.second); |
| 330 | } |
| 331 | return result; |
| 332 | } |
| 333 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 334 | // Returns if the instruction is removed from the graph. |
| 335 | inline bool IsRemoved(HInstruction* instruction) { |
| 336 | return instruction->GetBlock() == nullptr; |
| 337 | } |
| 338 | |
| Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 339 | class 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 Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 364 | graph_->ClearReachabilityInformation(); |
| Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 365 | graph_->ComputeDominanceInformation(); |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 366 | graph_->ComputeReachabilityInformation(); |
| Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 367 | for (auto [name, blk] : name_to_block_) { |
| 368 | block_to_name_.Put(blk, name); |
| 369 | } |
| 370 | } |
| 371 | |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 372 | bool HasBlock(const HBasicBlock* blk) const { |
| Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 373 | return block_to_name_.find(blk) != block_to_name_.end(); |
| 374 | } |
| 375 | |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 376 | std::string_view GetName(const HBasicBlock* blk) const { |
| Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 377 | return block_to_name_.Get(blk); |
| 378 | } |
| 379 | |
| Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 380 | HBasicBlock* Get(const std::string_view& sv) const { |
| Alex Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 381 | 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 Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 389 | 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 Light | 9dec90a | 2020-09-14 17:58:28 -0700 | [diff] [blame] | 407 | 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 Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 413 | inline std::ostream& operator<<(std::ostream& oss, const AdjacencyListGraph& alg) { |
| 414 | return alg.Dump(oss); |
| 415 | } |
| 416 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 417 | } // namespace art |
| 418 | |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 419 | #endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ |