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