blob: 6dd53e5b1423cc1ab6782d4e1140ba7c57686b8b [file] [log] [blame]
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001/*
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
Roland Levillainccc07a92014-09-16 14:48:16 +010020#include "nodes.h"
21#include "builder.h"
22#include "dex_file.h"
23#include "dex_instruction.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010024#include "ssa_liveness_analysis.h"
25
Roland Levillain72bceff2014-09-15 18:29:00 +010026#include "gtest/gtest.h"
27
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010028namespace art {
29
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000030#define NUM_INSTRUCTIONS(...) \
31 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
32
33#define ZERO_REGISTER_CODE_ITEM(...) \
34 { 0, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
35
36#define ONE_REGISTER_CODE_ITEM(...) \
37 { 1, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
38
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039#define TWO_REGISTERS_CODE_ITEM(...) \
40 { 2, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
41
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010042#define THREE_REGISTERS_CODE_ITEM(...) \
43 { 3, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
44
45LiveInterval* BuildInterval(const size_t ranges[][2],
46 size_t number_of_ranges,
47 ArenaAllocator* allocator,
48 int reg = -1) {
49 LiveInterval* interval = new (allocator) LiveInterval(allocator, Primitive::kPrimInt);
50 for (size_t i = number_of_ranges; i > 0; --i) {
51 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
52 }
53 interval->SetRegister(reg);
54 return interval;
55}
56
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000057void RemoveSuspendChecks(HGraph* graph) {
58 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
59 for (HInstructionIterator it(graph->GetBlocks().Get(i)->GetInstructions());
60 !it.Done();
61 it.Advance()) {
62 HInstruction* current = it.Current();
63 if (current->IsSuspendCheck()) {
64 current->GetBlock()->RemoveInstruction(current);
65 }
66 }
67 }
68}
69
Roland Levillainccc07a92014-09-16 14:48:16 +010070// Create a control-flow graph from Dex instructions.
71inline HGraph* CreateCFG(ArenaAllocator* allocator, const uint16_t* data) {
72 HGraphBuilder builder(allocator);
73 const DexFile::CodeItem* item =
74 reinterpret_cast<const DexFile::CodeItem*>(data);
75 HGraph* graph = builder.BuildGraph(*item);
76 return graph;
77}
78
Roland Levillain72bceff2014-09-15 18:29:00 +010079// Naive string diff data type.
80typedef std::list<std::pair<std::string, std::string>> diff_t;
81
82// An alias for the empty string used to make it clear that a line is
83// removed in a diff.
84static const std::string removed = "";
85
86// Naive patch command: apply a diff to a string.
87inline std::string Patch(const std::string& original, const diff_t& diff) {
88 std::string result = original;
89 for (const auto& p : diff) {
90 std::string::size_type pos = result.find(p.first);
91 EXPECT_NE(pos, std::string::npos);
92 result.replace(pos, p.first.size(), p.second);
93 }
94 return result;
95}
96
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010097} // namespace art
98
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000099#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_