blob: 9aba91205c06a3976469e912bacb723573556703 [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"
Vladimir Marko69d310e2017-10-09 14:12:23 +010025#include "handle_scope-inl.h"
26#include "mirror/class_loader.h"
27#include "mirror/dex_cache.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000029#include "scoped_thread_state_change.h"
30#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010031#include "ssa_liveness_analysis.h"
32
Roland Levillain72bceff2014-09-15 18:29:00 +010033#include "gtest/gtest.h"
34
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010035namespace art {
36
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000037#define NUM_INSTRUCTIONS(...) \
38 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
39
Roland Levillain55dcfb52014-10-24 18:09:09 +010040#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
41 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000042
Roland Levillain55dcfb52014-10-24 18:09:09 +010043#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
44#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
45#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
46#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
47#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
48#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
49#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000050
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010051LiveInterval* BuildInterval(const size_t ranges[][2],
52 size_t number_of_ranges,
Vladimir Markoe764d2e2017-10-05 14:35:55 +010053 ScopedArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000054 int reg = -1,
55 HInstruction* defined_by = nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 LiveInterval* interval =
57 LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000058 if (defined_by != nullptr) {
59 defined_by->SetLiveInterval(interval);
60 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010061 for (size_t i = number_of_ranges; i > 0; --i) {
62 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
63 }
64 interval->SetRegister(reg);
65 return interval;
66}
67
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000068void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010069 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000070 if (block != nullptr) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010071 if (block->GetLoopInformation() != nullptr) {
72 block->GetLoopInformation()->SetSuspendCheck(nullptr);
73 }
David Brazdilbadd8262016-02-02 16:28:56 +000074 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
75 HInstruction* current = it.Current();
76 if (current->IsSuspendCheck()) {
77 current->GetBlock()->RemoveInstruction(current);
78 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000079 }
80 }
81 }
82}
83
Vladimir Markoca6fff82017-10-03 14:49:14 +010084class ArenaPoolAndAllocator {
85 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010086 ArenaPoolAndAllocator()
87 : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { }
Vladimir Markoca6fff82017-10-03 14:49:14 +010088
89 ArenaAllocator* GetAllocator() { return &allocator_; }
90 ArenaStack* GetArenaStack() { return &arena_stack_; }
Vladimir Markoe764d2e2017-10-05 14:35:55 +010091 ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +010092
93 private:
94 ArenaPool pool_;
95 ArenaAllocator allocator_;
96 ArenaStack arena_stack_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +010097 ScopedArenaAllocator scoped_allocator_;
Vladimir Markoca6fff82017-10-03 14:49:14 +010098};
99
100inline HGraph* CreateGraph(ArenaPoolAndAllocator* pool_and_allocator) {
101 return new (pool_and_allocator->GetAllocator()) HGraph(
102 pool_and_allocator->GetAllocator(),
103 pool_and_allocator->GetArenaStack(),
104 *reinterpret_cast<DexFile*>(pool_and_allocator->GetAllocator()->Alloc(sizeof(DexFile))),
Igor Murashkin032cacd2017-04-06 14:40:08 -0700105 /*method_idx*/-1,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 kRuntimeISA);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100107}
108
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109class OptimizingUnitTest : public CommonCompilerTest {
110 protected:
111 OptimizingUnitTest() : pool_and_allocator_(new ArenaPoolAndAllocator()) { }
David Brazdilbadd8262016-02-02 16:28:56 +0000112
Vladimir Markoca6fff82017-10-03 14:49:14 +0100113 ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); }
114 ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); }
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100115 ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100116
117 void ResetPoolAndAllocator() {
118 pool_and_allocator_.reset(new ArenaPoolAndAllocator());
119 handles_.reset(); // When getting rid of the old HGraph, we can also reset handles_.
David Brazdilbadd8262016-02-02 16:28:56 +0000120 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100121
122 HGraph* CreateGraph() {
123 return art::CreateGraph(pool_and_allocator_.get());
124 }
125
126 // Create a control-flow graph from Dex instructions.
127 HGraph* CreateCFG(const uint16_t* data, DataType::Type return_type = DataType::Type::kInt32) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100128 const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(data);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100129 HGraph* graph = CreateGraph();
130
131 {
132 ScopedObjectAccess soa(Thread::Current());
133 if (handles_ == nullptr) {
134 handles_.reset(new VariableSizedHandleScope(soa.Self()));
135 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100136 const DexFile* dex_file = graph->GetAllocator()->Alloc<DexFile>();
137 const DexCompilationUnit* dex_compilation_unit =
138 new (graph->GetAllocator()) DexCompilationUnit(
139 handles_->NewHandle<mirror::ClassLoader>(nullptr),
140 /* class_linker */ nullptr,
141 *dex_file,
142 code_item,
143 /* class_def_index */ DexFile::kDexNoIndex16,
144 /* method_idx */ dex::kDexNoIndex,
145 /* access_flags */ 0u,
146 /* verified_method */ nullptr,
147 handles_->NewHandle<mirror::DexCache>(nullptr));
148 HGraphBuilder builder(graph, dex_compilation_unit, *code_item, handles_.get(), return_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
150 return graph_built ? graph : nullptr;
151 }
152 }
153
154 private:
155 std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_;
156 std::unique_ptr<VariableSizedHandleScope> handles_;
157};
Roland Levillainccc07a92014-09-16 14:48:16 +0100158
Roland Levillain72bceff2014-09-15 18:29:00 +0100159// Naive string diff data type.
160typedef std::list<std::pair<std::string, std::string>> diff_t;
161
162// An alias for the empty string used to make it clear that a line is
163// removed in a diff.
164static const std::string removed = "";
165
166// Naive patch command: apply a diff to a string.
167inline std::string Patch(const std::string& original, const diff_t& diff) {
168 std::string result = original;
169 for (const auto& p : diff) {
170 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000171 DCHECK_NE(pos, std::string::npos)
172 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100173 result.replace(pos, p.first.size(), p.second);
174 }
175 return result;
176}
177
Mingyao Yangf384f882014-10-22 16:08:18 -0700178// Returns if the instruction is removed from the graph.
179inline bool IsRemoved(HInstruction* instruction) {
180 return instruction->GetBlock() == nullptr;
181}
182
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100183} // namespace art
184
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000185#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_