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