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 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 20 | #include "nodes.h" |
| 21 | #include "builder.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 22 | #include "common_compiler_test.h" |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 23 | #include "dex_file.h" |
| 24 | #include "dex_instruction.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "handle_scope.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
| 27 | #include "ssa_builder.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 28 | #include "ssa_liveness_analysis.h" |
| 29 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 30 | #include "gtest/gtest.h" |
| 31 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 32 | namespace art { |
| 33 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 34 | #define NUM_INSTRUCTIONS(...) \ |
| 35 | (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t)) |
| 36 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 37 | #define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \ |
| 38 | { 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] | 39 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 40 | #define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__) |
| 41 | #define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__) |
| 42 | #define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__) |
| 43 | #define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__) |
| 44 | #define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__) |
| 45 | #define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__) |
| 46 | #define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__) |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 47 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 48 | LiveInterval* BuildInterval(const size_t ranges[][2], |
| 49 | size_t number_of_ranges, |
| 50 | ArenaAllocator* allocator, |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 51 | int reg = -1, |
| 52 | HInstruction* defined_by = nullptr) { |
| 53 | LiveInterval* interval = LiveInterval::MakeInterval(allocator, Primitive::kPrimInt, defined_by); |
| 54 | if (defined_by != nullptr) { |
| 55 | defined_by->SetLiveInterval(interval); |
| 56 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 57 | for (size_t i = number_of_ranges; i > 0; --i) { |
| 58 | interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]); |
| 59 | } |
| 60 | interval->SetRegister(reg); |
| 61 | return interval; |
| 62 | } |
| 63 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 64 | void RemoveSuspendChecks(HGraph* graph) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 65 | for (HBasicBlock* block : graph->GetBlocks()) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 66 | if (block != nullptr) { |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 67 | if (block->GetLoopInformation() != nullptr) { |
| 68 | block->GetLoopInformation()->SetSuspendCheck(nullptr); |
| 69 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 70 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 71 | HInstruction* current = it.Current(); |
| 72 | if (current->IsSuspendCheck()) { |
| 73 | current->GetBlock()->RemoveInstruction(current); |
| 74 | } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 80 | inline HGraph* CreateGraph(ArenaAllocator* allocator) { |
| 81 | return new (allocator) HGraph( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 82 | allocator, *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))), -1, false, |
| 83 | kRuntimeISA); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 86 | // Create a control-flow graph from Dex instructions. |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 87 | inline HGraph* CreateCFG(ArenaAllocator* allocator, |
| 88 | const uint16_t* data, |
| 89 | Primitive::Type return_type = Primitive::kPrimInt) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 90 | const DexFile::CodeItem* item = |
| 91 | reinterpret_cast<const DexFile::CodeItem*>(data); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 92 | HGraph* graph = CreateGraph(allocator); |
| 93 | |
| 94 | { |
| 95 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 96 | VariableSizedHandleScope handles(soa.Self()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 97 | HGraphBuilder builder(graph, *item, &handles, return_type); |
| 98 | bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 99 | return graph_built ? graph : nullptr; |
| 100 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 103 | // Naive string diff data type. |
| 104 | typedef std::list<std::pair<std::string, std::string>> diff_t; |
| 105 | |
| 106 | // An alias for the empty string used to make it clear that a line is |
| 107 | // removed in a diff. |
| 108 | static const std::string removed = ""; |
| 109 | |
| 110 | // Naive patch command: apply a diff to a string. |
| 111 | inline std::string Patch(const std::string& original, const diff_t& diff) { |
| 112 | std::string result = original; |
| 113 | for (const auto& p : diff) { |
| 114 | std::string::size_type pos = result.find(p.first); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 115 | DCHECK_NE(pos, std::string::npos) |
| 116 | << "Could not find: \"" << p.first << "\" in \"" << result << "\""; |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 117 | result.replace(pos, p.first.size(), p.second); |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 122 | // Returns if the instruction is removed from the graph. |
| 123 | inline bool IsRemoved(HInstruction* instruction) { |
| 124 | return instruction->GetBlock() == nullptr; |
| 125 | } |
| 126 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 127 | } // namespace art |
| 128 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 129 | #endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ |