blob: 4e83ce576c8ac96bdf69760b5e4baabce07cac48 [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010018#include "nodes.h"
Nicolas Geoffray724c9632014-09-22 12:27:27 +010019
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);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +010053 null_check->SetRawEnvironment(environment);
Nicolas Geoffray724c9632014-09-22 12:27:27 +010054 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());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010084
85 HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
86 entry->InsertInstructionBefore(to_insert, parameter2);
87
88 ASSERT_TRUE(parameter1->HasUses());
David Brazdilea55b932015-01-27 17:12:29 +000089 ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +010090}
91
92/**
93 * Test that adding an instruction in the graph updates user lists.
94 */
95TEST(Node, AddInstruction) {
96 ArenaPool pool;
97 ArenaAllocator allocator(&pool);
98
99 HGraph* graph = new (&allocator) HGraph(&allocator);
100 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
101 graph->AddBlock(entry);
102 graph->SetEntryBlock(entry);
103 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
104 entry->AddInstruction(parameter);
105
106 ASSERT_FALSE(parameter->HasUses());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100107
108 HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
109 entry->AddInstruction(to_add);
110
111 ASSERT_TRUE(parameter->HasUses());
David Brazdilea55b932015-01-27 17:12:29 +0000112 ASSERT_TRUE(parameter->GetUses().HasOnlyOneUse());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100113}
114
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100115} // namespace art