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