blob: 7686ba851b648c324da647712fd7eb0ee9110ad1 [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010018#include "nodes.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010019#include "optimizing_unit_test.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010020
21#include "gtest/gtest.h"
22
23namespace art {
24
25/**
26 * Test that removing instruction from the graph removes itself from user lists
27 * and environment lists.
28 */
29TEST(Node, RemoveInstruction) {
30 ArenaPool pool;
31 ArenaAllocator allocator(&pool);
32
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010033 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010034 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
35 graph->AddBlock(entry);
36 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +000037 HInstruction* parameter = new (&allocator) HParameterValue(
Andreas Gampea5b09a62016-11-17 15:21:22 -080038 graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010039 entry->AddInstruction(parameter);
40 entry->AddInstruction(new (&allocator) HGoto());
41
42 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
43 graph->AddBlock(first_block);
44 entry->AddSuccessor(first_block);
45 HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0);
46 first_block->AddInstruction(null_check);
47 first_block->AddInstruction(new (&allocator) HReturnVoid());
48
49 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
50 graph->AddBlock(exit_block);
51 first_block->AddSuccessor(exit_block);
52 exit_block->AddInstruction(new (&allocator) HExit());
53
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010054 HEnvironment* environment = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000055 &allocator, 1, graph->GetArtMethod(), 0, null_check);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +010056 null_check->SetRawEnvironment(environment);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010057 environment->SetRawEnvAt(0, parameter);
58 parameter->AddEnvUseAt(null_check->GetEnvironment(), 0);
59
60 ASSERT_TRUE(parameter->HasEnvironmentUses());
61 ASSERT_TRUE(parameter->HasUses());
62
63 first_block->RemoveInstruction(null_check);
64
65 ASSERT_FALSE(parameter->HasEnvironmentUses());
66 ASSERT_FALSE(parameter->HasUses());
67}
68
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010069/**
70 * Test that inserting an instruction in the graph updates user lists.
71 */
72TEST(Node, InsertInstruction) {
73 ArenaPool pool;
74 ArenaAllocator allocator(&pool);
75
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010076 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010077 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
78 graph->AddBlock(entry);
79 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +000080 HInstruction* parameter1 = new (&allocator) HParameterValue(
Andreas Gampea5b09a62016-11-17 15:21:22 -080081 graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
Calin Juravlee6e3bea2015-10-14 13:53:10 +000082 HInstruction* parameter2 = new (&allocator) HParameterValue(
Andreas Gampea5b09a62016-11-17 15:21:22 -080083 graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010084 entry->AddInstruction(parameter1);
85 entry->AddInstruction(parameter2);
86 entry->AddInstruction(new (&allocator) HExit());
87
88 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010089
90 HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
91 entry->InsertInstructionBefore(to_insert, parameter2);
92
93 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +010094 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010095}
96
97/**
98 * Test that adding an instruction in the graph updates user lists.
99 */
100TEST(Node, AddInstruction) {
101 ArenaPool pool;
102 ArenaAllocator allocator(&pool);
103
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100104 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100105 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
106 graph->AddBlock(entry);
107 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000108 HInstruction* parameter = new (&allocator) HParameterValue(
Andreas Gampea5b09a62016-11-17 15:21:22 -0800109 graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100110 entry->AddInstruction(parameter);
111
112 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100113
114 HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
115 entry->AddInstruction(to_add);
116
117 ASSERT_TRUE(parameter->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100118 ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100119}
120
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100121TEST(Node, ParentEnvironment) {
122 ArenaPool pool;
123 ArenaAllocator allocator(&pool);
124
125 HGraph* graph = CreateGraph(&allocator);
126 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
127 graph->AddBlock(entry);
128 graph->SetEntryBlock(entry);
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000129 HInstruction* parameter1 = new (&allocator) HParameterValue(
Andreas Gampea5b09a62016-11-17 15:21:22 -0800130 graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100131 HInstruction* with_environment = new (&allocator) HNullCheck(parameter1, 0);
132 entry->AddInstruction(parameter1);
133 entry->AddInstruction(with_environment);
134 entry->AddInstruction(new (&allocator) HExit());
135
136 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100137 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100138
139 HEnvironment* environment = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000140 &allocator, 1, graph->GetArtMethod(), 0, with_environment);
Vladimir Marko71bf8092015-09-15 15:33:14 +0100141 ArenaVector<HInstruction*> array(allocator.Adapter());
142 array.push_back(parameter1);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100143
144 environment->CopyFrom(array);
145 with_environment->SetRawEnvironment(environment);
146
147 ASSERT_TRUE(parameter1->HasEnvironmentUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100148 ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149
150 HEnvironment* parent1 = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000151 &allocator, 1, graph->GetArtMethod(), 0, nullptr);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100152 parent1->CopyFrom(array);
153
154 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
155
156 HEnvironment* parent2 = new (&allocator) HEnvironment(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000157 &allocator, 1, graph->GetArtMethod(), 0, nullptr);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100158 parent2->CopyFrom(array);
159 parent1->SetAndCopyParentChain(&allocator, parent2);
160
161 // One use for parent2, and one other use for the new parent of parent1.
162 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u);
163
164 // We have copied the parent chain. So we now have two more uses.
165 environment->SetAndCopyParentChain(&allocator, parent1);
166 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
167}
168
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100169} // namespace art