blob: 6dcbadba6ed13c6f3f27e4202c70ed801a429d50 [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
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080020#include <memory>
21#include <vector>
22
Vladimir Markoca6fff82017-10-03 14:49:14 +010023#include "base/scoped_arena_allocator.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010024#include "builder.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000025#include "common_compiler_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/code_item_accessors-inl.h"
27#include "dex/dex_file.h"
28#include "dex/dex_instruction.h"
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080029#include "dex/standard_dex_file.h"
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000030#include "driver/dex_compilation_unit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010031#include "handle_scope-inl.h"
32#include "mirror/class_loader.h"
33#include "mirror/dex_cache.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000035#include "scoped_thread_state_change.h"
36#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010037#include "ssa_liveness_analysis.h"
38
Roland Levillain72bceff2014-09-15 18:29:00 +010039#include "gtest/gtest.h"
40
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010041namespace art {
42
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043#define NUM_INSTRUCTIONS(...) \
44 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
45
Roland Levillain55dcfb52014-10-24 18:09:09 +010046#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
47 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048
Roland Levillain55dcfb52014-10-24 18:09:09 +010049#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
50#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
51#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
52#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
53#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
54#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
55#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057LiveInterval* BuildInterval(const size_t ranges[][2],
58 size_t number_of_ranges,
Vladimir Markoe764d2e2017-10-05 14:35:55 +010059 ScopedArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000060 int reg = -1,
61 HInstruction* defined_by = nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 LiveInterval* interval =
63 LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000064 if (defined_by != nullptr) {
65 defined_by->SetLiveInterval(interval);
66 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010067 for (size_t i = number_of_ranges; i > 0; --i) {
68 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
69 }
70 interval->SetRegister(reg);
71 return interval;
72}
73
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000074void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010075 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000076 if (block != nullptr) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010077 if (block->GetLoopInformation() != nullptr) {
78 block->GetLoopInformation()->SetSuspendCheck(nullptr);
79 }
David Brazdilbadd8262016-02-02 16:28:56 +000080 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
81 HInstruction* current = it.Current();
82 if (current->IsSuspendCheck()) {
83 current->GetBlock()->RemoveInstruction(current);
84 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000085 }
86 }
87 }
88}
89
Vladimir Markoca6fff82017-10-03 14:49:14 +010090class ArenaPoolAndAllocator {
91 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010092 ArenaPoolAndAllocator()
93 : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { }
Vladimir Markoca6fff82017-10-03 14:49:14 +010094
95 ArenaAllocator* GetAllocator() { return &allocator_; }
96 ArenaStack* GetArenaStack() { return &arena_stack_; }
Vladimir Markoe764d2e2017-10-05 14:35:55 +010097 ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +010098
99 private:
100 ArenaPool pool_;
101 ArenaAllocator allocator_;
102 ArenaStack arena_stack_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100103 ScopedArenaAllocator scoped_allocator_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100104};
105
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800106// Have a separate helper so the OptimizingCFITest can inherit it without causing
107// multiple inheritance errors from having two gtest as a parent twice.
108class OptimizingUnitTestHelper {
109 public:
110 OptimizingUnitTestHelper() : pool_and_allocator_(new ArenaPoolAndAllocator()) { }
David Brazdilbadd8262016-02-02 16:28:56 +0000111
Vladimir Markoca6fff82017-10-03 14:49:14 +0100112 ArenaAllocator* GetAllocator() { return pool_and_allocator_->GetAllocator(); }
113 ArenaStack* GetArenaStack() { return pool_and_allocator_->GetArenaStack(); }
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100114 ScopedArenaAllocator* GetScopedAllocator() { return pool_and_allocator_->GetScopedAllocator(); }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100115
116 void ResetPoolAndAllocator() {
117 pool_and_allocator_.reset(new ArenaPoolAndAllocator());
118 handles_.reset(); // When getting rid of the old HGraph, we can also reset handles_.
David Brazdilbadd8262016-02-02 16:28:56 +0000119 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100120
121 HGraph* CreateGraph() {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800122 ArenaAllocator* const allocator = pool_and_allocator_->GetAllocator();
123
124 // Reserve a big array of 0s so the dex file constructor can offsets from the header.
125 static constexpr size_t kDexDataSize = 4 * KB;
126 const uint8_t* dex_data = reinterpret_cast<uint8_t*>(allocator->Alloc(kDexDataSize));
127
128 // Create the dex file based on the fake data. Call the constructor so that we can use virtual
129 // functions. Don't use the arena for the StandardDexFile otherwise the dex location leaks.
130 dex_files_.emplace_back(new StandardDexFile(
131 dex_data,
132 sizeof(StandardDexFile::Header),
133 "no_location",
134 /*location_checksum*/ 0,
135 /*oat_dex_file*/ nullptr,
136 /*container*/ nullptr));
137
138 return new (allocator) HGraph(
139 allocator,
140 pool_and_allocator_->GetArenaStack(),
141 *dex_files_.back(),
142 /*method_idx*/-1,
143 kRuntimeISA);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100144 }
145
146 // Create a control-flow graph from Dex instructions.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800147 HGraph* CreateCFG(const std::vector<uint16_t>& data,
148 DataType::Type return_type = DataType::Type::kInt32) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 HGraph* graph = CreateGraph();
150
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800151 // The code item data might not aligned to 4 bytes, copy it to ensure that.
152 const size_t code_item_size = data.size() * sizeof(data.front());
153 void* aligned_data = GetAllocator()->Alloc(code_item_size);
154 memcpy(aligned_data, &data[0], code_item_size);
155 CHECK_ALIGNED(aligned_data, StandardDexFile::CodeItem::kAlignment);
156 const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(aligned_data);
157
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 {
159 ScopedObjectAccess soa(Thread::Current());
160 if (handles_ == nullptr) {
161 handles_.reset(new VariableSizedHandleScope(soa.Self()));
162 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100163 const DexCompilationUnit* dex_compilation_unit =
164 new (graph->GetAllocator()) DexCompilationUnit(
165 handles_->NewHandle<mirror::ClassLoader>(nullptr),
166 /* class_linker */ nullptr,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000167 graph->GetDexFile(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100168 code_item,
169 /* class_def_index */ DexFile::kDexNoIndex16,
170 /* method_idx */ dex::kDexNoIndex,
171 /* access_flags */ 0u,
172 /* verified_method */ nullptr,
173 handles_->NewHandle<mirror::DexCache>(nullptr));
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800174 CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item, /*dex_method_idx*/ 0u);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800175 HGraphBuilder builder(graph, dex_compilation_unit, accessor, handles_.get(), return_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100176 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
177 return graph_built ? graph : nullptr;
178 }
179 }
180
181 private:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800182 std::vector<std::unique_ptr<const StandardDexFile>> dex_files_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100183 std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_;
184 std::unique_ptr<VariableSizedHandleScope> handles_;
185};
Roland Levillainccc07a92014-09-16 14:48:16 +0100186
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800187class OptimizingUnitTest : public CommonCompilerTest, public OptimizingUnitTestHelper {};
188
Roland Levillain72bceff2014-09-15 18:29:00 +0100189// Naive string diff data type.
190typedef std::list<std::pair<std::string, std::string>> diff_t;
191
192// An alias for the empty string used to make it clear that a line is
193// removed in a diff.
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800194static const std::string removed = ""; // NOLINT [runtime/string] [4]
Roland Levillain72bceff2014-09-15 18:29:00 +0100195
196// Naive patch command: apply a diff to a string.
197inline std::string Patch(const std::string& original, const diff_t& diff) {
198 std::string result = original;
199 for (const auto& p : diff) {
200 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000201 DCHECK_NE(pos, std::string::npos)
202 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100203 result.replace(pos, p.first.size(), p.second);
204 }
205 return result;
206}
207
Mingyao Yangf384f882014-10-22 16:08:18 -0700208// Returns if the instruction is removed from the graph.
209inline bool IsRemoved(HInstruction* instruction) {
210 return instruction->GetBlock() == nullptr;
211}
212
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100213} // namespace art
214
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000215#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_