blob: 34f0e9b1e1e58f4af1b7610f9cb075d31eaff813 [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"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010020#include "optimizing_unit_test.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010021
22#include "gtest/gtest.h"
23
Vladimir Marko0a516052019-10-14 13:00:44 +000024namespace art {
Nicolas Geoffray724c9632014-09-22 12:27:27 +010025
Vladimir Markoca6fff82017-10-03 14:49:14 +010026class NodeTest : public OptimizingUnitTest {};
27
Nicolas Geoffray724c9632014-09-22 12:27:27 +010028/**
Alex Light210a78d2020-11-30 16:58:05 -080029 * 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 */
37TEST_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 */
93TEST_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 Geoffray724c9632014-09-22 12:27:27 +0100141 * Test that removing instruction from the graph removes itself from user lists
142 * and environment lists.
143 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100144TEST_F(NodeTest, RemoveInstruction) {
145 HGraph* graph = CreateGraph();
146 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100147 graph->AddBlock(entry);
148 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100151 entry->AddInstruction(parameter);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100152 entry->AddInstruction(new (GetAllocator()) HGoto());
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100153
Vladimir Markoca6fff82017-10-03 14:49:14 +0100154 HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100155 graph->AddBlock(first_block);
156 entry->AddSuccessor(first_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100157 HInstruction* null_check = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100158 first_block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 first_block->AddInstruction(new (GetAllocator()) HReturnVoid());
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100160
Vladimir Markoca6fff82017-10-03 14:49:14 +0100161 HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100162 graph->AddBlock(exit_block);
163 first_block->AddSuccessor(exit_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100164 exit_block->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100165
Vladimir Markoca6fff82017-10-03 14:49:14 +0100166 HEnvironment* environment = new (GetAllocator()) HEnvironment(
167 GetAllocator(), 1, graph->GetArtMethod(), 0, null_check);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +0100168 null_check->SetRawEnvironment(environment);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100169 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 Geoffray191c4b12014-10-07 14:14:27 +0100181/**
182 * Test that inserting an instruction in the graph updates user lists.
183 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100184TEST_F(NodeTest, InsertInstruction) {
185 HGraph* graph = CreateGraph();
186 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100187 graph->AddBlock(entry);
188 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100189 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100190 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100191 HInstruction* parameter2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100192 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100193 entry->AddInstruction(parameter1);
194 entry->AddInstruction(parameter2);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100195 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100196
197 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100198
Vladimir Markoca6fff82017-10-03 14:49:14 +0100199 HInstruction* to_insert = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100200 entry->InsertInstructionBefore(to_insert, parameter2);
201
202 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100203 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100204}
205
206/**
207 * Test that adding an instruction in the graph updates user lists.
208 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100209TEST_F(NodeTest, AddInstruction) {
210 HGraph* graph = CreateGraph();
211 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100212 graph->AddBlock(entry);
213 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100214 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100215 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100216 entry->AddInstruction(parameter);
217
218 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100219
Vladimir Markoca6fff82017-10-03 14:49:14 +0100220 HInstruction* to_add = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100221 entry->AddInstruction(to_add);
222
223 ASSERT_TRUE(parameter->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100224 ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100225}
226
Vladimir Markoca6fff82017-10-03 14:49:14 +0100227TEST_F(NodeTest, ParentEnvironment) {
228 HGraph* graph = CreateGraph();
229 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100230 graph->AddBlock(entry);
231 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100232 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100233 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100234 HInstruction* with_environment = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100235 entry->AddInstruction(parameter1);
236 entry->AddInstruction(with_environment);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100237 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100238
239 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100240 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100241
Vladimir Markoca6fff82017-10-03 14:49:14 +0100242 HEnvironment* environment = new (GetAllocator()) HEnvironment(
243 GetAllocator(), 1, graph->GetArtMethod(), 0, with_environment);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100244 HInstruction* const array[] = { parameter1 };
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100245
Vladimir Marko69d310e2017-10-09 14:12:23 +0100246 environment->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100247 with_environment->SetRawEnvironment(environment);
248
249 ASSERT_TRUE(parameter1->HasEnvironmentUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100250 ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100251
Vladimir Markoca6fff82017-10-03 14:49:14 +0100252 HEnvironment* parent1 = new (GetAllocator()) HEnvironment(
253 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100254 parent1->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100255
256 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
257
Vladimir Markoca6fff82017-10-03 14:49:14 +0100258 HEnvironment* parent2 = new (GetAllocator()) HEnvironment(
259 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100260 parent2->CopyFrom(ArrayRef<HInstruction* const>(array));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100261 parent1->SetAndCopyParentChain(GetAllocator(), parent2);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100262
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 Markoca6fff82017-10-03 14:49:14 +0100267 environment->SetAndCopyParentChain(GetAllocator(), parent1);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100268 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
269}
270
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100271} // namespace art