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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 19 | #include "base/arena_allocator.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 20 | #include "base/stringprintf.h" |
| 21 | #include "builder.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 22 | #include "code_generator.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 23 | #include "code_generator_x86.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 24 | #include "dex_file.h" |
| 25 | #include "dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 26 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 27 | #include "graph_visualizer.h" |
| 28 | #include "nodes.h" |
| 29 | #include "optimizing_unit_test.h" |
| 30 | #include "pretty_printer.h" |
| 31 | #include "ssa_builder.h" |
| 32 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 33 | |
| 34 | #include "gtest/gtest.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | static void TestCode(const uint16_t* data, const int* expected_order, size_t number_of_blocks) { |
| 39 | ArenaPool pool; |
| 40 | ArenaAllocator allocator(&pool); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 41 | HGraph* graph = new (&allocator) HGraph(&allocator); |
| 42 | HGraphBuilder builder(graph); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 43 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 44 | bool graph_built = builder.BuildGraph(*item); |
| 45 | ASSERT_TRUE(graph_built); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 46 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 47 | graph->TryBuildingSsa(); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 48 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 49 | x86::CodeGeneratorX86 codegen(graph, CompilerOptions()); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 50 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 51 | liveness.Analyze(); |
| 52 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 53 | ASSERT_EQ(liveness.GetLinearOrder().Size(), number_of_blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 54 | for (size_t i = 0; i < number_of_blocks; ++i) { |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 55 | ASSERT_EQ(liveness.GetLinearOrder().Get(i)->GetBlockId(), expected_order[i]); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | TEST(LinearizeTest, CFG1) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 60 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 61 | // Block0 |
| 62 | // | |
| 63 | // Block1 |
| 64 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 65 | // Block2 ++++++ |
| 66 | // / \ + |
| 67 | // Block5 Block7 + |
| 68 | // | | + |
| 69 | // Block6 Block3 + |
| 70 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 71 | // Block4 Block8 |
| 72 | |
| 73 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 74 | Instruction::CONST_4 | 0 | 0, |
| 75 | Instruction::IF_EQ, 5, |
| 76 | Instruction::IF_EQ, 0xFFFE, |
| 77 | Instruction::GOTO | 0xFE00, |
| 78 | Instruction::RETURN_VOID); |
| 79 | |
| 80 | const int blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6}; |
| 81 | TestCode(data, blocks, 9); |
| 82 | } |
| 83 | |
| 84 | TEST(LinearizeTest, CFG2) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 85 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 86 | // Block0 |
| 87 | // | |
| 88 | // Block1 |
| 89 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 90 | // Block2 ++++++ |
| 91 | // / \ + |
| 92 | // Block3 Block7 + |
| 93 | // | | + |
| 94 | // Block6 Block4 + |
| 95 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 96 | // Block5 Block8 |
| 97 | |
| 98 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 99 | Instruction::CONST_4 | 0 | 0, |
| 100 | Instruction::IF_EQ, 3, |
| 101 | Instruction::RETURN_VOID, |
| 102 | Instruction::IF_EQ, 0xFFFD, |
| 103 | Instruction::GOTO | 0xFE00); |
| 104 | |
| 105 | const int blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6}; |
| 106 | TestCode(data, blocks, 9); |
| 107 | } |
| 108 | |
| 109 | TEST(LinearizeTest, CFG3) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 110 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 111 | // Block0 |
| 112 | // | |
| 113 | // Block1 |
| 114 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 115 | // Block2 ++++++ |
| 116 | // / \ + |
| 117 | // Block3 Block8 + |
| 118 | // | | + |
| 119 | // Block7 Block5 + |
| 120 | // / + \ + |
| 121 | // Block6 + Block9 |
| 122 | // | + |
| 123 | // Block4 ++ |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 124 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 125 | Instruction::CONST_4 | 0 | 0, |
| 126 | Instruction::IF_EQ, 4, |
| 127 | Instruction::RETURN_VOID, |
| 128 | Instruction::GOTO | 0x0100, |
| 129 | Instruction::IF_EQ, 0xFFFC, |
| 130 | Instruction::GOTO | 0xFD00); |
| 131 | |
| 132 | const int blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7}; |
| 133 | TestCode(data, blocks, 10); |
| 134 | } |
| 135 | |
| 136 | TEST(LinearizeTest, CFG4) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 137 | /* Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 138 | // Block0 |
| 139 | // | |
| 140 | // Block1 |
| 141 | // | |
| 142 | // Block2 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 143 | // / + \ |
| 144 | // Block6 + Block8 |
| 145 | // | + | |
| 146 | // Block7 + Block3 +++++++ |
| 147 | // + / \ + |
| 148 | // Block9 Block10 + |
| 149 | // | + |
| 150 | // Block4 + |
| 151 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 152 | // Block5 Block11 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 153 | */ |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 154 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 155 | Instruction::CONST_4 | 0 | 0, |
| 156 | Instruction::IF_EQ, 7, |
| 157 | Instruction::IF_EQ, 0xFFFE, |
| 158 | Instruction::IF_EQ, 0xFFFE, |
| 159 | Instruction::GOTO | 0xFE00, |
| 160 | Instruction::RETURN_VOID); |
| 161 | |
| 162 | const int blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7}; |
| 163 | TestCode(data, blocks, 12); |
| 164 | } |
| 165 | |
| 166 | TEST(LinearizeTest, CFG5) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 167 | /* Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 168 | // Block0 |
| 169 | // | |
| 170 | // Block1 |
| 171 | // | |
| 172 | // Block2 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 173 | // / + \ |
| 174 | // Block3 + Block8 |
| 175 | // | + | |
| 176 | // Block7 + Block4 +++++++ |
| 177 | // + / \ + |
| 178 | // Block9 Block10 + |
| 179 | // | + |
| 180 | // Block5 + |
| 181 | // +/ \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 182 | // Block6 Block11 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 183 | */ |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 184 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 185 | Instruction::CONST_4 | 0 | 0, |
| 186 | Instruction::IF_EQ, 3, |
| 187 | Instruction::RETURN_VOID, |
| 188 | Instruction::IF_EQ, 0xFFFD, |
| 189 | Instruction::IF_EQ, 0xFFFE, |
| 190 | Instruction::GOTO | 0xFE00); |
| 191 | |
| 192 | const int blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7}; |
| 193 | TestCode(data, blocks, 12); |
| 194 | } |
| 195 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 196 | TEST(LinearizeTest, CFG6) { |
| 197 | // Block0 |
| 198 | // | |
| 199 | // Block1 |
| 200 | // | |
| 201 | // Block2 ++++++++++++++ |
| 202 | // | + |
| 203 | // Block3 + |
| 204 | // / \ + |
| 205 | // Block8 Block4 + |
| 206 | // | / \ + |
| 207 | // Block5 <- Block9 Block6 + |
| 208 | // | |
| 209 | // Block7 |
| 210 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 211 | Instruction::CONST_4 | 0 | 0, |
| 212 | Instruction::GOTO | 0x0100, |
| 213 | Instruction::IF_EQ, 0x0004, |
| 214 | Instruction::IF_EQ, 0x0003, |
| 215 | Instruction::RETURN_VOID, |
| 216 | Instruction::GOTO | 0xFA00); |
| 217 | |
| 218 | const int blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7}; |
| 219 | TestCode(data, blocks, arraysize(blocks)); |
| 220 | } |
| 221 | |
| 222 | TEST(LinearizeTest, CFG7) { |
| 223 | // Structure of this graph (+ are back edges) |
| 224 | // Block0 |
| 225 | // | |
| 226 | // Block1 |
| 227 | // | |
| 228 | // Block2 ++++++++ |
| 229 | // | + |
| 230 | // Block3 + |
| 231 | // / \ + |
| 232 | // Block4 Block8 + |
| 233 | // / \ | + |
| 234 | // Block5 Block9 - Block6 + |
| 235 | // | |
| 236 | // Block7 |
| 237 | // |
| 238 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 239 | Instruction::CONST_4 | 0 | 0, |
| 240 | Instruction::GOTO | 0x0100, |
| 241 | Instruction::IF_EQ, 0x0005, |
| 242 | Instruction::IF_EQ, 0x0003, |
| 243 | Instruction::RETURN_VOID, |
| 244 | Instruction::GOTO | 0xFA00); |
| 245 | |
| 246 | const int blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7}; |
| 247 | TestCode(data, blocks, arraysize(blocks)); |
| 248 | } |
| 249 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 250 | } // namespace art |