blob: d012c3294ac8392a6d5ada45ab282fceb5bb88b5 [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"
Vladimir Markoe2727152019-10-10 10:46:42 +010020#include "base/macros.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010021#include "optimizing_unit_test.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010022
23#include "gtest/gtest.h"
24
Vladimir Markoe2727152019-10-10 10:46:42 +010025namespace art HIDDEN {
Nicolas Geoffray724c9632014-09-22 12:27:27 +010026
Vladimir Markoca6fff82017-10-03 14:49:14 +010027class NodeTest : public OptimizingUnitTest {};
28
Nicolas Geoffray724c9632014-09-22 12:27:27 +010029/**
30 * Test that removing instruction from the graph removes itself from user lists
31 * and environment lists.
32 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010033TEST_F(NodeTest, RemoveInstruction) {
34 HGraph* graph = CreateGraph();
35 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010036 graph->AddBlock(entry);
37 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +010038 HInstruction* parameter = new (GetAllocator()) 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);
Vladimir Markoca6fff82017-10-03 14:49:14 +010041 entry->AddInstruction(new (GetAllocator()) HGoto());
Nicolas Geoffray724c9632014-09-22 12:27:27 +010042
Vladimir Markoca6fff82017-10-03 14:49:14 +010043 HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010044 graph->AddBlock(first_block);
45 entry->AddSuccessor(first_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +010046 HInstruction* null_check = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010047 first_block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +010048 first_block->AddInstruction(new (GetAllocator()) HReturnVoid());
Nicolas Geoffray724c9632014-09-22 12:27:27 +010049
Vladimir Markoca6fff82017-10-03 14:49:14 +010050 HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010051 graph->AddBlock(exit_block);
52 first_block->AddSuccessor(exit_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +010053 exit_block->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray724c9632014-09-22 12:27:27 +010054
Vladimir Markoca6fff82017-10-03 14:49:14 +010055 HEnvironment* environment = new (GetAllocator()) HEnvironment(
56 GetAllocator(), 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 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010073TEST_F(NodeTest, InsertInstruction) {
74 HGraph* graph = CreateGraph();
75 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010076 graph->AddBlock(entry);
77 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +010078 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +010080 HInstruction* parameter2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010082 entry->AddInstruction(parameter1);
83 entry->AddInstruction(parameter2);
Vladimir Markoca6fff82017-10-03 14:49:14 +010084 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010085
86 ASSERT_FALSE(parameter1->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010087
Vladimir Markoca6fff82017-10-03 14:49:14 +010088 HInstruction* to_insert = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010089 entry->InsertInstructionBefore(to_insert, parameter2);
90
91 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +010092 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010093}
94
95/**
96 * Test that adding an instruction in the graph updates user lists.
97 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010098TEST_F(NodeTest, AddInstruction) {
99 HGraph* graph = CreateGraph();
100 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100101 graph->AddBlock(entry);
102 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100103 HInstruction* parameter = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100104 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100105 entry->AddInstruction(parameter);
106
107 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100108
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109 HInstruction* to_add = new (GetAllocator()) HNullCheck(parameter, 0);
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100110 entry->AddInstruction(to_add);
111
112 ASSERT_TRUE(parameter->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100113 ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100114}
115
Vladimir Markoca6fff82017-10-03 14:49:14 +0100116TEST_F(NodeTest, ParentEnvironment) {
117 HGraph* graph = CreateGraph();
118 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100119 graph->AddBlock(entry);
120 graph->SetEntryBlock(entry);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100121 HInstruction* parameter1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100123 HInstruction* with_environment = new (GetAllocator()) HNullCheck(parameter1, 0);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100124 entry->AddInstruction(parameter1);
125 entry->AddInstruction(with_environment);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100126 entry->AddInstruction(new (GetAllocator()) HExit());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100127
128 ASSERT_TRUE(parameter1->HasUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100129 ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100130
Vladimir Markoca6fff82017-10-03 14:49:14 +0100131 HEnvironment* environment = new (GetAllocator()) HEnvironment(
132 GetAllocator(), 1, graph->GetArtMethod(), 0, with_environment);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100133 HInstruction* const array[] = { parameter1 };
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100134
Vladimir Marko69d310e2017-10-09 14:12:23 +0100135 environment->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100136 with_environment->SetRawEnvironment(environment);
137
138 ASSERT_TRUE(parameter1->HasEnvironmentUses());
Vladimir Marko46817b82016-03-29 12:21:58 +0100139 ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100140
Vladimir Markoca6fff82017-10-03 14:49:14 +0100141 HEnvironment* parent1 = new (GetAllocator()) HEnvironment(
142 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100143 parent1->CopyFrom(ArrayRef<HInstruction* const>(array));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100144
145 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
146
Vladimir Markoca6fff82017-10-03 14:49:14 +0100147 HEnvironment* parent2 = new (GetAllocator()) HEnvironment(
148 GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100149 parent2->CopyFrom(ArrayRef<HInstruction* const>(array));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100150 parent1->SetAndCopyParentChain(GetAllocator(), parent2);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100151
152 // One use for parent2, and one other use for the new parent of parent1.
153 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u);
154
155 // We have copied the parent chain. So we now have two more uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100156 environment->SetAndCopyParentChain(GetAllocator(), parent1);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100157 ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
158}
159
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100160} // namespace art