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 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 20 | #include "base/scoped_arena_allocator.h" |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 21 | #include "builder.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 22 | #include "common_compiler_test.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "dex/code_item_accessors-inl.h" |
| 24 | #include "dex/dex_file.h" |
| 25 | #include "dex/dex_instruction.h" |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 26 | #include "driver/dex_compilation_unit.h" |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
| 28 | #include "mirror/class_loader.h" |
| 29 | #include "mirror/dex_cache.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "nodes.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "ssa_builder.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 33 | #include "ssa_liveness_analysis.h" |
| 34 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 35 | #include "gtest/gtest.h" |
| 36 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 39 | #define NUM_INSTRUCTIONS(...) \ |
| 40 | (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t)) |
| 41 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 42 | #define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \ |
| 43 | { 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] | 44 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 45 | #define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__) |
| 46 | #define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__) |
| 47 | #define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__) |
| 48 | #define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__) |
| 49 | #define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__) |
| 50 | #define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__) |
| 51 | #define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__) |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 53 | LiveInterval* BuildInterval(const size_t ranges[][2], |
| 54 | size_t number_of_ranges, |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 55 | ScopedArenaAllocator* allocator, |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 56 | int reg = -1, |
| 57 | HInstruction* defined_by = nullptr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 58 | LiveInterval* interval = |
| 59 | LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 60 | if (defined_by != nullptr) { |
| 61 | defined_by->SetLiveInterval(interval); |
| 62 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 63 | for (size_t i = number_of_ranges; i > 0; --i) { |
| 64 | interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]); |
| 65 | } |
| 66 | interval->SetRegister(reg); |
| 67 | return interval; |
| 68 | } |
| 69 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 70 | void RemoveSuspendChecks(HGraph* graph) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 71 | for (HBasicBlock* block : graph->GetBlocks()) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 72 | if (block != nullptr) { |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 73 | if (block->GetLoopInformation() != nullptr) { |
| 74 | block->GetLoopInformation()->SetSuspendCheck(nullptr); |
| 75 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 76 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 77 | HInstruction* current = it.Current(); |
| 78 | if (current->IsSuspendCheck()) { |
| 79 | current->GetBlock()->RemoveInstruction(current); |
| 80 | } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 86 | class ArenaPoolAndAllocator { |
| 87 | public: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 88 | ArenaPoolAndAllocator() |
| 89 | : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 90 | |
| 91 | ArenaAllocator* GetAllocator() { return &allocator_; } |
| 92 | ArenaStack* GetArenaStack() { return &arena_stack_; } |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 93 | ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | ArenaPool pool_; |
| 97 | ArenaAllocator allocator_; |
| 98 | ArenaStack arena_stack_; |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 99 | ScopedArenaAllocator scoped_allocator_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | inline HGraph* CreateGraph(ArenaPoolAndAllocator* pool_and_allocator) { |
| 103 | return new (pool_and_allocator->GetAllocator()) HGraph( |
| 104 | pool_and_allocator->GetAllocator(), |
| 105 | pool_and_allocator->GetArenaStack(), |
| 106 | *reinterpret_cast<DexFile*>(pool_and_allocator->GetAllocator()->Alloc(sizeof(DexFile))), |
Igor Murashkin | 032cacd | 2017-04-06 14:40:08 -0700 | [diff] [blame] | 107 | /*method_idx*/-1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 108 | kRuntimeISA); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 111 | class OptimizingUnitTest : public CommonCompilerTest { |
| 112 | protected: |
| 113 | OptimizingUnitTest() : pool_and_allocator_(new ArenaPoolAndAllocator()) { } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 114 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 115 | ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); } |
| 116 | ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); } |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 117 | ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 118 | |
| 119 | void ResetPoolAndAllocator() { |
| 120 | pool_and_allocator_.reset(new ArenaPoolAndAllocator()); |
| 121 | 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] | 122 | } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 123 | |
| 124 | HGraph* CreateGraph() { |
| 125 | return art::CreateGraph(pool_and_allocator_.get()); |
| 126 | } |
| 127 | |
| 128 | // Create a control-flow graph from Dex instructions. |
| 129 | HGraph* CreateCFG(const uint16_t* data, DataType::Type return_type = DataType::Type::kInt32) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 130 | const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(data); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 131 | HGraph* graph = CreateGraph(); |
| 132 | |
| 133 | { |
| 134 | ScopedObjectAccess soa(Thread::Current()); |
| 135 | if (handles_ == nullptr) { |
| 136 | handles_.reset(new VariableSizedHandleScope(soa.Self())); |
| 137 | } |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 138 | const DexCompilationUnit* dex_compilation_unit = |
| 139 | new (graph->GetAllocator()) DexCompilationUnit( |
| 140 | handles_->NewHandle<mirror::ClassLoader>(nullptr), |
| 141 | /* class_linker */ nullptr, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 142 | graph->GetDexFile(), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 143 | code_item, |
| 144 | /* class_def_index */ DexFile::kDexNoIndex16, |
| 145 | /* method_idx */ dex::kDexNoIndex, |
| 146 | /* access_flags */ 0u, |
| 147 | /* verified_method */ nullptr, |
| 148 | handles_->NewHandle<mirror::DexCache>(nullptr)); |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 149 | CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 150 | HGraphBuilder builder(graph, dex_compilation_unit, accessor, handles_.get(), return_type); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 151 | bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); |
| 152 | return graph_built ? graph : nullptr; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_; |
| 158 | std::unique_ptr<VariableSizedHandleScope> handles_; |
| 159 | }; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 160 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 161 | // Naive string diff data type. |
| 162 | typedef std::list<std::pair<std::string, std::string>> diff_t; |
| 163 | |
| 164 | // An alias for the empty string used to make it clear that a line is |
| 165 | // removed in a diff. |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 166 | static const std::string removed = ""; // NOLINT [runtime/string] [4] |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 167 | |
| 168 | // Naive patch command: apply a diff to a string. |
| 169 | inline std::string Patch(const std::string& original, const diff_t& diff) { |
| 170 | std::string result = original; |
| 171 | for (const auto& p : diff) { |
| 172 | std::string::size_type pos = result.find(p.first); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 173 | DCHECK_NE(pos, std::string::npos) |
| 174 | << "Could not find: \"" << p.first << "\" in \"" << result << "\""; |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 175 | result.replace(pos, p.first.size(), p.second); |
| 176 | } |
| 177 | return result; |
| 178 | } |
| 179 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 180 | // Returns if the instruction is removed from the graph. |
| 181 | inline bool IsRemoved(HInstruction* instruction) { |
| 182 | return instruction->GetBlock() == nullptr; |
| 183 | } |
| 184 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 185 | } // namespace art |
| 186 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 187 | #endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ |