blob: 2736453cccfa5d69994dc8bbed583d70f53dd034 [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);
37 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
38 entry->AddInstruction(parameter);
39 entry->AddInstruction(new (&allocator) HGoto());
40
41 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
42 graph->AddBlock(first_block);
43 entry->AddSuccessor(first_block);
44 HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0);
45 first_block->AddInstruction(null_check);
46 first_block->AddInstruction(new (&allocator) HReturnVoid());
47
48 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
49 graph->AddBlock(exit_block);
50 first_block->AddSuccessor(exit_block);
51 exit_block->AddInstruction(new (&allocator) HExit());
52
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010053 HEnvironment* environment = new (&allocator) HEnvironment(
54 &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +010055 null_check->SetRawEnvironment(environment);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010056 environment->SetRawEnvAt(0, parameter);
57 parameter->AddEnvUseAt(null_check->GetEnvironment(), 0);
58
59 ASSERT_TRUE(parameter->HasEnvironmentUses());
60 ASSERT_TRUE(parameter->HasUses());
61
62 first_block->RemoveInstruction(null_check);
63
64 ASSERT_FALSE(parameter->HasEnvironmentUses());
65 ASSERT_FALSE(parameter->HasUses());
66}
67
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010068/**
69 * Test that inserting an instruction in the graph updates user lists.
70 */
71TEST(Node, InsertInstruction) {
72 ArenaPool pool;
73 ArenaAllocator allocator(&pool);
74
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010075 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010076 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
77 graph->AddBlock(entry);
78 graph->SetEntryBlock(entry);
79 HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
80 HInstruction* parameter2 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
81 entry->AddInstruction(parameter1);
82 entry->AddInstruction(parameter2);
83 entry->AddInstruction(new (&allocator) HExit());
84
85 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010086
87 HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
88 entry->InsertInstructionBefore(to_insert, parameter2);
89
90 ASSERT_TRUE(parameter1->HasUses());
David Brazdilea55b932015-01-27 17:12:29 +000091 ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010092}
93
94/**
95 * Test that adding an instruction in the graph updates user lists.
96 */
97TEST(Node, AddInstruction) {
98 ArenaPool pool;
99 ArenaAllocator allocator(&pool);
100
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100101 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100102 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
103 graph->AddBlock(entry);
104 graph->SetEntryBlock(entry);
105 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
106 entry->AddInstruction(parameter);
107
108 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100109
110 HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
111 entry->AddInstruction(to_add);
112
113 ASSERT_TRUE(parameter->HasUses());
David Brazdilea55b932015-01-27 17:12:29 +0000114 ASSERT_TRUE(parameter->GetUses().HasOnlyOneUse());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100115}
116
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100117TEST(Node, ParentEnvironment) {
118 ArenaPool pool;
119 ArenaAllocator allocator(&pool);
120
121 HGraph* graph = CreateGraph(&allocator);
122 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
123 graph->AddBlock(entry);
124 graph->SetEntryBlock(entry);
125 HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
126 HInstruction* with_environment = new (&allocator) HNullCheck(parameter1, 0);
127 entry->AddInstruction(parameter1);
128 entry->AddInstruction(with_environment);
129 entry->AddInstruction(new (&allocator) HExit());
130
131 ASSERT_TRUE(parameter1->HasUses());
132 ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse());
133
134 HEnvironment* environment = new (&allocator) HEnvironment(
135 &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0);
136 GrowableArray<HInstruction*> array(&allocator, 1);
137 array.Add(parameter1);
138
139 environment->CopyFrom(array);
140 with_environment->SetRawEnvironment(environment);
141
142 ASSERT_TRUE(parameter1->HasEnvironmentUses());
143 ASSERT_TRUE(parameter1->GetEnvUses().HasOnlyOneUse());
144
145 HEnvironment* parent1 = new (&allocator) HEnvironment(
146 &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0);
147 parent1->CopyFrom(array);
148
149 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
150
151 HEnvironment* parent2 = new (&allocator) HEnvironment(
152 &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0);
153 parent2->CopyFrom(array);
154 parent1->SetAndCopyParentChain(&allocator, parent2);
155
156 // One use for parent2, and one other use for the new parent of parent1.
157 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u);
158
159 // We have copied the parent chain. So we now have two more uses.
160 environment->SetAndCopyParentChain(&allocator, parent1);
161 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
162}
163
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100164} // namespace art