Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #include "base/arena_allocator.h" |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 18 | #include "base/macros.h" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 19 | #include "builder.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 20 | #include "dex/dex_file.h" |
| 21 | #include "dex/dex_instruction.h" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 22 | #include "nodes.h" |
| 23 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 24 | #include "pretty_printer.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 26 | |
| 27 | #include "gtest/gtest.h" |
| 28 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 29 | namespace art HIDDEN { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 30 | |
Santiago Aboy Solanes | 2c50b3a | 2023-02-10 10:25:31 +0000 | [diff] [blame] | 31 | class FindLoopsTest : public CommonCompilerTest, public OptimizingUnitTestHelper {}; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 32 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 33 | TEST_F(FindLoopsTest, CFG1) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 34 | // Constant is not used. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 35 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 36 | Instruction::CONST_4 | 0 | 0, |
| 37 | Instruction::RETURN_VOID); |
| 38 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 39 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 40 | for (HBasicBlock* block : graph->GetBlocks()) { |
| 41 | ASSERT_EQ(block->GetLoopInformation(), nullptr); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 45 | TEST_F(FindLoopsTest, CFG2) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 46 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 47 | Instruction::CONST_4 | 0 | 0, |
| 48 | Instruction::RETURN); |
| 49 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 50 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 51 | for (HBasicBlock* block : graph->GetBlocks()) { |
| 52 | ASSERT_EQ(block->GetLoopInformation(), nullptr); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 56 | TEST_F(FindLoopsTest, CFG3) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 57 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 58 | Instruction::CONST_4 | 3 << 12 | 0, |
| 59 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 60 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 61 | Instruction::GOTO | 0x100, |
| 62 | Instruction::RETURN); |
| 63 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 64 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 65 | for (HBasicBlock* block : graph->GetBlocks()) { |
| 66 | ASSERT_EQ(block->GetLoopInformation(), nullptr); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 70 | TEST_F(FindLoopsTest, CFG4) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 71 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 72 | Instruction::CONST_4 | 0 | 0, |
| 73 | Instruction::IF_EQ, 4, |
| 74 | Instruction::CONST_4 | 4 << 12 | 0, |
| 75 | Instruction::GOTO | 0x200, |
| 76 | Instruction::CONST_4 | 5 << 12 | 0, |
| 77 | Instruction::RETURN | 0 << 8); |
| 78 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 79 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 80 | for (HBasicBlock* block : graph->GetBlocks()) { |
| 81 | ASSERT_EQ(block->GetLoopInformation(), nullptr); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 85 | TEST_F(FindLoopsTest, CFG5) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 86 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 87 | Instruction::CONST_4 | 0 | 0, |
| 88 | Instruction::IF_EQ, 3, |
| 89 | Instruction::CONST_4 | 4 << 12 | 0, |
| 90 | Instruction::RETURN | 0 << 8); |
| 91 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 92 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 93 | for (HBasicBlock* block : graph->GetBlocks()) { |
| 94 | ASSERT_EQ(block->GetLoopInformation(), nullptr); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | static void TestBlock(HGraph* graph, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 99 | uint32_t block_id, |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 100 | bool is_loop_header, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 101 | uint32_t parent_loop_header_id, |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 102 | const int* blocks_in_loop = nullptr, |
| 103 | size_t number_of_blocks = 0) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 104 | HBasicBlock* block = graph->GetBlocks()[block_id]; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 105 | ASSERT_EQ(block->IsLoopHeader(), is_loop_header); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 106 | if (parent_loop_header_id == kInvalidBlockId) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 107 | ASSERT_EQ(block->GetLoopInformation(), nullptr); |
| 108 | } else { |
| 109 | ASSERT_EQ(block->GetLoopInformation()->GetHeader()->GetBlockId(), parent_loop_header_id); |
| 110 | } |
| 111 | |
| 112 | if (blocks_in_loop != nullptr) { |
| 113 | HLoopInformation* info = block->GetLoopInformation(); |
| 114 | const BitVector& blocks = info->GetBlocks(); |
| 115 | ASSERT_EQ(blocks.NumSetBits(), number_of_blocks); |
| 116 | for (size_t i = 0; i < number_of_blocks; ++i) { |
| 117 | ASSERT_TRUE(blocks.IsBitSet(blocks_in_loop[i])); |
| 118 | } |
| 119 | } else { |
| 120 | ASSERT_FALSE(block->IsLoopHeader()); |
| 121 | } |
| 122 | } |
| 123 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 124 | TEST_F(FindLoopsTest, Loop1) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 125 | // Simple loop with one preheader and one back edge. |
| 126 | // var a = 0; |
| 127 | // while (a == a) { |
| 128 | // } |
| 129 | // return; |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 130 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 131 | Instruction::CONST_4 | 0 | 0, |
| 132 | Instruction::IF_EQ, 3, |
| 133 | Instruction::GOTO | 0xFE00, |
| 134 | Instruction::RETURN_VOID); |
| 135 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 136 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 137 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 138 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 139 | TestBlock(graph, 1, false, kInvalidBlockId); // pre header |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 140 | const int blocks2[] = {2, 3}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 141 | TestBlock(graph, 2, true, 2, blocks2, 2); // loop header |
| 142 | TestBlock(graph, 3, false, 2); // block in loop |
| 143 | TestBlock(graph, 4, false, kInvalidBlockId); // return block |
| 144 | TestBlock(graph, 5, false, kInvalidBlockId); // exit block |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 145 | } |
| 146 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 147 | TEST_F(FindLoopsTest, Loop2) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 148 | // Make sure we support a preheader of a loop not being the first predecessor |
| 149 | // in the predecessor list of the header. |
| 150 | // var a = 0; |
| 151 | // while (a == a) { |
| 152 | // } |
| 153 | // return a; |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 154 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 155 | Instruction::CONST_4 | 0 | 0, |
| 156 | Instruction::GOTO | 0x400, |
| 157 | Instruction::IF_EQ, 4, |
| 158 | Instruction::GOTO | 0xFE00, |
| 159 | Instruction::GOTO | 0xFD00, |
| 160 | Instruction::RETURN | 0 << 8); |
| 161 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 162 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 163 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 164 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 165 | TestBlock(graph, 1, false, kInvalidBlockId); // goto block |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 166 | const int blocks2[] = {2, 3}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 167 | TestBlock(graph, 2, true, 2, blocks2, 2); // loop header |
| 168 | TestBlock(graph, 3, false, 2); // block in loop |
| 169 | TestBlock(graph, 4, false, kInvalidBlockId); // pre header |
| 170 | TestBlock(graph, 5, false, kInvalidBlockId); // return block |
| 171 | TestBlock(graph, 6, false, kInvalidBlockId); // exit block |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 172 | } |
| 173 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 174 | TEST_F(FindLoopsTest, Loop3) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 175 | // Make sure we create a preheader of a loop when a header originally has two |
| 176 | // incoming blocks and one back edge. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 177 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 178 | Instruction::CONST_4 | 0 | 0, |
| 179 | Instruction::IF_EQ, 3, |
| 180 | Instruction::GOTO | 0x100, |
| 181 | Instruction::IF_EQ, 3, |
| 182 | Instruction::GOTO | 0xFE00, |
| 183 | Instruction::RETURN | 0 << 8); |
| 184 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 185 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 186 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 187 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 188 | TestBlock(graph, 1, false, kInvalidBlockId); // goto block |
| 189 | TestBlock(graph, 2, false, kInvalidBlockId); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 190 | const int blocks2[] = {3, 4}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 191 | TestBlock(graph, 3, true, 3, blocks2, 2); // loop header |
| 192 | TestBlock(graph, 4, false, 3); // block in loop |
| 193 | TestBlock(graph, 5, false, kInvalidBlockId); // pre header |
| 194 | TestBlock(graph, 6, false, kInvalidBlockId); // return block |
| 195 | TestBlock(graph, 7, false, kInvalidBlockId); // exit block |
| 196 | TestBlock(graph, 8, false, kInvalidBlockId); // synthesized pre header |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 197 | } |
| 198 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 199 | TEST_F(FindLoopsTest, Loop4) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 200 | // Test loop with originally two back edges. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 201 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 202 | Instruction::CONST_4 | 0 | 0, |
| 203 | Instruction::IF_EQ, 6, |
| 204 | Instruction::IF_EQ, 3, |
| 205 | Instruction::GOTO | 0xFC00, |
| 206 | Instruction::GOTO | 0xFB00, |
| 207 | Instruction::RETURN | 0 << 8); |
| 208 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 209 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 210 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 211 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 212 | TestBlock(graph, 1, false, kInvalidBlockId); // pre header |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 213 | const int blocks2[] = {2, 3, 4, 5}; |
| 214 | TestBlock(graph, 2, true, 2, blocks2, arraysize(blocks2)); // loop header |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 215 | TestBlock(graph, 3, false, 2); // block in loop |
| 216 | TestBlock(graph, 4, false, 2); // back edge |
| 217 | TestBlock(graph, 5, false, 2); // back edge |
| 218 | TestBlock(graph, 6, false, kInvalidBlockId); // return block |
| 219 | TestBlock(graph, 7, false, kInvalidBlockId); // exit block |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 223 | TEST_F(FindLoopsTest, Loop5) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 224 | // Test loop with two exit edges. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 225 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 226 | Instruction::CONST_4 | 0 | 0, |
| 227 | Instruction::IF_EQ, 6, |
| 228 | Instruction::IF_EQ, 3, |
| 229 | Instruction::GOTO | 0x0200, |
| 230 | Instruction::GOTO | 0xFB00, |
| 231 | Instruction::RETURN | 0 << 8); |
| 232 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 233 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 234 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 235 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 236 | TestBlock(graph, 1, false, kInvalidBlockId); // pre header |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 237 | const int blocks2[] = {2, 3, 5}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 238 | TestBlock(graph, 2, true, 2, blocks2, 3); // loop header |
| 239 | TestBlock(graph, 3, false, 2); // block in loop |
| 240 | TestBlock(graph, 4, false, kInvalidBlockId); // loop exit |
| 241 | TestBlock(graph, 5, false, 2); // back edge |
| 242 | TestBlock(graph, 6, false, kInvalidBlockId); // return block |
| 243 | TestBlock(graph, 7, false, kInvalidBlockId); // exit block |
| 244 | TestBlock(graph, 8, false, kInvalidBlockId); // synthesized block at the loop exit |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 245 | } |
| 246 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 247 | TEST_F(FindLoopsTest, InnerLoop) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 248 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 249 | Instruction::CONST_4 | 0 | 0, |
| 250 | Instruction::IF_EQ, 6, |
| 251 | Instruction::IF_EQ, 3, |
| 252 | Instruction::GOTO | 0xFE00, // inner loop |
| 253 | Instruction::GOTO | 0xFB00, |
| 254 | Instruction::RETURN | 0 << 8); |
| 255 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 256 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 257 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 258 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 259 | TestBlock(graph, 1, false, kInvalidBlockId); // pre header of outer loop |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 260 | const int blocks2[] = {2, 3, 4, 5, 8}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 261 | TestBlock(graph, 2, true, 2, blocks2, 5); // outer loop header |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 262 | const int blocks3[] = {3, 4}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 263 | TestBlock(graph, 3, true, 3, blocks3, 2); // inner loop header |
| 264 | TestBlock(graph, 4, false, 3); // back edge on inner loop |
| 265 | TestBlock(graph, 5, false, 2); // back edge on outer loop |
| 266 | TestBlock(graph, 6, false, kInvalidBlockId); // return block |
| 267 | TestBlock(graph, 7, false, kInvalidBlockId); // exit block |
| 268 | TestBlock(graph, 8, false, 2); // synthesized block as pre header of inner loop |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 269 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 270 | ASSERT_TRUE(graph->GetBlocks()[3]->GetLoopInformation()->IsIn( |
| 271 | *graph->GetBlocks()[2]->GetLoopInformation())); |
| 272 | ASSERT_FALSE(graph->GetBlocks()[2]->GetLoopInformation()->IsIn( |
| 273 | *graph->GetBlocks()[3]->GetLoopInformation())); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 274 | } |
| 275 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 276 | TEST_F(FindLoopsTest, TwoLoops) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 277 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 278 | Instruction::CONST_4 | 0 | 0, |
| 279 | Instruction::IF_EQ, 3, |
| 280 | Instruction::GOTO | 0xFE00, // first loop |
| 281 | Instruction::IF_EQ, 3, |
| 282 | Instruction::GOTO | 0xFE00, // second loop |
| 283 | Instruction::RETURN | 0 << 8); |
| 284 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 285 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 286 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 287 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 288 | TestBlock(graph, 1, false, kInvalidBlockId); // pre header of first loop |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 289 | const int blocks2[] = {2, 3}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 290 | TestBlock(graph, 2, true, 2, blocks2, 2); // first loop header |
| 291 | TestBlock(graph, 3, false, 2); // back edge of first loop |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 292 | const int blocks4[] = {4, 5}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 293 | TestBlock(graph, 4, true, 4, blocks4, 2); // second loop header |
| 294 | TestBlock(graph, 5, false, 4); // back edge of second loop |
| 295 | TestBlock(graph, 6, false, kInvalidBlockId); // return block |
| 296 | TestBlock(graph, 7, false, kInvalidBlockId); // exit block |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 297 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 298 | ASSERT_FALSE(graph->GetBlocks()[4]->GetLoopInformation()->IsIn( |
| 299 | *graph->GetBlocks()[2]->GetLoopInformation())); |
| 300 | ASSERT_FALSE(graph->GetBlocks()[2]->GetLoopInformation()->IsIn( |
| 301 | *graph->GetBlocks()[4]->GetLoopInformation())); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 302 | } |
| 303 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 304 | TEST_F(FindLoopsTest, NonNaturalLoop) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 305 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 306 | Instruction::CONST_4 | 0 | 0, |
| 307 | Instruction::IF_EQ, 3, |
| 308 | Instruction::GOTO | 0x0100, |
| 309 | Instruction::IF_EQ, 3, |
| 310 | Instruction::GOTO | 0xFD00, |
| 311 | Instruction::RETURN | 0 << 8); |
| 312 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 313 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 314 | ASSERT_TRUE(graph->GetBlocks()[3]->IsLoopHeader()); |
| 315 | HLoopInformation* info = graph->GetBlocks()[3]->GetLoopInformation(); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 316 | ASSERT_EQ(1u, info->NumberOfBackEdges()); |
| 317 | ASSERT_FALSE(info->GetHeader()->Dominates(info->GetBackEdges()[0])); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 318 | } |
| 319 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 320 | TEST_F(FindLoopsTest, DoWhileLoop) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 321 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 322 | Instruction::CONST_4 | 0 | 0, |
| 323 | Instruction::GOTO | 0x0100, |
| 324 | Instruction::IF_EQ, 0xFFFF, |
| 325 | Instruction::RETURN | 0 << 8); |
| 326 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 327 | HGraph* graph = CreateCFG(data); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 328 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 329 | TestBlock(graph, 0, false, kInvalidBlockId); // entry block |
| 330 | TestBlock(graph, 1, false, kInvalidBlockId); // pre header of first loop |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 331 | const int blocks2[] = {2, 3, 6}; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 332 | TestBlock(graph, 2, true, 2, blocks2, 3); // loop header |
| 333 | TestBlock(graph, 3, false, 2); // back edge of first loop |
| 334 | TestBlock(graph, 4, false, kInvalidBlockId); // return block |
| 335 | TestBlock(graph, 5, false, kInvalidBlockId); // exit block |
| 336 | TestBlock(graph, 6, false, 2); // synthesized block to avoid a critical edge |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | } // namespace art |