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#ifndef ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ 18#define ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ 19 20#include "nodes.h" 21#include "builder.h" 22#include "common_compiler_test.h" 23#include "dex_file.h" 24#include "dex_instruction.h" 25#include "handle_scope-inl.h" 26#include "scoped_thread_state_change.h" 27#include "ssa_builder.h" 28#include "ssa_liveness_analysis.h" 29 30#include "gtest/gtest.h" 31 32namespace art { 33 34#define NUM_INSTRUCTIONS(...) \ 35 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t)) 36 37#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \ 38 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ } 39 40#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__) 41#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__) 42#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__) 43#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__) 44#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__) 45#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__) 46#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__) 47 48LiveInterval* BuildInterval(const size_t ranges[][2], 49 size_t number_of_ranges, 50 ArenaAllocator* allocator, 51 int reg = -1, 52 HInstruction* defined_by = nullptr) { 53 LiveInterval* interval = LiveInterval::MakeInterval(allocator, Primitive::kPrimInt, defined_by); 54 if (defined_by != nullptr) { 55 defined_by->SetLiveInterval(interval); 56 } 57 for (size_t i = number_of_ranges; i > 0; --i) { 58 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]); 59 } 60 interval->SetRegister(reg); 61 return interval; 62} 63 64void RemoveSuspendChecks(HGraph* graph) { 65 for (HBasicBlock* block : graph->GetBlocks()) { 66 if (block != nullptr) { 67 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { 68 HInstruction* current = it.Current(); 69 if (current->IsSuspendCheck()) { 70 current->GetBlock()->RemoveInstruction(current); 71 } 72 } 73 } 74 } 75} 76 77inline HGraph* CreateGraph(ArenaAllocator* allocator) { 78 return new (allocator) HGraph( 79 allocator, *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))), -1, false, 80 kRuntimeISA); 81} 82 83// Create a control-flow graph from Dex instructions. 84inline HGraph* CreateCFG(ArenaAllocator* allocator, 85 const uint16_t* data, 86 Primitive::Type return_type = Primitive::kPrimInt) { 87 const DexFile::CodeItem* item = 88 reinterpret_cast<const DexFile::CodeItem*>(data); 89 HGraph* graph = CreateGraph(allocator); 90 91 { 92 ScopedObjectAccess soa(Thread::Current()); 93 StackHandleScopeCollection handles(soa.Self()); 94 HGraphBuilder builder(graph, *item, &handles, return_type); 95 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); 96 return graph_built ? graph : nullptr; 97 } 98} 99 100// Naive string diff data type. 101typedef std::list<std::pair<std::string, std::string>> diff_t; 102 103// An alias for the empty string used to make it clear that a line is 104// removed in a diff. 105static const std::string removed = ""; 106 107// Naive patch command: apply a diff to a string. 108inline std::string Patch(const std::string& original, const diff_t& diff) { 109 std::string result = original; 110 for (const auto& p : diff) { 111 std::string::size_type pos = result.find(p.first); 112 DCHECK_NE(pos, std::string::npos) 113 << "Could not find: \"" << p.first << "\" in \"" << result << "\""; 114 result.replace(pos, p.first.size(), p.second); 115 } 116 return result; 117} 118 119// Returns if the instruction is removed from the graph. 120inline bool IsRemoved(HInstruction* instruction) { 121 return instruction->GetBlock() == nullptr; 122} 123 124} // namespace art 125 126#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ 127