blob: 29210fe10fab38aa358b486e3ce4a2acc31b3e08 [file] [log] [blame]
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001/*
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 Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include "base/arena_allocator.h"
Vladimír Marko434d9682022-11-04 14:04:17 +000020#include "base/macros.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010021#include "optimizing_unit_test.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010022
23#include "gtest/gtest.h"
24
Vladimír Marko434d9682022-11-04 14:04:17 +000025namespace art HIDDEN {
Nicolas Geoffray724c9632014-09-22 12:27:27 +010026
Vladimir Markoca6fff82017-10-03 14:49:14 +010027class NodeTest : public OptimizingUnitTest {};
28
Nicolas Geoffray724c9632014-09-22 12:27:27 +010029/**
Alex Light210a78d2020-11-30 16:58:05 -080030 * 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 */
38TEST_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 */
94TEST_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 Geoffray724c9632014-09-22 12:27:27 +0100142 * Test that removing instruction from the graph removes itself from user lists
143 * and environment lists.
144 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100145TEST_F(NodeTest, RemoveInstruction) {
146 HGraph* graph = CreateGraph();
147 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100148 graph->AddBlock(entry);
149 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100150 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100152 entry->AddInstruction(parameter);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100153 entry->AddInstruction(new (GetAllocator()) HGoto());
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100154
Vladimir Markoca6fff82017-10-03 14:49:14 +0100155 HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100156 graph->AddBlock(first_block);
157 entry->AddSuccessor(first_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 HInstruction* null_check = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100159 first_block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100160 first_block->AddInstruction(new (GetAllocator()) HReturnVoid());
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100161
Vladimir Markoca6fff82017-10-03 14:49:14 +0100162 HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100163 graph->AddBlock(exit_block);
164 first_block->AddSuccessor(exit_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100165 exit_block->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100166
Vladimir Markoca6fff82017-10-03 14:49:14 +0100167 HEnvironment* environment = new (GetAllocator()) HEnvironment(
168 GetAllocator(), 1, graph->GetArtMethod(), 0, null_check);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +0100169 null_check->SetRawEnvironment(environment);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100170 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 Geoffray191c4b12014-10-07 14:14:27 +0100182/**
183 * Test that inserting an instruction in the graph updates user lists.
184 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100185TEST_F(NodeTest, InsertInstruction) {
186 HGraph* graph = CreateGraph();
187 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100188 graph->AddBlock(entry);
189 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100190 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100191 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100192 HInstruction* parameter2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100193 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100194 entry->AddInstruction(parameter1);
195 entry->AddInstruction(parameter2);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100196 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100197
198 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100199
Vladimir Markoca6fff82017-10-03 14:49:14 +0100200 HInstruction* to_insert = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100201 entry->InsertInstructionBefore(to_insert, parameter2);
202
203 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100204 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100205}
206
207/**
208 * Test that adding an instruction in the graph updates user lists.
209 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100210TEST_F(NodeTest, AddInstruction) {
211 HGraph* graph = CreateGraph();
212 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100213 graph->AddBlock(entry);
214 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100215 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100216 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100217 entry->AddInstruction(parameter);
218
219 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100220
Vladimir Markoca6fff82017-10-03 14:49:14 +0100221 HInstruction* to_add = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100222 entry->AddInstruction(to_add);
223
224 ASSERT_TRUE(parameter->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100225 ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100226}
227
Vladimir Markoca6fff82017-10-03 14:49:14 +0100228TEST_F(NodeTest, ParentEnvironment) {
229 HGraph* graph = CreateGraph();
230 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100231 graph->AddBlock(entry);
232 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100233 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100234 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100235 HInstruction* with_environment = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100236 entry->AddInstruction(parameter1);
237 entry->AddInstruction(with_environment);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100238 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100239
240 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100241 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100242
Vladimir Markoca6fff82017-10-03 14:49:14 +0100243 HEnvironment* environment = new (GetAllocator()) HEnvironment(
244 GetAllocator(), 1, graph->GetArtMethod(), 0, with_environment);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100245 HInstruction* const array[] = { parameter1 };
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100246
Vladimir Marko69d310e2017-10-09 14:12:23 +0100247 environment->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100248 with_environment->SetRawEnvironment(environment);
249
250 ASSERT_TRUE(parameter1->HasEnvironmentUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100251 ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100252
Vladimir Markoca6fff82017-10-03 14:49:14 +0100253 HEnvironment* parent1 = new (GetAllocator()) HEnvironment(
254 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100255 parent1->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100256
257 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
258
Vladimir Markoca6fff82017-10-03 14:49:14 +0100259 HEnvironment* parent2 = new (GetAllocator()) HEnvironment(
260 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100261 parent2->CopyFrom(ArrayRef<HInstruction* const>(array));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100262 parent1->SetAndCopyParentChain(GetAllocator(), parent2);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100263
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 Markoca6fff82017-10-03 14:49:14 +0100268 environment->SetAndCopyParentChain(GetAllocator(), parent1);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100269 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
270}
271
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100272} // namespace art