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 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 43 | namespace art { |
| 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 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 59 | LiveInterval* BuildInterval(const size_t ranges[][2], |
| 60 | size_t number_of_ranges, |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 61 | ScopedArenaAllocator* allocator, |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 76 | 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: |
| 112 | OptimizingUnitTestHelper() : pool_and_allocator_(new ArenaPoolAndAllocator()) { } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 113 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 114 | ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); } |
| 115 | ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); } |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 116 | ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 117 | |
| 118 | void ResetPoolAndAllocator() { |
| 119 | pool_and_allocator_.reset(new ArenaPoolAndAllocator()); |
| 120 | handles_.reset(); // When getting rid of the old HGraph, we can also reset handles_. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 121 | } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 122 | |
| 123 | HGraph* CreateGraph() { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 124 | ArenaAllocator* const allocator = pool_and_allocator_->GetAllocator(); |
| 125 | |
| 126 | // Reserve a big array of 0s so the dex file constructor can offsets from the header. |
| 127 | static constexpr size_t kDexDataSize = 4 * KB; |
| 128 | const uint8_t* dex_data = reinterpret_cast<uint8_t*>(allocator->Alloc(kDexDataSize)); |
| 129 | |
| 130 | // Create the dex file based on the fake data. Call the constructor so that we can use virtual |
| 131 | // functions. Don't use the arena for the StandardDexFile otherwise the dex location leaks. |
| 132 | dex_files_.emplace_back(new StandardDexFile( |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame^] | 133 | dex_data, |
| 134 | sizeof(StandardDexFile::Header), |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 135 | "no_location", |
| 136 | /*location_checksum*/ 0, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame^] | 137 | /*oat_dex_file*/ nullptr, |
| 138 | /*container*/ nullptr)); |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 139 | |
| 140 | return new (allocator) HGraph( |
| 141 | allocator, |
| 142 | pool_and_allocator_->GetArenaStack(), |
| 143 | *dex_files_.back(), |
| 144 | /*method_idx*/-1, |
| 145 | kRuntimeISA); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Create a control-flow graph from Dex instructions. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 149 | HGraph* CreateCFG(const std::vector<uint16_t>& data, |
| 150 | DataType::Type return_type = DataType::Type::kInt32) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 151 | HGraph* graph = CreateGraph(); |
| 152 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 153 | // The code item data might not aligned to 4 bytes, copy it to ensure that. |
| 154 | const size_t code_item_size = data.size() * sizeof(data.front()); |
| 155 | void* aligned_data = GetAllocator()->Alloc(code_item_size); |
| 156 | memcpy(aligned_data, &data[0], code_item_size); |
| 157 | CHECK_ALIGNED(aligned_data, StandardDexFile::CodeItem::kAlignment); |
| 158 | const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(aligned_data); |
| 159 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 160 | { |
| 161 | ScopedObjectAccess soa(Thread::Current()); |
| 162 | if (handles_ == nullptr) { |
| 163 | handles_.reset(new VariableSizedHandleScope(soa.Self())); |
| 164 | } |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 165 | const DexCompilationUnit* dex_compilation_unit = |
| 166 | new (graph->GetAllocator()) DexCompilationUnit( |
| 167 | handles_->NewHandle<mirror::ClassLoader>(nullptr), |
| 168 | /* class_linker */ nullptr, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 169 | graph->GetDexFile(), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 170 | code_item, |
| 171 | /* class_def_index */ DexFile::kDexNoIndex16, |
| 172 | /* method_idx */ dex::kDexNoIndex, |
| 173 | /* access_flags */ 0u, |
| 174 | /* verified_method */ nullptr, |
| 175 | handles_->NewHandle<mirror::DexCache>(nullptr)); |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 176 | CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item, /*dex_method_idx*/ 0u); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 177 | HGraphBuilder builder(graph, dex_compilation_unit, accessor, handles_.get(), return_type); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 178 | bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); |
| 179 | return graph_built ? graph : nullptr; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private: |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 184 | std::vector<std::unique_ptr<const StandardDexFile>> dex_files_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 185 | std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_; |
| 186 | std::unique_ptr<VariableSizedHandleScope> handles_; |
| 187 | }; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 188 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 189 | class OptimizingUnitTest : public CommonCompilerTest, public OptimizingUnitTestHelper {}; |
| 190 | |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 191 | // OptimizingUnitTest with some handy functions to ease the graph creation. |
| 192 | class ImprovedOptimizingUnitTest : public OptimizingUnitTest { |
| 193 | public: |
| 194 | ImprovedOptimizingUnitTest() : graph_(CreateGraph()), |
| 195 | entry_block_(nullptr), |
| 196 | return_block_(nullptr), |
| 197 | exit_block_(nullptr), |
| 198 | parameter_(nullptr) {} |
| 199 | |
| 200 | virtual ~ImprovedOptimizingUnitTest() {} |
| 201 | |
| 202 | void InitGraph() { |
| 203 | entry_block_ = new (GetAllocator()) HBasicBlock(graph_); |
| 204 | graph_->AddBlock(entry_block_); |
| 205 | graph_->SetEntryBlock(entry_block_); |
| 206 | |
| 207 | return_block_ = new (GetAllocator()) HBasicBlock(graph_); |
| 208 | graph_->AddBlock(return_block_); |
| 209 | |
| 210 | exit_block_ = new (GetAllocator()) HBasicBlock(graph_); |
| 211 | graph_->AddBlock(exit_block_); |
| 212 | graph_->SetExitBlock(exit_block_); |
| 213 | |
| 214 | entry_block_->AddSuccessor(return_block_); |
| 215 | return_block_->AddSuccessor(exit_block_); |
| 216 | |
| 217 | parameter_ = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 218 | dex::TypeIndex(0), |
| 219 | 0, |
| 220 | DataType::Type::kInt32); |
| 221 | entry_block_->AddInstruction(parameter_); |
| 222 | return_block_->AddInstruction(new (GetAllocator()) HReturnVoid()); |
| 223 | exit_block_->AddInstruction(new (GetAllocator()) HExit()); |
| 224 | } |
| 225 | |
| 226 | bool CheckGraph() { |
| 227 | GraphChecker checker(graph_); |
| 228 | checker.Run(); |
| 229 | if (!checker.IsValid()) { |
| 230 | for (const std::string& error : checker.GetErrors()) { |
| 231 | std::cout << error << std::endl; |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | HEnvironment* ManuallyBuildEnvFor(HInstruction* instruction, |
| 239 | ArenaVector<HInstruction*>* current_locals) { |
| 240 | HEnvironment* environment = new (GetAllocator()) HEnvironment( |
| 241 | (GetAllocator()), |
| 242 | current_locals->size(), |
| 243 | graph_->GetArtMethod(), |
| 244 | instruction->GetDexPc(), |
| 245 | instruction); |
| 246 | |
| 247 | environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals)); |
| 248 | instruction->SetRawEnvironment(environment); |
| 249 | return environment; |
| 250 | } |
| 251 | |
| 252 | protected: |
| 253 | HGraph* graph_; |
| 254 | |
| 255 | HBasicBlock* entry_block_; |
| 256 | HBasicBlock* return_block_; |
| 257 | HBasicBlock* exit_block_; |
| 258 | |
| 259 | HInstruction* parameter_; |
| 260 | }; |
| 261 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 262 | // Naive string diff data type. |
| 263 | typedef std::list<std::pair<std::string, std::string>> diff_t; |
| 264 | |
| 265 | // An alias for the empty string used to make it clear that a line is |
| 266 | // removed in a diff. |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 267 | static const std::string removed = ""; // NOLINT [runtime/string] [4] |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 268 | |
| 269 | // Naive patch command: apply a diff to a string. |
| 270 | inline std::string Patch(const std::string& original, const diff_t& diff) { |
| 271 | std::string result = original; |
| 272 | for (const auto& p : diff) { |
| 273 | std::string::size_type pos = result.find(p.first); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 274 | DCHECK_NE(pos, std::string::npos) |
| 275 | << "Could not find: \"" << p.first << "\" in \"" << result << "\""; |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 276 | result.replace(pos, p.first.size(), p.second); |
| 277 | } |
| 278 | return result; |
| 279 | } |
| 280 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 281 | // Returns if the instruction is removed from the graph. |
| 282 | inline bool IsRemoved(HInstruction* instruction) { |
| 283 | return instruction->GetBlock() == nullptr; |
| 284 | } |
| 285 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 286 | } // namespace art |
| 287 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 288 | #endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ |