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 | |
| 26 | /** |
| 27 | * Test that removing instruction from the graph removes itself from user lists |
| 28 | * and environment lists. |
| 29 | */ |
| 30 | TEST(Node, RemoveInstruction) { |
| 31 | ArenaPool pool; |
| 32 | ArenaAllocator allocator(&pool); |
| 33 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 34 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 35 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 36 | graph->AddBlock(entry); |
| 37 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 38 | HInstruction* parameter = new (&allocator) HParameterValue( |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 39 | graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 40 | entry->AddInstruction(parameter); |
| 41 | entry->AddInstruction(new (&allocator) HGoto()); |
| 42 | |
| 43 | HBasicBlock* first_block = new (&allocator) HBasicBlock(graph); |
| 44 | graph->AddBlock(first_block); |
| 45 | entry->AddSuccessor(first_block); |
| 46 | HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0); |
| 47 | first_block->AddInstruction(null_check); |
| 48 | first_block->AddInstruction(new (&allocator) HReturnVoid()); |
| 49 | |
| 50 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 51 | graph->AddBlock(exit_block); |
| 52 | first_block->AddSuccessor(exit_block); |
| 53 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 54 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 55 | HEnvironment* environment = new (&allocator) HEnvironment( |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 56 | &allocator, 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 | */ |
| 73 | TEST(Node, InsertInstruction) { |
| 74 | ArenaPool pool; |
| 75 | ArenaAllocator allocator(&pool); |
| 76 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 77 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 78 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 79 | graph->AddBlock(entry); |
| 80 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 81 | HInstruction* parameter1 = new (&allocator) HParameterValue( |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 82 | graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 83 | HInstruction* parameter2 = new (&allocator) HParameterValue( |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 84 | graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 85 | entry->AddInstruction(parameter1); |
| 86 | entry->AddInstruction(parameter2); |
| 87 | entry->AddInstruction(new (&allocator) HExit()); |
| 88 | |
| 89 | ASSERT_FALSE(parameter1->HasUses()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 90 | |
| 91 | HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0); |
| 92 | entry->InsertInstructionBefore(to_insert, parameter2); |
| 93 | |
| 94 | ASSERT_TRUE(parameter1->HasUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 95 | ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Test that adding an instruction in the graph updates user lists. |
| 100 | */ |
| 101 | TEST(Node, AddInstruction) { |
| 102 | ArenaPool pool; |
| 103 | ArenaAllocator allocator(&pool); |
| 104 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 105 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 106 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 107 | graph->AddBlock(entry); |
| 108 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 109 | HInstruction* parameter = new (&allocator) HParameterValue( |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 110 | graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 111 | entry->AddInstruction(parameter); |
| 112 | |
| 113 | ASSERT_FALSE(parameter->HasUses()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 114 | |
| 115 | HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0); |
| 116 | entry->AddInstruction(to_add); |
| 117 | |
| 118 | ASSERT_TRUE(parameter->HasUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 119 | ASSERT_TRUE(parameter->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 122 | TEST(Node, ParentEnvironment) { |
| 123 | ArenaPool pool; |
| 124 | ArenaAllocator allocator(&pool); |
| 125 | |
| 126 | HGraph* graph = CreateGraph(&allocator); |
| 127 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 128 | graph->AddBlock(entry); |
| 129 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 130 | HInstruction* parameter1 = new (&allocator) HParameterValue( |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 131 | graph->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 132 | HInstruction* with_environment = new (&allocator) HNullCheck(parameter1, 0); |
| 133 | entry->AddInstruction(parameter1); |
| 134 | entry->AddInstruction(with_environment); |
| 135 | entry->AddInstruction(new (&allocator) HExit()); |
| 136 | |
| 137 | ASSERT_TRUE(parameter1->HasUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 138 | ASSERT_TRUE(parameter1->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 139 | |
| 140 | HEnvironment* environment = new (&allocator) HEnvironment( |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 141 | &allocator, 1, graph->GetArtMethod(), 0, with_environment); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 142 | ArenaVector<HInstruction*> array(allocator.Adapter()); |
| 143 | array.push_back(parameter1); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 144 | |
| 145 | environment->CopyFrom(array); |
| 146 | with_environment->SetRawEnvironment(environment); |
| 147 | |
| 148 | ASSERT_TRUE(parameter1->HasEnvironmentUses()); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 149 | ASSERT_TRUE(parameter1->GetEnvUses().HasExactlyOneElement()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 150 | |
| 151 | HEnvironment* parent1 = new (&allocator) HEnvironment( |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 152 | &allocator, 1, graph->GetArtMethod(), 0, nullptr); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 153 | parent1->CopyFrom(array); |
| 154 | |
| 155 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u); |
| 156 | |
| 157 | HEnvironment* parent2 = new (&allocator) HEnvironment( |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 158 | &allocator, 1, graph->GetArtMethod(), 0, nullptr); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 159 | parent2->CopyFrom(array); |
| 160 | parent1->SetAndCopyParentChain(&allocator, parent2); |
| 161 | |
| 162 | // One use for parent2, and one other use for the new parent of parent1. |
| 163 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u); |
| 164 | |
| 165 | // We have copied the parent chain. So we now have two more uses. |
| 166 | environment->SetAndCopyParentChain(&allocator, parent1); |
| 167 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u); |
| 168 | } |
| 169 | |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 170 | } // namespace art |