blob: a9bc5664c0981dbdf33e18b134cb00b2c27bc5f5 [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
David Sehr3215fff2018-04-03 17:10:12 -070023#include "base/malloc_arena_pool.h"
Vladimir Markoca6fff82017-10-03 14:49:14 +010024#include "base/scoped_arena_allocator.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010025#include "builder.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000026#include "common_compiler_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/code_item_accessors-inl.h"
28#include "dex/dex_file.h"
29#include "dex/dex_instruction.h"
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080030#include "dex/standard_dex_file.h"
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000031#include "driver/dex_compilation_unit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010032#include "handle_scope-inl.h"
33#include "mirror/class_loader.h"
34#include "mirror/dex_cache.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000036#include "scoped_thread_state_change.h"
37#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010038#include "ssa_liveness_analysis.h"
39
Roland Levillain72bceff2014-09-15 18:29:00 +010040#include "gtest/gtest.h"
41
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010042namespace art {
43
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044#define NUM_INSTRUCTIONS(...) \
45 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
46
Roland Levillain55dcfb52014-10-24 18:09:09 +010047#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
48 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000049
Roland Levillain55dcfb52014-10-24 18:09:09 +010050#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
51#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
52#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
53#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
54#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
55#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
56#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058LiveInterval* BuildInterval(const size_t ranges[][2],
59 size_t number_of_ranges,
Vladimir Markoe764d2e2017-10-05 14:35:55 +010060 ScopedArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000061 int reg = -1,
62 HInstruction* defined_by = nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 LiveInterval* interval =
64 LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000065 if (defined_by != nullptr) {
66 defined_by->SetLiveInterval(interval);
67 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010068 for (size_t i = number_of_ranges; i > 0; --i) {
69 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
70 }
71 interval->SetRegister(reg);
72 return interval;
73}
74
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000075void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010076 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000077 if (block != nullptr) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010078 if (block->GetLoopInformation() != nullptr) {
79 block->GetLoopInformation()->SetSuspendCheck(nullptr);
80 }
David Brazdilbadd8262016-02-02 16:28:56 +000081 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
82 HInstruction* current = it.Current();
83 if (current->IsSuspendCheck()) {
84 current->GetBlock()->RemoveInstruction(current);
85 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000086 }
87 }
88 }
89}
90
Vladimir Markoca6fff82017-10-03 14:49:14 +010091class ArenaPoolAndAllocator {
92 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010093 ArenaPoolAndAllocator()
94 : pool_(), allocator_(&pool_), arena_stack_(&pool_), scoped_allocator_(&arena_stack_) { }
Vladimir Markoca6fff82017-10-03 14:49:14 +010095
96 ArenaAllocator* GetAllocator() { return &allocator_; }
97 ArenaStack* GetArenaStack() { return &arena_stack_; }
Vladimir Markoe764d2e2017-10-05 14:35:55 +010098 ScopedArenaAllocator* GetScopedAllocator() { return &scoped_allocator_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +010099
100 private:
David Sehr3215fff2018-04-03 17:10:12 -0700101 MallocArenaPool pool_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100102 ArenaAllocator allocator_;
103 ArenaStack arena_stack_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100104 ScopedArenaAllocator scoped_allocator_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100105};
106
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800107// Have a separate helper so the OptimizingCFITest can inherit it without causing
108// multiple inheritance errors from having two gtest as a parent twice.
109class OptimizingUnitTestHelper {
110 public:
111 OptimizingUnitTestHelper() : 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() {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800123 ArenaAllocator* const allocator = pool_and_allocator_->GetAllocator();
124
125 // Reserve a big array of 0s so the dex file constructor can offsets from the header.
126 static constexpr size_t kDexDataSize = 4 * KB;
127 const uint8_t* dex_data = reinterpret_cast<uint8_t*>(allocator->Alloc(kDexDataSize));
128
129 // Create the dex file based on the fake data. Call the constructor so that we can use virtual
130 // functions. Don't use the arena for the StandardDexFile otherwise the dex location leaks.
131 dex_files_.emplace_back(new StandardDexFile(
132 dex_data,
133 sizeof(StandardDexFile::Header),
134 "no_location",
135 /*location_checksum*/ 0,
136 /*oat_dex_file*/ nullptr,
137 /*container*/ nullptr));
138
139 return new (allocator) HGraph(
140 allocator,
141 pool_and_allocator_->GetArenaStack(),
142 *dex_files_.back(),
143 /*method_idx*/-1,
144 kRuntimeISA);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100145 }
146
147 // Create a control-flow graph from Dex instructions.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800148 HGraph* CreateCFG(const std::vector<uint16_t>& data,
149 DataType::Type return_type = DataType::Type::kInt32) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100150 HGraph* graph = CreateGraph();
151
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800152 // The code item data might not aligned to 4 bytes, copy it to ensure that.
153 const size_t code_item_size = data.size() * sizeof(data.front());
154 void* aligned_data = GetAllocator()->Alloc(code_item_size);
155 memcpy(aligned_data, &data[0], code_item_size);
156 CHECK_ALIGNED(aligned_data, StandardDexFile::CodeItem::kAlignment);
157 const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(aligned_data);
158
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 {
160 ScopedObjectAccess soa(Thread::Current());
161 if (handles_ == nullptr) {
162 handles_.reset(new VariableSizedHandleScope(soa.Self()));
163 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100164 const DexCompilationUnit* dex_compilation_unit =
165 new (graph->GetAllocator()) DexCompilationUnit(
166 handles_->NewHandle<mirror::ClassLoader>(nullptr),
167 /* class_linker */ nullptr,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000168 graph->GetDexFile(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100169 code_item,
170 /* class_def_index */ DexFile::kDexNoIndex16,
171 /* method_idx */ dex::kDexNoIndex,
172 /* access_flags */ 0u,
173 /* verified_method */ nullptr,
174 handles_->NewHandle<mirror::DexCache>(nullptr));
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800175 CodeItemDebugInfoAccessor accessor(graph->GetDexFile(), code_item, /*dex_method_idx*/ 0u);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800176 HGraphBuilder builder(graph, dex_compilation_unit, accessor, handles_.get(), return_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100177 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
178 return graph_built ? graph : nullptr;
179 }
180 }
181
182 private:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800183 std::vector<std::unique_ptr<const StandardDexFile>> dex_files_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100184 std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_;
185 std::unique_ptr<VariableSizedHandleScope> handles_;
186};
Roland Levillainccc07a92014-09-16 14:48:16 +0100187
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800188class OptimizingUnitTest : public CommonCompilerTest, public OptimizingUnitTestHelper {};
189
Roland Levillain72bceff2014-09-15 18:29:00 +0100190// Naive string diff data type.
191typedef std::list<std::pair<std::string, std::string>> diff_t;
192
193// An alias for the empty string used to make it clear that a line is
194// removed in a diff.
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800195static const std::string removed = ""; // NOLINT [runtime/string] [4]
Roland Levillain72bceff2014-09-15 18:29:00 +0100196
197// Naive patch command: apply a diff to a string.
198inline std::string Patch(const std::string& original, const diff_t& diff) {
199 std::string result = original;
200 for (const auto& p : diff) {
201 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000202 DCHECK_NE(pos, std::string::npos)
203 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100204 result.replace(pos, p.first.size(), p.second);
205 }
206 return result;
207}
208
Mingyao Yangf384f882014-10-22 16:08:18 -0700209// Returns if the instruction is removed from the graph.
210inline bool IsRemoved(HInstruction* instruction) {
211 return instruction->GetBlock() == nullptr;
212}
213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100214} // namespace art
215
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000216#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_