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 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 19 | #include "arch/x86/instruction_set_features_x86.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 20 | #include "base/arena_allocator.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 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" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/dex_file.h" |
| 25 | #include "dex/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" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 31 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 32 | |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 33 | namespace art { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 34 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 35 | class LinearizeTest : public OptimizingUnitTest { |
| 36 | protected: |
| 37 | template <size_t number_of_blocks> |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 38 | void TestCode(const std::vector<uint16_t>& data, |
| 39 | const uint32_t (&expected_order)[number_of_blocks]); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 40 | }; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 41 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 42 | template <size_t number_of_blocks> |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 43 | void LinearizeTest::TestCode(const std::vector<uint16_t>& data, |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 44 | const uint32_t (&expected_order)[number_of_blocks]) { |
| 45 | HGraph* graph = CreateCFG(data); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 46 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 47 | X86InstructionSetFeatures::FromCppDefines()); |
| 48 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 49 | SsaLivenessAnalysis liveness(graph, &codegen, GetScopedAllocator()); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 50 | liveness.Analyze(); |
| 51 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 52 | ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 53 | for (size_t i = 0; i < number_of_blocks; ++i) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 54 | ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 58 | TEST_F(LinearizeTest, CFG1) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 59 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 60 | // Block0 |
| 61 | // | |
| 62 | // Block1 |
| 63 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 64 | // Block2 ++++++ |
| 65 | // / \ + |
| 66 | // Block5 Block7 + |
| 67 | // | | + |
| 68 | // Block6 Block3 + |
| 69 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 70 | // Block4 Block8 |
| 71 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 72 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 73 | Instruction::CONST_4 | 0 | 0, |
| 74 | Instruction::IF_EQ, 5, |
| 75 | Instruction::IF_EQ, 0xFFFE, |
| 76 | Instruction::GOTO | 0xFE00, |
| 77 | Instruction::RETURN_VOID); |
| 78 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 79 | const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6}; |
| 80 | TestCode(data, blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 81 | } |
| 82 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 83 | TEST_F(LinearizeTest, CFG2) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 84 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 85 | // Block0 |
| 86 | // | |
| 87 | // Block1 |
| 88 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 89 | // Block2 ++++++ |
| 90 | // / \ + |
| 91 | // Block3 Block7 + |
| 92 | // | | + |
| 93 | // Block6 Block4 + |
| 94 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 95 | // Block5 Block8 |
| 96 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 97 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 98 | Instruction::CONST_4 | 0 | 0, |
| 99 | Instruction::IF_EQ, 3, |
| 100 | Instruction::RETURN_VOID, |
| 101 | Instruction::IF_EQ, 0xFFFD, |
| 102 | Instruction::GOTO | 0xFE00); |
| 103 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 104 | const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6}; |
| 105 | TestCode(data, blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 106 | } |
| 107 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 108 | TEST_F(LinearizeTest, CFG3) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 109 | // Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 110 | // Block0 |
| 111 | // | |
| 112 | // Block1 |
| 113 | // | |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 114 | // Block2 ++++++ |
| 115 | // / \ + |
| 116 | // Block3 Block8 + |
| 117 | // | | + |
| 118 | // Block7 Block5 + |
| 119 | // / + \ + |
| 120 | // Block6 + Block9 |
| 121 | // | + |
| 122 | // Block4 ++ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 123 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 124 | Instruction::CONST_4 | 0 | 0, |
| 125 | Instruction::IF_EQ, 4, |
| 126 | Instruction::RETURN_VOID, |
| 127 | Instruction::GOTO | 0x0100, |
| 128 | Instruction::IF_EQ, 0xFFFC, |
| 129 | Instruction::GOTO | 0xFD00); |
| 130 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 131 | const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7}; |
| 132 | TestCode(data, blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 133 | } |
| 134 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 135 | TEST_F(LinearizeTest, CFG4) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 136 | /* Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 137 | // Block0 |
| 138 | // | |
| 139 | // Block1 |
| 140 | // | |
| 141 | // Block2 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 142 | // / + \ |
| 143 | // Block6 + Block8 |
| 144 | // | + | |
| 145 | // Block7 + Block3 +++++++ |
| 146 | // + / \ + |
| 147 | // Block9 Block10 + |
| 148 | // | + |
| 149 | // Block4 + |
| 150 | // + / \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 151 | // Block5 Block11 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 152 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 153 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 154 | Instruction::CONST_4 | 0 | 0, |
| 155 | Instruction::IF_EQ, 7, |
| 156 | Instruction::IF_EQ, 0xFFFE, |
| 157 | Instruction::IF_EQ, 0xFFFE, |
| 158 | Instruction::GOTO | 0xFE00, |
| 159 | Instruction::RETURN_VOID); |
| 160 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 161 | const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7}; |
| 162 | TestCode(data, blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 163 | } |
| 164 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 165 | TEST_F(LinearizeTest, CFG5) { |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 166 | /* Structure of this graph (+ are back edges) |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 167 | // Block0 |
| 168 | // | |
| 169 | // Block1 |
| 170 | // | |
| 171 | // Block2 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 172 | // / + \ |
| 173 | // Block3 + Block8 |
| 174 | // | + | |
| 175 | // Block7 + Block4 +++++++ |
| 176 | // + / \ + |
| 177 | // Block9 Block10 + |
| 178 | // | + |
| 179 | // Block5 + |
| 180 | // +/ \ + |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 181 | // Block6 Block11 |
Nicolas Geoffray | 8f1a4d4 | 2014-05-16 09:36:00 +0100 | [diff] [blame] | 182 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 183 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 184 | Instruction::CONST_4 | 0 | 0, |
| 185 | Instruction::IF_EQ, 3, |
| 186 | Instruction::RETURN_VOID, |
| 187 | Instruction::IF_EQ, 0xFFFD, |
| 188 | Instruction::IF_EQ, 0xFFFE, |
| 189 | Instruction::GOTO | 0xFE00); |
| 190 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 191 | const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7}; |
| 192 | TestCode(data, blocks); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 193 | } |
| 194 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 195 | TEST_F(LinearizeTest, CFG6) { |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 196 | // Block0 |
| 197 | // | |
| 198 | // Block1 |
| 199 | // | |
| 200 | // Block2 ++++++++++++++ |
| 201 | // | + |
| 202 | // Block3 + |
| 203 | // / \ + |
| 204 | // Block8 Block4 + |
| 205 | // | / \ + |
| 206 | // Block5 <- Block9 Block6 + |
| 207 | // | |
| 208 | // Block7 |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 209 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 210 | Instruction::CONST_4 | 0 | 0, |
| 211 | Instruction::GOTO | 0x0100, |
| 212 | Instruction::IF_EQ, 0x0004, |
| 213 | Instruction::IF_EQ, 0x0003, |
| 214 | Instruction::RETURN_VOID, |
| 215 | Instruction::GOTO | 0xFA00); |
| 216 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 217 | const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7}; |
| 218 | TestCode(data, blocks); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 219 | } |
| 220 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 221 | TEST_F(LinearizeTest, CFG7) { |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 222 | // Structure of this graph (+ are back edges) |
| 223 | // Block0 |
| 224 | // | |
| 225 | // Block1 |
| 226 | // | |
| 227 | // Block2 ++++++++ |
| 228 | // | + |
| 229 | // Block3 + |
| 230 | // / \ + |
| 231 | // Block4 Block8 + |
| 232 | // / \ | + |
| 233 | // Block5 Block9 - Block6 + |
| 234 | // | |
| 235 | // Block7 |
| 236 | // |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame^] | 237 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 238 | Instruction::CONST_4 | 0 | 0, |
| 239 | Instruction::GOTO | 0x0100, |
| 240 | Instruction::IF_EQ, 0x0005, |
| 241 | Instruction::IF_EQ, 0x0003, |
| 242 | Instruction::RETURN_VOID, |
| 243 | Instruction::GOTO | 0xFA00); |
| 244 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 245 | const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7}; |
| 246 | TestCode(data, blocks); |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 249 | } // namespace art |