blob: 70dd8d7f88be268c29efbdda10b55231435cb6dc [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"
18#include "utils/arena_allocator.h"
19
20#include "gtest/gtest.h"
21
22namespace art {
23
24/**
25 * Test that removing instruction from the graph removes itself from user lists
26 * and environment lists.
27 */
28TEST(Node, RemoveInstruction) {
29 ArenaPool pool;
30 ArenaAllocator allocator(&pool);
31
32 HGraph* graph = new (&allocator) HGraph(&allocator);
33 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
34 graph->AddBlock(entry);
35 graph->SetEntryBlock(entry);
36 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
37 entry->AddInstruction(parameter);
38 entry->AddInstruction(new (&allocator) HGoto());
39
40 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
41 graph->AddBlock(first_block);
42 entry->AddSuccessor(first_block);
43 HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0);
44 first_block->AddInstruction(null_check);
45 first_block->AddInstruction(new (&allocator) HReturnVoid());
46
47 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
48 graph->AddBlock(exit_block);
49 first_block->AddSuccessor(exit_block);
50 exit_block->AddInstruction(new (&allocator) HExit());
51
52 HEnvironment* environment = new (&allocator) HEnvironment(&allocator, 1);
53 null_check->SetEnvironment(environment);
54 environment->SetRawEnvAt(0, parameter);
55 parameter->AddEnvUseAt(null_check->GetEnvironment(), 0);
56
57 ASSERT_TRUE(parameter->HasEnvironmentUses());
58 ASSERT_TRUE(parameter->HasUses());
59
60 first_block->RemoveInstruction(null_check);
61
62 ASSERT_FALSE(parameter->HasEnvironmentUses());
63 ASSERT_FALSE(parameter->HasUses());
64}
65
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010066/**
67 * Test that inserting an instruction in the graph updates user lists.
68 */
69TEST(Node, InsertInstruction) {
70 ArenaPool pool;
71 ArenaAllocator allocator(&pool);
72
73 HGraph* graph = new (&allocator) HGraph(&allocator);
74 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
75 graph->AddBlock(entry);
76 graph->SetEntryBlock(entry);
77 HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
78 HInstruction* parameter2 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
79 entry->AddInstruction(parameter1);
80 entry->AddInstruction(parameter2);
81 entry->AddInstruction(new (&allocator) HExit());
82
83 ASSERT_FALSE(parameter1->HasUses());
84 ASSERT_EQ(parameter1->NumberOfUses(), 0u);
85
86 HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
87 entry->InsertInstructionBefore(to_insert, parameter2);
88
89 ASSERT_TRUE(parameter1->HasUses());
90 ASSERT_EQ(parameter1->NumberOfUses(), 1u);
91}
92
93/**
94 * Test that adding an instruction in the graph updates user lists.
95 */
96TEST(Node, AddInstruction) {
97 ArenaPool pool;
98 ArenaAllocator allocator(&pool);
99
100 HGraph* graph = new (&allocator) HGraph(&allocator);
101 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
102 graph->AddBlock(entry);
103 graph->SetEntryBlock(entry);
104 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
105 entry->AddInstruction(parameter);
106
107 ASSERT_FALSE(parameter->HasUses());
108 ASSERT_EQ(parameter->NumberOfUses(), 0u);
109
110 HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
111 entry->AddInstruction(to_add);
112
113 ASSERT_TRUE(parameter->HasUses());
114 ASSERT_EQ(parameter->NumberOfUses(), 1u);
115}
116
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100117} // namespace art