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