blob: 5632f9a453b9507aa389ddde092e708c95b3cf4b [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
Vladimir Markoca6fff82017-10-03 14:49:14 +010020#include "base/scoped_arena_allocator.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010021#include "builder.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000022#include "common_compiler_test.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010023#include "dex_file.h"
24#include "dex_instruction.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "handle_scope.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000027#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,
Vladimir Markoe764d2e2017-10-05 14:35:55 +010051 ScopedArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000052 int reg = -1,
53 HInstruction* defined_by = nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010054 LiveInterval* interval =
55 LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000056 if (defined_by != nullptr) {
57 defined_by->SetLiveInterval(interval);
58 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 for (size_t i = number_of_ranges; i > 0; --i) {
60 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
61 }
62 interval->SetRegister(reg);
63 return interval;
64}
65
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000066void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010067 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000068 if (block != nullptr) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010069 if (block->GetLoopInformation() != nullptr) {
70 block->GetLoopInformation()->SetSuspendCheck(nullptr);
71 }
David Brazdilbadd8262016-02-02 16:28:56 +000072 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
73 HInstruction* current = it.Current();
74 if (current->IsSuspendCheck()) {
75 current->GetBlock()->RemoveInstruction(current);
76 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000077 }
78 }
79 }
80}
81
Vladimir Markoca6fff82017-10-03 14:49:14 +010082class ArenaPoolAndAllocator {
83 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010084 ArenaPoolAndAllocator()
85 : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { }
Vladimir Markoca6fff82017-10-03 14:49:14 +010086
87 ArenaAllocator* GetAllocator() { return &allocator_; }
88 ArenaStack* GetArenaStack() { return &arena_stack_; }
Vladimir Markoe764d2e2017-10-05 14:35:55 +010089 ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +010090
91 private:
92 ArenaPool pool_;
93 ArenaAllocator allocator_;
94 ArenaStack arena_stack_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +010095 ScopedArenaAllocator scoped_allocator_;
Vladimir Markoca6fff82017-10-03 14:49:14 +010096};
97
98inline HGraph* CreateGraph(ArenaPoolAndAllocator* pool_and_allocator) {
99 return new (pool_and_allocator->GetAllocator()) HGraph(
100 pool_and_allocator->GetAllocator(),
101 pool_and_allocator->GetArenaStack(),
102 *reinterpret_cast<DexFile*>(pool_and_allocator->GetAllocator()->Alloc(sizeof(DexFile))),
Igor Murashkin032cacd2017-04-06 14:40:08 -0700103 /*method_idx*/-1,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 kRuntimeISA);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100105}
106
Vladimir Markoca6fff82017-10-03 14:49:14 +0100107class OptimizingUnitTest : public CommonCompilerTest {
108 protected:
109 OptimizingUnitTest() : pool_and_allocator_(new ArenaPoolAndAllocator()) { }
David Brazdilbadd8262016-02-02 16:28:56 +0000110
Vladimir Markoca6fff82017-10-03 14:49:14 +0100111 ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); }
112 ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); }
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100113 ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100114
115 void ResetPoolAndAllocator() {
116 pool_and_allocator_.reset(new ArenaPoolAndAllocator());
117 handles_.reset(); // When getting rid of the old HGraph, we can also reset handles_.
David Brazdilbadd8262016-02-02 16:28:56 +0000118 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100119
120 HGraph* CreateGraph() {
121 return art::CreateGraph(pool_and_allocator_.get());
122 }
123
124 // Create a control-flow graph from Dex instructions.
125 HGraph* CreateCFG(const uint16_t* data, DataType::Type return_type = DataType::Type::kInt32) {
126 const DexFile::CodeItem* item =
127 reinterpret_cast<const DexFile::CodeItem*>(data);
128 HGraph* graph = CreateGraph();
129
130 {
131 ScopedObjectAccess soa(Thread::Current());
132 if (handles_ == nullptr) {
133 handles_.reset(new VariableSizedHandleScope(soa.Self()));
134 }
135 HGraphBuilder builder(graph, *item, handles_.get(), return_type);
136 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
137 return graph_built ? graph : nullptr;
138 }
139 }
140
141 private:
142 std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_;
143 std::unique_ptr<VariableSizedHandleScope> handles_;
144};
Roland Levillainccc07a92014-09-16 14:48:16 +0100145
Roland Levillain72bceff2014-09-15 18:29:00 +0100146// Naive string diff data type.
147typedef std::list<std::pair<std::string, std::string>> diff_t;
148
149// An alias for the empty string used to make it clear that a line is
150// removed in a diff.
151static const std::string removed = "";
152
153// Naive patch command: apply a diff to a string.
154inline std::string Patch(const std::string& original, const diff_t& diff) {
155 std::string result = original;
156 for (const auto& p : diff) {
157 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000158 DCHECK_NE(pos, std::string::npos)
159 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100160 result.replace(pos, p.first.size(), p.second);
161 }
162 return result;
163}
164
Mingyao Yangf384f882014-10-22 16:08:18 -0700165// Returns if the instruction is removed from the graph.
166inline bool IsRemoved(HInstruction* instruction) {
167 return instruction->GetBlock() == nullptr;
168}
169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100170} // namespace art
171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_