Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +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 "nodes.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 18 | |
| 19 | #include "base/arena_allocator.h" |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 20 | #include "base/macros.h" |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 21 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 22 | |
| 23 | #include "gtest/gtest.h" |
| 24 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 25 | namespace art HIDDEN { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 26 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 27 | class NodeTest : public OptimizingUnitTest {}; |
| 28 | |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 29 | /** |
Alex Light | 210a78d | 2020-11-30 16:58:05 -0800 | [diff] [blame] | 30 | * Test that we can clear loop and dominator information in either order. |
| 31 | * Code is: |
| 32 | * while (true) { |
| 33 | * if (foobar) { break; } |
| 34 | * if (baz) { xyz; } else { abc; } |
| 35 | * } |
| 36 | * dosomething(); |
| 37 | */ |
| 38 | TEST_F(NodeTest, ClearLoopThenDominanceInformation) { |
| 39 | CreateGraph(); |
| 40 | AdjacencyListGraph alg(graph_, |
| 41 | GetAllocator(), |
| 42 | "entry", |
| 43 | "exit", |
| 44 | {{"entry", "loop_pre_header"}, |
| 45 | |
| 46 | {"loop_pre_header", "loop_header"}, |
| 47 | {"loop_header", "critical_break"}, |
| 48 | {"loop_header", "loop_body"}, |
| 49 | {"loop_body", "loop_if_left"}, |
| 50 | {"loop_body", "loop_if_right"}, |
| 51 | {"loop_if_left", "loop_merge"}, |
| 52 | {"loop_if_right", "loop_merge"}, |
| 53 | {"loop_merge", "loop_header"}, |
| 54 | |
| 55 | {"critical_break", "breturn"}, |
| 56 | {"breturn", "exit"}}); |
| 57 | graph_->ClearDominanceInformation(); |
| 58 | graph_->BuildDominatorTree(); |
| 59 | |
| 60 | // Test |
| 61 | EXPECT_TRUE( |
| 62 | std::all_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 63 | return b == graph_->GetEntryBlock() || b == nullptr || b->GetDominator() != nullptr; |
| 64 | })); |
| 65 | EXPECT_TRUE( |
| 66 | std::any_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 67 | return b != nullptr && b->GetLoopInformation() != nullptr; |
| 68 | })); |
| 69 | |
| 70 | // Clear |
| 71 | graph_->ClearLoopInformation(); |
| 72 | graph_->ClearDominanceInformation(); |
| 73 | |
| 74 | // Test |
| 75 | EXPECT_TRUE( |
| 76 | std::none_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 77 | return b != nullptr && b->GetDominator() != nullptr; |
| 78 | })); |
| 79 | EXPECT_TRUE( |
| 80 | std::all_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 81 | return b == nullptr || b->GetLoopInformation() == nullptr; |
| 82 | })); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Test that we can clear loop and dominator information in either order. |
| 87 | * Code is: |
| 88 | * while (true) { |
| 89 | * if (foobar) { break; } |
| 90 | * if (baz) { xyz; } else { abc; } |
| 91 | * } |
| 92 | * dosomething(); |
| 93 | */ |
| 94 | TEST_F(NodeTest, ClearDominanceThenLoopInformation) { |
| 95 | CreateGraph(); |
| 96 | AdjacencyListGraph alg(graph_, |
| 97 | GetAllocator(), |
| 98 | "entry", |
| 99 | "exit", |
| 100 | {{"entry", "loop_pre_header"}, |
| 101 | |
| 102 | {"loop_pre_header", "loop_header"}, |
| 103 | {"loop_header", "critical_break"}, |
| 104 | {"loop_header", "loop_body"}, |
| 105 | {"loop_body", "loop_if_left"}, |
| 106 | {"loop_body", "loop_if_right"}, |
| 107 | {"loop_if_left", "loop_merge"}, |
| 108 | {"loop_if_right", "loop_merge"}, |
| 109 | {"loop_merge", "loop_header"}, |
| 110 | |
| 111 | {"critical_break", "breturn"}, |
| 112 | {"breturn", "exit"}}); |
| 113 | graph_->ClearDominanceInformation(); |
| 114 | graph_->BuildDominatorTree(); |
| 115 | |
| 116 | // Test |
| 117 | EXPECT_TRUE( |
| 118 | std::all_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 119 | return b == graph_->GetEntryBlock() || b == nullptr || b->GetDominator() != nullptr; |
| 120 | })); |
| 121 | EXPECT_TRUE( |
| 122 | std::any_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 123 | return b != nullptr && b->GetLoopInformation() != nullptr; |
| 124 | })); |
| 125 | |
| 126 | // Clear |
| 127 | graph_->ClearDominanceInformation(); |
| 128 | graph_->ClearLoopInformation(); |
| 129 | |
| 130 | // Test |
| 131 | EXPECT_TRUE( |
| 132 | std::none_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 133 | return b != nullptr && b->GetDominator() != nullptr; |
| 134 | })); |
| 135 | EXPECT_TRUE( |
| 136 | std::all_of(graph_->GetBlocks().begin(), graph_->GetBlocks().end(), [&](HBasicBlock* b) { |
| 137 | return b == nullptr || b->GetLoopInformation() == nullptr; |
| 138 | })); |
| 139 | } |
| 140 | |
| 141 | /** |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 142 | * Test that removing instruction from the graph removes itself from user lists |
| 143 | * and environment lists. |
| 144 | */ |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 145 | TEST_F(NodeTest, RemoveInstruction) { |
| 146 | HGraph* graph = CreateGraph(); |
| 147 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 148 | graph->AddBlock(entry); |
| 149 | graph->SetEntryBlock(entry); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 150 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 151 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 152 | entry->AddInstruction(parameter); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 153 | entry->AddInstruction(new (GetAllocator()) HGoto()); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 154 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 155 | HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 156 | graph->AddBlock(first_block); |
| 157 | entry->AddSuccessor(first_block); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 158 | HInstruction* null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 159 | first_block->AddInstruction(null_check); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 160 | first_block->AddInstruction(new (GetAllocator()) HReturnVoid()); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 161 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 162 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 163 | graph->AddBlock(exit_block); |
| 164 | first_block->AddSuccessor(exit_block); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 165 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 166 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 167 | HEnvironment* environment = new (GetAllocator()) HEnvironment( |
| 168 | GetAllocator(), 1, graph->GetArtMethod(), 0, null_check); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 169 | null_check->SetRawEnvironment(environment); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 170 | environment->SetRawEnvAt(0, parameter); |
| 171 | parameter->AddEnvUseAt(null_check->GetEnvironment(), 0); |
| 172 | |
| 173 | ASSERT_TRUE(parameter->HasEnvironmentUses()); |
| 174 | ASSERT_TRUE(parameter->HasUses()); |
| 175 | |
| 176 | first_block->RemoveInstruction(null_check); |
| 177 | |
| 178 | ASSERT_FALSE(parameter->HasEnvironmentUses()); |
| 179 | ASSERT_FALSE(parameter->HasUses()); |
| 180 | } |
| 181 | |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 182 | /** |
| 183 | * Test that inserting an instruction in the graph updates user lists. |
| 184 | */ |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 185 | TEST_F(NodeTest, InsertInstruction) { |
| 186 | HGraph* graph = CreateGraph(); |
| 187 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 188 | graph->AddBlock(entry); |
| 189 | graph->SetEntryBlock(entry); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 190 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 191 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 192 | HInstruction* parameter2 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 193 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 194 | entry->AddInstruction(parameter1); |
| 195 | entry->AddInstruction(parameter2); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 196 | entry->AddInstruction(new (GetAllocator()) HExit()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 197 | |
| 198 | ASSERT_FALSE(parameter1->HasUses()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 199 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 200 | HInstruction* to_insert = new (GetAllocator()) HNullCheck(parameter1, 0); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 201 | entry->InsertInstructionBefore(to_insert, parameter2); |
| 202 | |
| 203 | ASSERT_TRUE(parameter1->HasUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 204 | ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Test that adding an instruction in the graph updates user lists. |
| 209 | */ |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 210 | TEST_F(NodeTest, AddInstruction) { |
| 211 | HGraph* graph = CreateGraph(); |
| 212 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 213 | graph->AddBlock(entry); |
| 214 | graph->SetEntryBlock(entry); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 215 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 216 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 217 | entry->AddInstruction(parameter); |
| 218 | |
| 219 | ASSERT_FALSE(parameter->HasUses()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 220 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 221 | HInstruction* to_add = new (GetAllocator()) HNullCheck(parameter, 0); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 222 | entry->AddInstruction(to_add); |
| 223 | |
| 224 | ASSERT_TRUE(parameter->HasUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 225 | ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 228 | TEST_F(NodeTest, ParentEnvironment) { |
| 229 | HGraph* graph = CreateGraph(); |
| 230 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 231 | graph->AddBlock(entry); |
| 232 | graph->SetEntryBlock(entry); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 233 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 234 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 235 | HInstruction* with_environment = new (GetAllocator()) HNullCheck(parameter1, 0); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 236 | entry->AddInstruction(parameter1); |
| 237 | entry->AddInstruction(with_environment); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 238 | entry->AddInstruction(new (GetAllocator()) HExit()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 239 | |
| 240 | ASSERT_TRUE(parameter1->HasUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 241 | ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 242 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 243 | HEnvironment* environment = new (GetAllocator()) HEnvironment( |
| 244 | GetAllocator(), 1, graph->GetArtMethod(), 0, with_environment); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 245 | HInstruction* const array[] = { parameter1 }; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 246 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 247 | environment->CopyFrom(ArrayRef<HInstruction* const>(array)); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 248 | with_environment->SetRawEnvironment(environment); |
| 249 | |
| 250 | ASSERT_TRUE(parameter1->HasEnvironmentUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 251 | ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 252 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 253 | HEnvironment* parent1 = new (GetAllocator()) HEnvironment( |
| 254 | GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 255 | parent1->CopyFrom(ArrayRef<HInstruction* const>(array)); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 256 | |
| 257 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u); |
| 258 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 259 | HEnvironment* parent2 = new (GetAllocator()) HEnvironment( |
| 260 | GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 261 | parent2->CopyFrom(ArrayRef<HInstruction* const>(array)); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 262 | parent1->SetAndCopyParentChain(GetAllocator(), parent2); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 263 | |
| 264 | // One use for parent2, and one other use for the new parent of parent1. |
| 265 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u); |
| 266 | |
| 267 | // We have copied the parent chain. So we now have two more uses. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 268 | environment->SetAndCopyParentChain(GetAllocator(), parent1); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 269 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u); |
| 270 | } |
| 271 | |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 272 | } // namespace art |