blob: 9bfd250ea4d110499e5e383ab8d56d80160f761e [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
Vladimir Markoca6fff82017-10-03 14:49:14 +010026class NodeTest : public OptimizingUnitTest {};
27
Nicolas Geoffray724c9632014-09-22 12:27:27 +010028/**
29 * Test that removing instruction from the graph removes itself from user lists
30 * and environment lists.
31 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010032TEST_F(NodeTest, RemoveInstruction) {
33 HGraph* graph = CreateGraph();
34 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010035 graph->AddBlock(entry);
36 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +010037 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010038 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010039 entry->AddInstruction(parameter);
Vladimir Markoca6fff82017-10-03 14:49:14 +010040 entry->AddInstruction(new (GetAllocator()) HGoto());
Nicolas Geoffray724c9632014-09-22 12:27:27 +010041
Vladimir Markoca6fff82017-10-03 14:49:14 +010042 HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010043 graph->AddBlock(first_block);
44 entry->AddSuccessor(first_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +010045 HInstruction* null_check = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010046 first_block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +010047 first_block->AddInstruction(new (GetAllocator()) HReturnVoid());
Nicolas Geoffray724c9632014-09-22 12:27:27 +010048
Vladimir Markoca6fff82017-10-03 14:49:14 +010049 HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010050 graph->AddBlock(exit_block);
51 first_block->AddSuccessor(exit_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +010052 exit_block->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray724c9632014-09-22 12:27:27 +010053
Vladimir Markoca6fff82017-10-03 14:49:14 +010054 HEnvironment* environment = new (GetAllocator()) HEnvironment(
55 GetAllocator(), 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 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010072TEST_F(NodeTest, InsertInstruction) {
73 HGraph* graph = CreateGraph();
74 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010075 graph->AddBlock(entry);
76 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +010077 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010078 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +010079 HInstruction* parameter2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010080 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010081 entry->AddInstruction(parameter1);
82 entry->AddInstruction(parameter2);
Vladimir Markoca6fff82017-10-03 14:49:14 +010083 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010084
85 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010086
Vladimir Markoca6fff82017-10-03 14:49:14 +010087 HInstruction* to_insert = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010088 entry->InsertInstructionBefore(to_insert, parameter2);
89
90 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +010091 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010092}
93
94/**
95 * Test that adding an instruction in the graph updates user lists.
96 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010097TEST_F(NodeTest, AddInstruction) {
98 HGraph* graph = CreateGraph();
99 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100100 graph->AddBlock(entry);
101 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100102 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100104 entry->AddInstruction(parameter);
105
106 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100107
Vladimir Markoca6fff82017-10-03 14:49:14 +0100108 HInstruction* to_add = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100109 entry->AddInstruction(to_add);
110
111 ASSERT_TRUE(parameter->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100112 ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100113}
114
Vladimir Markoca6fff82017-10-03 14:49:14 +0100115TEST_F(NodeTest, ParentEnvironment) {
116 HGraph* graph = CreateGraph();
117 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100118 graph->AddBlock(entry);
119 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100120 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100121 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100122 HInstruction* with_environment = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100123 entry->AddInstruction(parameter1);
124 entry->AddInstruction(with_environment);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100125 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100126
127 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100128 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100129
Vladimir Markoca6fff82017-10-03 14:49:14 +0100130 HEnvironment* environment = new (GetAllocator()) HEnvironment(
131 GetAllocator(), 1, graph->GetArtMethod(), 0, with_environment);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100132 HInstruction* const array[] = { parameter1 };
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100133
Vladimir Marko69d310e2017-10-09 14:12:23 +0100134 environment->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100135 with_environment->SetRawEnvironment(environment);
136
137 ASSERT_TRUE(parameter1->HasEnvironmentUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100138 ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139
Vladimir Markoca6fff82017-10-03 14:49:14 +0100140 HEnvironment* parent1 = new (GetAllocator()) HEnvironment(
141 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100142 parent1->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100143
144 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
145
Vladimir Markoca6fff82017-10-03 14:49:14 +0100146 HEnvironment* parent2 = new (GetAllocator()) HEnvironment(
147 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100148 parent2->CopyFrom(ArrayRef<HInstruction* const>(array));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 parent1->SetAndCopyParentChain(GetAllocator(), parent2);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100150
151 // One use for parent2, and one other use for the new parent of parent1.
152 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u);
153
154 // We have copied the parent chain. So we now have two more uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100155 environment->SetAndCopyParentChain(GetAllocator(), parent1);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100156 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
157}
158
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100159} // namespace art