nodes_test.cc revision ed59619b370ef23ffbb25d1d01f615e60a9262b6
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 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 66/** 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->ExpensiveComputeNumberOfUses(), 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->ExpensiveComputeNumberOfUses(), 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->ExpensiveComputeNumberOfUses(), 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->ExpensiveComputeNumberOfUses(), 1u); 115} 116 117} // namespace art 118