nodes_test.cc revision b666f4805c8ae707ea6fd7f6c7f375e0b000dba8
1724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray/*
2724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray *
4724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * you may not use this file except in compliance with the License.
6724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * You may obtain a copy of the License at
7724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray *
8724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray *
10724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * See the License for the specific language governing permissions and
14724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * limitations under the License.
15724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray */
16724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
17b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
18724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray#include "nodes.h"
19724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
20724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray#include "gtest/gtest.h"
21724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
22724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffraynamespace art {
23724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
24724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray/**
25724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * Test that removing instruction from the graph removes itself from user lists
26724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * and environment lists.
27724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray */
28724c96326dea6ec33287a0076279c136abb0208aNicolas GeoffrayTEST(Node, RemoveInstruction) {
29724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ArenaPool pool;
30724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ArenaAllocator allocator(&pool);
31724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
32724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HGraph* graph = new (&allocator) HGraph(&allocator);
33724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
34724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->AddBlock(entry);
35724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->SetEntryBlock(entry);
36724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
37724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  entry->AddInstruction(parameter);
38724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  entry->AddInstruction(new (&allocator) HGoto());
39724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
40724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
41724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->AddBlock(first_block);
42724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  entry->AddSuccessor(first_block);
43724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0);
44724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->AddInstruction(null_check);
45724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->AddInstruction(new (&allocator) HReturnVoid());
46724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
47724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
48724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->AddBlock(exit_block);
49724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->AddSuccessor(exit_block);
50724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  exit_block->AddInstruction(new (&allocator) HExit());
51724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
52724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HEnvironment* environment = new (&allocator) HEnvironment(&allocator, 1);
53724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  null_check->SetEnvironment(environment);
54724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  environment->SetRawEnvAt(0, parameter);
55724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  parameter->AddEnvUseAt(null_check->GetEnvironment(), 0);
56724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
57724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_TRUE(parameter->HasEnvironmentUses());
58724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_TRUE(parameter->HasUses());
59724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
60724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->RemoveInstruction(null_check);
61724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
62724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_FALSE(parameter->HasEnvironmentUses());
63724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_FALSE(parameter->HasUses());
64724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray}
65724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
66191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray/**
67191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray * Test that inserting an instruction in the graph updates user lists.
68191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray */
69191c4b1372aef7c0272f8fa3985b55513029e728Nicolas GeoffrayTEST(Node, InsertInstruction) {
70191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaPool pool;
71191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaAllocator allocator(&pool);
72191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
73191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HGraph* graph = new (&allocator) HGraph(&allocator);
74191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
75191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->AddBlock(entry);
76191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->SetEntryBlock(entry);
77191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
78191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* parameter2 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
79191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(parameter1);
80191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(parameter2);
81191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(new (&allocator) HExit());
82191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
83191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_FALSE(parameter1->HasUses());
84191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
85191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
86191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->InsertInstructionBefore(to_insert, parameter2);
87191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
88191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_TRUE(parameter1->HasUses());
89ea55b934cff1280318f5514039549799227cfa3dDavid Brazdil  ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse());
90191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray}
91191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
92191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray/**
93191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray * Test that adding an instruction in the graph updates user lists.
94191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray */
95191c4b1372aef7c0272f8fa3985b55513029e728Nicolas GeoffrayTEST(Node, AddInstruction) {
96191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaPool pool;
97191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaAllocator allocator(&pool);
98191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
99191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HGraph* graph = new (&allocator) HGraph(&allocator);
100191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
101191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->AddBlock(entry);
102191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->SetEntryBlock(entry);
103191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
104191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(parameter);
105191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
106191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_FALSE(parameter->HasUses());
107191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
108191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
109191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(to_add);
110191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
111191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_TRUE(parameter->HasUses());
112ea55b934cff1280318f5514039549799227cfa3dDavid Brazdil  ASSERT_TRUE(parameter->GetUses().HasOnlyOneUse());
113191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray}
114191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
115724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray}  // namespace art
116