blob: 58d90176cd037b9552d060bb96562a168e568d23 [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 "nodes.h"
21#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"
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) {
53 LiveInterval* interval = LiveInterval::MakeInterval(allocator, Primitive::kPrimInt, defined_by);
54 if (defined_by != nullptr) {
55 defined_by->SetLiveInterval(interval);
56 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057 for (size_t i = number_of_ranges; i > 0; --i) {
58 interval->AddRange(ranges[i - 1][0], ranges[i - 1][1]);
59 }
60 interval->SetRegister(reg);
61 return interval;
62}
63
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000064void RemoveSuspendChecks(HGraph* graph) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010065 for (HBasicBlock* block : graph->GetBlocks()) {
David Brazdilbadd8262016-02-02 16:28:56 +000066 if (block != nullptr) {
67 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
68 HInstruction* current = it.Current();
69 if (current->IsSuspendCheck()) {
70 current->GetBlock()->RemoveInstruction(current);
71 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000072 }
73 }
74 }
75}
76
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010077inline HGraph* CreateGraph(ArenaAllocator* allocator) {
78 return new (allocator) HGraph(
Mathieu Chartiere401d142015-04-22 13:56:20 -070079 allocator, *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))), -1, false,
80 kRuntimeISA);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010081}
82
Roland Levillainccc07a92014-09-16 14:48:16 +010083// Create a control-flow graph from Dex instructions.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010084inline HGraph* CreateCFG(ArenaAllocator* allocator,
85 const uint16_t* data,
86 Primitive::Type return_type = Primitive::kPrimInt) {
Roland Levillainccc07a92014-09-16 14:48:16 +010087 const DexFile::CodeItem* item =
88 reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdilbadd8262016-02-02 16:28:56 +000089 HGraph* graph = CreateGraph(allocator);
90
91 {
92 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070093 VariableSizedHandleScope handles(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +000094 HGraphBuilder builder(graph, *item, &handles, return_type);
95 bool graph_built = (builder.BuildGraph() == kAnalysisSuccess);
David Brazdilbadd8262016-02-02 16:28:56 +000096 return graph_built ? graph : nullptr;
97 }
Roland Levillainccc07a92014-09-16 14:48:16 +010098}
99
Roland Levillain72bceff2014-09-15 18:29:00 +0100100// Naive string diff data type.
101typedef std::list<std::pair<std::string, std::string>> diff_t;
102
103// An alias for the empty string used to make it clear that a line is
104// removed in a diff.
105static const std::string removed = "";
106
107// Naive patch command: apply a diff to a string.
108inline std::string Patch(const std::string& original, const diff_t& diff) {
109 std::string result = original;
110 for (const auto& p : diff) {
111 std::string::size_type pos = result.find(p.first);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000112 DCHECK_NE(pos, std::string::npos)
113 << "Could not find: \"" << p.first << "\" in \"" << result << "\"";
Roland Levillain72bceff2014-09-15 18:29:00 +0100114 result.replace(pos, p.first.size(), p.second);
115 }
116 return result;
117}
118
Mingyao Yangf384f882014-10-22 16:08:18 -0700119// Returns if the instruction is removed from the graph.
120inline bool IsRemoved(HInstruction* instruction) {
121 return instruction->GetBlock() == nullptr;
122}
123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124} // namespace art
125
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_