blob: 0c7648edc2294f9916ea39f6d29fd669e18fb1db [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"
David Brazdil4833f5a2015-12-16 10:37:39 +000022#include "common_compiler_test.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080023#include "compiler/dex/pass_manager.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010024#include "dex_file.h"
25#include "dex_instruction.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000026#include "handle_scope-inl.h"
27#include "scoped_thread_state_change.h"
28#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010029#include "ssa_liveness_analysis.h"
30
Roland Levillain72bceff2014-09-15 18:29:00 +010031#include "gtest/gtest.h"
32
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010033namespace art {
34
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000035#define NUM_INSTRUCTIONS(...) \
36 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
37
Roland Levillain55dcfb52014-10-24 18:09:09 +010038#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
39 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000040
Roland Levillain55dcfb52014-10-24 18:09:09 +010041#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
42#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
43#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
44#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
45#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
46#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
47#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010049LiveInterval* BuildInterval(const size_t ranges[][2],
50 size_t number_of_ranges,
51 ArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000052 int reg = -1,
53 HInstruction* defined_by = nullptr) {
54 LiveInterval* interval = LiveInterval::MakeInterval(allocator, Primitive::kPrimInt, defined_by);
55 if (defined_by != nullptr) {
56 defined_by->SetLiveInterval(interval);
57 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058 for (size_t i = number_of_ranges; i > 0; --i) {
59 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
60 }
61 interval->SetRegister(reg);
62 return interval;
63}
64
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000065void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010066 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000067 if (block != nullptr) {
68 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
69 HInstruction* current = it.Current();
70 if (current->IsSuspendCheck()) {
71 current->GetBlock()->RemoveInstruction(current);
72 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000073 }
74 }
75 }
76}
77
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010078inline HGraph* CreateGraph(ArenaAllocator* allocator) {
79 return new (allocator) HGraph(
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 allocator, *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))), -1, false,
81 kRuntimeISA);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010082}
83
Roland Levillainccc07a92014-09-16 14:48:16 +010084// Create a control-flow graph from Dex instructions.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010085inline HGraph* CreateCFG(ArenaAllocator* allocator,
86 const uint16_t* data,
87 Primitive::Type return_type = Primitive::kPrimInt) {
Roland Levillainccc07a92014-09-16 14:48:16 +010088 const DexFile::CodeItem* item =
89 reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdilbadd8262016-02-02 16:28:56 +000090 HGraph* graph = CreateGraph(allocator);
91
92 {
93 ScopedObjectAccess soa(Thread::Current());
94 StackHandleScopeCollection handles(soa.Self());
95 HGraphBuilder builder(graph, return_type);
96 bool graph_built = (builder.BuildGraph(*item, &handles) == kAnalysisSuccess);
97 return graph_built ? graph : nullptr;
98 }
Roland Levillainccc07a92014-09-16 14:48:16 +010099}
100
Roland Levillain72bceff2014-09-15 18:29:00 +0100101// Naive string diff data type.
102typedef std::list<std::pair<std::string, std::string>> diff_t;
103
104// An alias for the empty string used to make it clear that a line is
105// removed in a diff.
106static const std::string removed = "";
107
108// Naive patch command: apply a diff to a string.
109inline std::string Patch(const std::string& original, const diff_t& diff) {
110 std::string result = original;
111 for (const auto& p : diff) {
112 std::string::size_type pos = result.find(p.first);
113 EXPECT_NE(pos, std::string::npos);
114 result.replace(pos, p.first.size(), p.second);
115 }
116 return result;
117}
118
Mingyao Yangf384f882014-10-22 16:08:18 -0700119// Returns if the instruction is removed from the graph.
120inline bool IsRemoved(HInstruction* instruction) {
121 return instruction->GetBlock() == nullptr;
122}
123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124} // namespace art
125
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_