Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [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 | #include <fstream> |
| 18 | |
| 19 | #include "base/stringprintf.h" |
| 20 | #include "builder.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 21 | #include "code_generator.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 22 | #include "code_generator_x86.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 23 | #include "dex_file.h" |
| 24 | #include "dex_instruction.h" |
| 25 | #include "graph_visualizer.h" |
| 26 | #include "nodes.h" |
| 27 | #include "optimizing_unit_test.h" |
| 28 | #include "pretty_printer.h" |
| 29 | #include "ssa_builder.h" |
| 30 | #include "ssa_liveness_analysis.h" |
| 31 | #include "utils/arena_allocator.h" |
| 32 | |
| 33 | #include "gtest/gtest.h" |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | static void TestCode(const uint16_t* data, const int* expected_order, size_t number_of_blocks) { |
| 38 | ArenaPool pool; |
| 39 | ArenaAllocator allocator(&pool); |
| 40 | HGraphBuilder builder(&allocator); |
| 41 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 42 | HGraph* graph = builder.BuildGraph(*item); |
| 43 | ASSERT_NE(graph, nullptr); |
| 44 | |
| 45 | graph->BuildDominatorTree(); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 46 | graph->TransformToSSA(); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 47 | graph->FindNaturalLoops(); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 48 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 49 | x86::CodeGeneratorX86 codegen(graph); |
| 50 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 51 | liveness.Analyze(); |
| 52 | |
| 53 | ASSERT_EQ(liveness.GetLinearPostOrder().Size(), number_of_blocks); |
| 54 | for (size_t i = 0; i < number_of_blocks; ++i) { |
| 55 | ASSERT_EQ(liveness.GetLinearPostOrder().Get(number_of_blocks - i - 1)->GetBlockId(), |
| 56 | expected_order[i]); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | TEST(LinearizeTest, CFG1) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 61 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 62 | // Block0 |
| 63 | // | |
| 64 | // Block1 |
| 65 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 66 | // Block2 ++++++ |
| 67 | // / \ + |
| 68 | // Block5 Block7 + |
| 69 | // | | + |
| 70 | // Block6 Block3 + |
| 71 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 72 | // Block4 Block8 |
| 73 | |
| 74 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 75 | Instruction::CONST_4 | 0 | 0, |
| 76 | Instruction::IF_EQ, 5, |
| 77 | Instruction::IF_EQ, 0xFFFE, |
| 78 | Instruction::GOTO | 0xFE00, |
| 79 | Instruction::RETURN_VOID); |
| 80 | |
| 81 | const int blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6}; |
| 82 | TestCode(data, blocks, 9); |
| 83 | } |
| 84 | |
| 85 | TEST(LinearizeTest, CFG2) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 86 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 87 | // Block0 |
| 88 | // | |
| 89 | // Block1 |
| 90 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 91 | // Block2 ++++++ |
| 92 | // / \ + |
| 93 | // Block3 Block7 + |
| 94 | // | | + |
| 95 | // Block6 Block4 + |
| 96 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 97 | // Block5 Block8 |
| 98 | |
| 99 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 100 | Instruction::CONST_4 | 0 | 0, |
| 101 | Instruction::IF_EQ, 3, |
| 102 | Instruction::RETURN_VOID, |
| 103 | Instruction::IF_EQ, 0xFFFD, |
| 104 | Instruction::GOTO | 0xFE00); |
| 105 | |
| 106 | const int blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6}; |
| 107 | TestCode(data, blocks, 9); |
| 108 | } |
| 109 | |
| 110 | TEST(LinearizeTest, CFG3) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 111 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 112 | // Block0 |
| 113 | // | |
| 114 | // Block1 |
| 115 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 116 | // Block2 ++++++ |
| 117 | // / \ + |
| 118 | // Block3 Block8 + |
| 119 | // | | + |
| 120 | // Block7 Block5 + |
| 121 | // / + \ + |
| 122 | // Block6 + Block9 |
| 123 | // | + |
| 124 | // Block4 ++ |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 125 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 126 | Instruction::CONST_4 | 0 | 0, |
| 127 | Instruction::IF_EQ, 4, |
| 128 | Instruction::RETURN_VOID, |
| 129 | Instruction::GOTO | 0x0100, |
| 130 | Instruction::IF_EQ, 0xFFFC, |
| 131 | Instruction::GOTO | 0xFD00); |
| 132 | |
| 133 | const int blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7}; |
| 134 | TestCode(data, blocks, 10); |
| 135 | } |
| 136 | |
| 137 | TEST(LinearizeTest, CFG4) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 138 | /* Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 139 | // Block0 |
| 140 | // | |
| 141 | // Block1 |
| 142 | // | |
| 143 | // Block2 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 144 | // / + \ |
| 145 | // Block6 + Block8 |
| 146 | // | + | |
| 147 | // Block7 + Block3 +++++++ |
| 148 | // + / \ + |
| 149 | // Block9 Block10 + |
| 150 | // | + |
| 151 | // Block4 + |
| 152 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 153 | // Block5 Block11 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 154 | */ |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 155 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 156 | Instruction::CONST_4 | 0 | 0, |
| 157 | Instruction::IF_EQ, 7, |
| 158 | Instruction::IF_EQ, 0xFFFE, |
| 159 | Instruction::IF_EQ, 0xFFFE, |
| 160 | Instruction::GOTO | 0xFE00, |
| 161 | Instruction::RETURN_VOID); |
| 162 | |
| 163 | const int blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7}; |
| 164 | TestCode(data, blocks, 12); |
| 165 | } |
| 166 | |
| 167 | TEST(LinearizeTest, CFG5) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 168 | /* Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 169 | // Block0 |
| 170 | // | |
| 171 | // Block1 |
| 172 | // | |
| 173 | // Block2 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 174 | // / + \ |
| 175 | // Block3 + Block8 |
| 176 | // | + | |
| 177 | // Block7 + Block4 +++++++ |
| 178 | // + / \ + |
| 179 | // Block9 Block10 + |
| 180 | // | + |
| 181 | // Block5 + |
| 182 | // +/ \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 183 | // Block6 Block11 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 184 | */ |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 185 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 186 | Instruction::CONST_4 | 0 | 0, |
| 187 | Instruction::IF_EQ, 3, |
| 188 | Instruction::RETURN_VOID, |
| 189 | Instruction::IF_EQ, 0xFFFD, |
| 190 | Instruction::IF_EQ, 0xFFFE, |
| 191 | Instruction::GOTO | 0xFE00); |
| 192 | |
| 193 | const int blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7}; |
| 194 | TestCode(data, blocks, 12); |
| 195 | } |
| 196 | |
| 197 | } // namespace art |