blob: ada6177bfb64d25ae69682c463287432fd9a9518 [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
24namespace art {
25
26/**
27 * Test that removing instruction from the graph removes itself from user lists
28 * and environment lists.
29 */
30TEST(Node, RemoveInstruction) {
31 ArenaPool pool;
32 ArenaAllocator allocator(&pool);
33
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010034 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010035 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
36 graph->AddBlock(entry);
37 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +000038 HInstruction* parameter = new (&allocator) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010039 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010040 entry->AddInstruction(parameter);
41 entry->AddInstruction(new (&allocator) HGoto());
42
43 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
44 graph->AddBlock(first_block);
45 entry->AddSuccessor(first_block);
46 HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0);
47 first_block->AddInstruction(null_check);
48 first_block->AddInstruction(new (&allocator) HReturnVoid());
49
50 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
51 graph->AddBlock(exit_block);
52 first_block->AddSuccessor(exit_block);
53 exit_block->AddInstruction(new (&allocator) HExit());
54
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010055 HEnvironment* environment = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000056 &allocator, 1, graph->GetArtMethod(), 0, null_check);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +010057 null_check->SetRawEnvironment(environment);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010058 environment->SetRawEnvAt(0, parameter);
59 parameter->AddEnvUseAt(null_check->GetEnvironment(), 0);
60
61 ASSERT_TRUE(parameter->HasEnvironmentUses());
62 ASSERT_TRUE(parameter->HasUses());
63
64 first_block->RemoveInstruction(null_check);
65
66 ASSERT_FALSE(parameter->HasEnvironmentUses());
67 ASSERT_FALSE(parameter->HasUses());
68}
69
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010070/**
71 * Test that inserting an instruction in the graph updates user lists.
72 */
73TEST(Node, InsertInstruction) {
74 ArenaPool pool;
75 ArenaAllocator allocator(&pool);
76
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010077 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010078 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
79 graph->AddBlock(entry);
80 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +000081 HInstruction* parameter1 = new (&allocator) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010082 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Calin Juravlee6e3bea2015-10-14 13:53:10 +000083 HInstruction* parameter2 = new (&allocator) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010085 entry->AddInstruction(parameter1);
86 entry->AddInstruction(parameter2);
87 entry->AddInstruction(new (&allocator) HExit());
88
89 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010090
91 HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
92 entry->InsertInstructionBefore(to_insert, parameter2);
93
94 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +010095 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010096}
97
98/**
99 * Test that adding an instruction in the graph updates user lists.
100 */
101TEST(Node, AddInstruction) {
102 ArenaPool pool;
103 ArenaAllocator allocator(&pool);
104
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100105 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100106 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
107 graph->AddBlock(entry);
108 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000109 HInstruction* parameter = new (&allocator) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100110 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100111 entry->AddInstruction(parameter);
112
113 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100114
115 HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
116 entry->AddInstruction(to_add);
117
118 ASSERT_TRUE(parameter->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100119 ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100120}
121
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100122TEST(Node, ParentEnvironment) {
123 ArenaPool pool;
124 ArenaAllocator allocator(&pool);
125
126 HGraph* graph = CreateGraph(&allocator);
127 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
128 graph->AddBlock(entry);
129 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000130 HInstruction* parameter1 = new (&allocator) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100131 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100132 HInstruction* with_environment = new (&allocator) HNullCheck(parameter1, 0);
133 entry->AddInstruction(parameter1);
134 entry->AddInstruction(with_environment);
135 entry->AddInstruction(new (&allocator) HExit());
136
137 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100138 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139
140 HEnvironment* environment = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000141 &allocator, 1, graph->GetArtMethod(), 0, with_environment);
Vladimir Marko71bf8092015-09-15 15:33:14 +0100142 ArenaVector<HInstruction*> array(allocator.Adapter());
143 array.push_back(parameter1);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100144
145 environment->CopyFrom(array);
146 with_environment->SetRawEnvironment(environment);
147
148 ASSERT_TRUE(parameter1->HasEnvironmentUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100149 ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100150
151 HEnvironment* parent1 = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000152 &allocator, 1, graph->GetArtMethod(), 0, nullptr);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100153 parent1->CopyFrom(array);
154
155 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
156
157 HEnvironment* parent2 = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000158 &allocator, 1, graph->GetArtMethod(), 0, nullptr);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100159 parent2->CopyFrom(array);
160 parent1->SetAndCopyParentChain(&allocator, parent2);
161
162 // One use for parent2, and one other use for the new parent of parent1.
163 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u);
164
165 // We have copied the parent chain. So we now have two more uses.
166 environment->SetAndCopyParentChain(&allocator, parent1);
167 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
168}
169
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100170} // namespace art