nodes_test.cc revision d23eeef3492b53102eb8093524cf37e2b4c296db
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"
190a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray#include "optimizing_unit_test.h"
20724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
21724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray#include "gtest/gtest.h"
22724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
23724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffraynamespace art {
24724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
25724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray/**
26724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * Test that removing instruction from the graph removes itself from user lists
27724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray * and environment lists.
28724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray */
29724c96326dea6ec33287a0076279c136abb0208aNicolas GeoffrayTEST(Node, RemoveInstruction) {
30724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ArenaPool pool;
31724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ArenaAllocator allocator(&pool);
32724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
330a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HGraph* graph = CreateGraph(&allocator);
34724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
35724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->AddBlock(entry);
36724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->SetEntryBlock(entry);
37724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
38724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  entry->AddInstruction(parameter);
39724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  entry->AddInstruction(new (&allocator) HGoto());
40724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
41724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
42724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->AddBlock(first_block);
43724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  entry->AddSuccessor(first_block);
44724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HInstruction* null_check = new (&allocator) HNullCheck(parameter, 0);
45724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->AddInstruction(null_check);
46724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->AddInstruction(new (&allocator) HReturnVoid());
47724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
48724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
49724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  graph->AddBlock(exit_block);
50724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->AddSuccessor(exit_block);
51724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  exit_block->AddInstruction(new (&allocator) HExit());
52724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
530a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HEnvironment* environment = new (&allocator) HEnvironment(
54d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray      &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic, null_check);
553dcd58cd54a922b864494fb7fff4a7f7a8562db9Nicolas Geoffray  null_check->SetRawEnvironment(environment);
56724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  environment->SetRawEnvAt(0, parameter);
57724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  parameter->AddEnvUseAt(null_check->GetEnvironment(), 0);
58724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
59724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_TRUE(parameter->HasEnvironmentUses());
60724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_TRUE(parameter->HasUses());
61724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
62724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  first_block->RemoveInstruction(null_check);
63724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
64724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_FALSE(parameter->HasEnvironmentUses());
65724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray  ASSERT_FALSE(parameter->HasUses());
66724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray}
67724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray
68191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray/**
69191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray * Test that inserting an instruction in the graph updates user lists.
70191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray */
71191c4b1372aef7c0272f8fa3985b55513029e728Nicolas GeoffrayTEST(Node, InsertInstruction) {
72191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaPool pool;
73191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaAllocator allocator(&pool);
74191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
750a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HGraph* graph = CreateGraph(&allocator);
76191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
77191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->AddBlock(entry);
78191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->SetEntryBlock(entry);
79191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
80191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* parameter2 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
81191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(parameter1);
82191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(parameter2);
83191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(new (&allocator) HExit());
84191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
85191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_FALSE(parameter1->HasUses());
86191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
87191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* to_insert = new (&allocator) HNullCheck(parameter1, 0);
88191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->InsertInstructionBefore(to_insert, parameter2);
89191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
90191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_TRUE(parameter1->HasUses());
91ea55b934cff1280318f5514039549799227cfa3dDavid Brazdil  ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse());
92191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray}
93191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
94191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray/**
95191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray * Test that adding an instruction in the graph updates user lists.
96191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray */
97191c4b1372aef7c0272f8fa3985b55513029e728Nicolas GeoffrayTEST(Node, AddInstruction) {
98191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaPool pool;
99191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ArenaAllocator allocator(&pool);
100191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
1010a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HGraph* graph = CreateGraph(&allocator);
102191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
103191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->AddBlock(entry);
104191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  graph->SetEntryBlock(entry);
105191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
106191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(parameter);
107191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
108191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_FALSE(parameter->HasUses());
109191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
110191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  HInstruction* to_add = new (&allocator) HNullCheck(parameter, 0);
111191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  entry->AddInstruction(to_add);
112191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
113191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray  ASSERT_TRUE(parameter->HasUses());
114ea55b934cff1280318f5514039549799227cfa3dDavid Brazdil  ASSERT_TRUE(parameter->GetUses().HasOnlyOneUse());
115191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray}
116191c4b1372aef7c0272f8fa3985b55513029e728Nicolas Geoffray
1170a23d74dc2751440822960eab218be4cb8843647Nicolas GeoffrayTEST(Node, ParentEnvironment) {
1180a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ArenaPool pool;
1190a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ArenaAllocator allocator(&pool);
1200a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1210a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HGraph* graph = CreateGraph(&allocator);
1220a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
1230a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  graph->AddBlock(entry);
1240a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  graph->SetEntryBlock(entry);
1250a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HInstruction* parameter1 = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
1260a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HInstruction* with_environment = new (&allocator) HNullCheck(parameter1, 0);
1270a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  entry->AddInstruction(parameter1);
1280a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  entry->AddInstruction(with_environment);
1290a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  entry->AddInstruction(new (&allocator) HExit());
1300a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1310a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_TRUE(parameter1->HasUses());
1320a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_TRUE(parameter1->GetUses().HasOnlyOneUse());
1330a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1340a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HEnvironment* environment = new (&allocator) HEnvironment(
135d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray      &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic, with_environment);
1360a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  GrowableArray<HInstruction*> array(&allocator, 1);
1370a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  array.Add(parameter1);
1380a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1390a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  environment->CopyFrom(array);
1400a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  with_environment->SetRawEnvironment(environment);
1410a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1420a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_TRUE(parameter1->HasEnvironmentUses());
1430a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_TRUE(parameter1->GetEnvUses().HasOnlyOneUse());
1440a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1450a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HEnvironment* parent1 = new (&allocator) HEnvironment(
146d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray      &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic, nullptr);
1470a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  parent1->CopyFrom(array);
1480a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1490a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 2u);
1500a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1510a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  HEnvironment* parent2 = new (&allocator) HEnvironment(
152d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray      &allocator, 1, graph->GetDexFile(), graph->GetMethodIdx(), 0, kStatic, nullptr);
1530a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  parent2->CopyFrom(array);
1540a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  parent1->SetAndCopyParentChain(&allocator, parent2);
1550a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1560a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  // One use for parent2, and one other use for the new parent of parent1.
1570a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 4u);
1580a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
1590a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  // We have copied the parent chain. So we now have two more uses.
1600a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  environment->SetAndCopyParentChain(&allocator, parent1);
1610a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  ASSERT_EQ(parameter1->GetEnvUses().SizeSlow(), 6u);
1620a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray}
1630a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray
164724c96326dea6ec33287a0076279c136abb0208aNicolas Geoffray}  // namespace art
165