blob: 33f1a4affe2e338534a3b4d9d03ec55162ae709e [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 "builder.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000021#include "common_compiler_test.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010022#include "dex_file.h"
23#include "dex_instruction.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "handle_scope.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000026#include "scoped_thread_state_change.h"
27#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010028#include "ssa_liveness_analysis.h"
29
Roland Levillain72bceff2014-09-15 18:29:00 +010030#include "gtest/gtest.h"
31
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010032namespace art {
33
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000034#define NUM_INSTRUCTIONS(...) \
35 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
36
Roland Levillain55dcfb52014-10-24 18:09:09 +010037#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
38 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000039
Roland Levillain55dcfb52014-10-24 18:09:09 +010040#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
41#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
42#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
43#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
44#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
45#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
46#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010048LiveInterval* BuildInterval(const size_t ranges[][2],
49 size_t number_of_ranges,
50 ArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000051 int reg = -1,
52 HInstruction* defined_by = nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 LiveInterval* interval =
54 LiveInterval::MakeInterval(allocator, DataType::Type::kInt32, defined_by);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000055 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) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010068 if (block->GetLoopInformation() != nullptr) {
69 block->GetLoopInformation()->SetSuspendCheck(nullptr);
70 }
David Brazdilbadd8262016-02-02 16:28:56 +000071 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
72 HInstruction* current = it.Current();
73 if (current->IsSuspendCheck()) {
74 current->GetBlock()->RemoveInstruction(current);
75 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000076 }
77 }
78 }
79}
80
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010081inline HGraph* CreateGraph(ArenaAllocator* allocator) {
82 return new (allocator) HGraph(
Igor Murashkin032cacd2017-04-06 14:40:08 -070083 allocator,
84 *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))),
85 /*method_idx*/-1,
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 kRuntimeISA);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010087}
88
Roland Levillainccc07a92014-09-16 14:48:16 +010089// Create a control-flow graph from Dex instructions.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010090inline HGraph* CreateCFG(ArenaAllocator* allocator,
91 const uint16_t* data,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 DataType::Type return_type = DataType::Type::kInt32) {
Roland Levillainccc07a92014-09-16 14:48:16 +010093 const DexFile::CodeItem* item =
94 reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdilbadd8262016-02-02 16:28:56 +000095 HGraph* graph = CreateGraph(allocator);
96
97 {
98 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070099 VariableSizedHandleScope handles(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +0000100 HGraphBuilder builder(graph, *item, &handles, return_type);
101 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
David Brazdilbadd8262016-02-02 16:28:56 +0000102 return graph_built ? graph : nullptr;
103 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100104}
105
Roland Levillain72bceff2014-09-15 18:29:00 +0100106// Naive string diff data type.
107typedef std::list<std::pair<std::string, std::string>> diff_t;
108
109// An alias for the empty string used to make it clear that a line is
110// removed in a diff.
111static const std::string removed = "";
112
113// Naive patch command: apply a diff to a string.
114inline std::string Patch(const std::string& original, const diff_t& diff) {
115 std::string result = original;
116 for (const auto& p : diff) {
117 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000118 DCHECK_NE(pos, std::string::npos)
119 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100120 result.replace(pos, p.first.size(), p.second);
121 }
122 return result;
123}
124
Mingyao Yangf384f882014-10-22 16:08:18 -0700125// Returns if the instruction is removed from the graph.
126inline bool IsRemoved(HInstruction* instruction) {
127 return instruction->GetBlock() == nullptr;
128}
129
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100130} // namespace art
131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000132#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_