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