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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #include "base/arena_allocator.h" |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 18 | #include "nodes.h" |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 19 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 20 | |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | /** |
| 26 | * Test that removing instruction from the graph removes itself from user lists |
| 27 | * and environment lists. |
| 28 | */ |
| 29 | TEST(Node, RemoveInstruction) { |
| 30 | ArenaPool pool; |
| 31 | ArenaAllocator allocator(&pool); |
| 32 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 33 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 34 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 35 | graph->AddBlock(entry); |
| 36 | graph->SetEntryBlock(entry); |
| 37 | HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot); |
| 38 | entry->AddInstruction(parameter); |
| 39 | entry->AddInstruction(new (&allocator) HGoto()); |
| 40 | |
| 41 | HBasicBlock* first_block = new (&allocator) HBasicBlock(graph); |
| 42 | graph->AddBlock(first_block); |
| 43 | entry->AddSuccessor(first_block); |
| 44 | HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0); |
| 45 | first_block->AddInstruction(null_check); |
| 46 | first_block->AddInstruction(new (&allocator) HReturnVoid()); |
| 47 | |
| 48 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 49 | graph->AddBlock(exit_block); |
| 50 | first_block->AddSuccessor(exit_block); |
| 51 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 52 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 53 | HEnvironment* environment = new (&allocator) HEnvironment( |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 54 | &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 55 | null_check->SetRawEnvironment(environment); |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 56 | environment->SetRawEnvAt(0, parameter); |
| 57 | parameter->AddEnvUseAt(null_check->GetEnvironment(), 0); |
| 58 | |
| 59 | ASSERT_TRUE(parameter->HasEnvironmentUses()); |
| 60 | ASSERT_TRUE(parameter->HasUses()); |
| 61 | |
| 62 | first_block->RemoveInstruction(null_check); |
| 63 | |
| 64 | ASSERT_FALSE(parameter->HasEnvironmentUses()); |
| 65 | ASSERT_FALSE(parameter->HasUses()); |
| 66 | } |
| 67 | |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 68 | /** |
| 69 | * Test that inserting an instruction in the graph updates user lists. |
| 70 | */ |
| 71 | TEST(Node, InsertInstruction) { |
| 72 | ArenaPool pool; |
| 73 | ArenaAllocator allocator(&pool); |
| 74 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 75 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 76 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 77 | graph->AddBlock(entry); |
| 78 | graph->SetEntryBlock(entry); |
| 79 | HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot); |
| 80 | HInstruction* parameter2 = new (&allocator) HParameterValue(0, Primitive::kPrimNot); |
| 81 | entry->AddInstruction(parameter1); |
| 82 | entry->AddInstruction(parameter2); |
| 83 | entry->AddInstruction(new (&allocator) HExit()); |
| 84 | |
| 85 | ASSERT_FALSE(parameter1->HasUses()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 86 | |
| 87 | HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0); |
| 88 | entry->InsertInstructionBefore(to_insert, parameter2); |
| 89 | |
| 90 | ASSERT_TRUE(parameter1->HasUses()); |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 91 | ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse()); |
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 | */ |
| 97 | TEST(Node, AddInstruction) { |
| 98 | ArenaPool pool; |
| 99 | ArenaAllocator allocator(&pool); |
| 100 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 101 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 102 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 103 | graph->AddBlock(entry); |
| 104 | graph->SetEntryBlock(entry); |
| 105 | HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot); |
| 106 | entry->AddInstruction(parameter); |
| 107 | |
| 108 | ASSERT_FALSE(parameter->HasUses()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 109 | |
| 110 | HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0); |
| 111 | entry->AddInstruction(to_add); |
| 112 | |
| 113 | ASSERT_TRUE(parameter->HasUses()); |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 114 | ASSERT_TRUE(parameter->GetUses().HasOnlyOneUse()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 117 | TEST(Node, ParentEnvironment) { |
| 118 | ArenaPool pool; |
| 119 | ArenaAllocator allocator(&pool); |
| 120 | |
| 121 | HGraph* graph = CreateGraph(&allocator); |
| 122 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 123 | graph->AddBlock(entry); |
| 124 | graph->SetEntryBlock(entry); |
| 125 | HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot); |
| 126 | HInstruction* with_environment = new (&allocator) HNullCheck(parameter1, 0); |
| 127 | entry->AddInstruction(parameter1); |
| 128 | entry->AddInstruction(with_environment); |
| 129 | entry->AddInstruction(new (&allocator) HExit()); |
| 130 | |
| 131 | ASSERT_TRUE(parameter1->HasUses()); |
| 132 | ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse()); |
| 133 | |
| 134 | HEnvironment* environment = new (&allocator) HEnvironment( |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 135 | &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 136 | GrowableArray<HInstruction*> array(&allocator, 1); |
| 137 | array.Add(parameter1); |
| 138 | |
| 139 | environment->CopyFrom(array); |
| 140 | with_environment->SetRawEnvironment(environment); |
| 141 | |
| 142 | ASSERT_TRUE(parameter1->HasEnvironmentUses()); |
| 143 | ASSERT_TRUE(parameter1->GetEnvUses().HasOnlyOneUse()); |
| 144 | |
| 145 | HEnvironment* parent1 = new (&allocator) HEnvironment( |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 146 | &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 147 | parent1->CopyFrom(array); |
| 148 | |
| 149 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u); |
| 150 | |
| 151 | HEnvironment* parent2 = new (&allocator) HEnvironment( |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 152 | &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 153 | parent2->CopyFrom(array); |
| 154 | parent1->SetAndCopyParentChain(&allocator, parent2); |
| 155 | |
| 156 | // One use for parent2, and one other use for the new parent of parent1. |
| 157 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u); |
| 158 | |
| 159 | // We have copied the parent chain. So we now have two more uses. |
| 160 | environment->SetAndCopyParentChain(&allocator, parent1); |
| 161 | ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u); |
| 162 | } |
| 163 | |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 164 | } // namespace art |