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