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.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 32 namespace 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 48 LiveInterval* 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 64 void RemoveSuspendChecks(HGraph* graph) { 65 for (HBasicBlock* block : graph->GetBlocks()) { 66 if (block != nullptr) { 67 if (block->GetLoopInformation() != nullptr) { 68 block->GetLoopInformation()->SetSuspendCheck(nullptr); 69 } 70 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { 71 HInstruction* current = it.Current(); 72 if (current->IsSuspendCheck()) { 73 current->GetBlock()->RemoveInstruction(current); 74 } 75 } 76 } 77 } 78 } 79 80 inline HGraph* CreateGraph(ArenaAllocator* allocator) { 81 return new (allocator) HGraph( 82 allocator, 83 *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))), 84 /*method_idx*/-1, 85 kRuntimeISA); 86 } 87 88 // Create a control-flow graph from Dex instructions. 89 inline HGraph* CreateCFG(ArenaAllocator* allocator, 90 const uint16_t* data, 91 Primitive::Type return_type = Primitive::kPrimInt) { 92 const DexFile::CodeItem* item = 93 reinterpret_cast<const DexFile::CodeItem*>(data); 94 HGraph* graph = CreateGraph(allocator); 95 96 { 97 ScopedObjectAccess soa(Thread::Current()); 98 VariableSizedHandleScope handles(soa.Self()); 99 HGraphBuilder builder(graph, *item, &handles, return_type); 100 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess); 101 return graph_built ? graph : nullptr; 102 } 103 } 104 105 // Naive string diff data type. 106 typedef std::list<std::pair<std::string, std::string>> diff_t; 107 108 // An alias for the empty string used to make it clear that a line is 109 // removed in a diff. 110 static const std::string removed = ""; 111 112 // Naive patch command: apply a diff to a string. 113 inline std::string Patch(const std::string& original, const diff_t& diff) { 114 std::string result = original; 115 for (const auto& p : diff) { 116 std::string::size_type pos = result.find(p.first); 117 DCHECK_NE(pos, std::string::npos) 118 << "Could not find: \"" << p.first << "\" in \"" << result << "\""; 119 result.replace(pos, p.first.size(), p.second); 120 } 121 return result; 122 } 123 124 // Returns if the instruction is removed from the graph. 125 inline bool IsRemoved(HInstruction* instruction) { 126 return instruction->GetBlock() == nullptr; 127 } 128 129 } // namespace art 130 131 #endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_ 132