| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 18 | |
| 19 | #include "base/arena_allocator.h" |
| Vladimir Marko | e272715 | 2019-10-10 10:46:42 +0100 | [diff] [blame^] | 20 | #include "base/macros.h" |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 21 | #include "optimizing_unit_test.h" |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 22 | |
| 23 | #include "gtest/gtest.h" |
| 24 | |
| Vladimir Marko | e272715 | 2019-10-10 10:46:42 +0100 | [diff] [blame^] | 25 | namespace art HIDDEN { |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 26 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 27 | class NodeTest : public OptimizingUnitTest {}; |
| 28 | |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 29 | /** |
| 30 | * Test that removing instruction from the graph removes itself from user lists |
| 31 | * and environment lists. |
| 32 | */ |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 33 | TEST_F(NodeTest, RemoveInstruction) { |
| 34 | HGraph* graph = CreateGraph(); |
| 35 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 36 | graph->AddBlock(entry); |
| 37 | graph->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 38 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 39 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 40 | entry->AddInstruction(parameter); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 41 | entry->AddInstruction(new (GetAllocator()) HGoto()); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 42 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 43 | HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 44 | graph->AddBlock(first_block); |
| 45 | entry->AddSuccessor(first_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 46 | HInstruction* null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 47 | first_block->AddInstruction(null_check); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 48 | first_block->AddInstruction(new (GetAllocator()) HReturnVoid()); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 49 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 50 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 51 | graph->AddBlock(exit_block); |
| 52 | first_block->AddSuccessor(exit_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 53 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 54 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 55 | HEnvironment* environment = new (GetAllocator()) HEnvironment( |
| 56 | GetAllocator(), 1, graph->GetArtMethod(), 0, null_check); |
| Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 57 | null_check->SetRawEnvironment(environment); |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 58 | 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 Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 70 | /** |
| 71 | * Test that inserting an instruction in the graph updates user lists. |
| 72 | */ |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 73 | TEST_F(NodeTest, InsertInstruction) { |
| 74 | HGraph* graph = CreateGraph(); |
| 75 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 76 | graph->AddBlock(entry); |
| 77 | graph->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 78 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 79 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 80 | HInstruction* parameter2 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 81 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 82 | entry->AddInstruction(parameter1); |
| 83 | entry->AddInstruction(parameter2); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 84 | entry->AddInstruction(new (GetAllocator()) HExit()); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 85 | |
| 86 | ASSERT_FALSE(parameter1->HasUses()); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 87 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 88 | HInstruction* to_insert = new (GetAllocator()) HNullCheck(parameter1, 0); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 89 | entry->InsertInstructionBefore(to_insert, parameter2); |
| 90 | |
| 91 | ASSERT_TRUE(parameter1->HasUses()); |
| Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 92 | ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement()); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Test that adding an instruction in the graph updates user lists. |
| 97 | */ |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 98 | TEST_F(NodeTest, AddInstruction) { |
| 99 | HGraph* graph = CreateGraph(); |
| 100 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 101 | graph->AddBlock(entry); |
| 102 | graph->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 103 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 104 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 105 | entry->AddInstruction(parameter); |
| 106 | |
| 107 | ASSERT_FALSE(parameter->HasUses()); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 108 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 109 | HInstruction* to_add = new (GetAllocator()) HNullCheck(parameter, 0); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 110 | entry->AddInstruction(to_add); |
| 111 | |
| 112 | ASSERT_TRUE(parameter->HasUses()); |
| Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 113 | ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement()); |
| Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 116 | TEST_F(NodeTest, ParentEnvironment) { |
| 117 | HGraph* graph = CreateGraph(); |
| 118 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 119 | graph->AddBlock(entry); |
| 120 | graph->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 121 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 122 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 123 | HInstruction* with_environment = new (GetAllocator()) HNullCheck(parameter1, 0); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 124 | entry->AddInstruction(parameter1); |
| 125 | entry->AddInstruction(with_environment); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 126 | entry->AddInstruction(new (GetAllocator()) HExit()); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 127 | |
| 128 | ASSERT_TRUE(parameter1->HasUses()); |
| Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 129 | ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement()); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 130 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 131 | HEnvironment* environment = new (GetAllocator()) HEnvironment( |
| 132 | GetAllocator(), 1, graph->GetArtMethod(), 0, with_environment); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 133 | HInstruction* const array[] = { parameter1 }; |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 134 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 135 | environment->CopyFrom(ArrayRef<HInstruction* const>(array)); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 136 | with_environment->SetRawEnvironment(environment); |
| 137 | |
| 138 | ASSERT_TRUE(parameter1->HasEnvironmentUses()); |
| Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 139 | ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement()); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 140 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 141 | HEnvironment* parent1 = new (GetAllocator()) HEnvironment( |
| 142 | GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 143 | parent1->CopyFrom(ArrayRef<HInstruction* const>(array)); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 144 | |
| 145 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u); |
| 146 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 147 | HEnvironment* parent2 = new (GetAllocator()) HEnvironment( |
| 148 | GetAllocator(), 1, graph->GetArtMethod(), 0, nullptr); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 149 | parent2->CopyFrom(ArrayRef<HInstruction* const>(array)); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 150 | parent1->SetAndCopyParentChain(GetAllocator(), parent2); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 151 | |
| 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 Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 156 | environment->SetAndCopyParentChain(GetAllocator(), parent1); |
| Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 157 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u); |
| 158 | } |
| 159 | |
| Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 160 | } // namespace art |