blob: 5a910433b4cb84ef519bc617a4376ce9a6ca625f [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"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080023#include "compiler/dex/pass_manager.h"
Roland Levillainccc07a92014-09-16 14:48:16 +010024#include "dex_file.h"
25#include "dex_instruction.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000026#include "handle_scope-inl.h"
27#include "scoped_thread_state_change.h"
28#include "ssa_builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010029#include "ssa_liveness_analysis.h"
30
Roland Levillain72bceff2014-09-15 18:29:00 +010031#include "gtest/gtest.h"
32
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010033namespace art {
34
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000035#define NUM_INSTRUCTIONS(...) \
36 (sizeof((uint16_t[]) {__VA_ARGS__}) /sizeof(uint16_t))
37
Roland Levillain55dcfb52014-10-24 18:09:09 +010038#define N_REGISTERS_CODE_ITEM(NUM_REGS, ...) \
39 { NUM_REGS, 0, 0, 0, 0, 0, NUM_INSTRUCTIONS(__VA_ARGS__), 0, __VA_ARGS__ }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000040
Roland Levillain55dcfb52014-10-24 18:09:09 +010041#define ZERO_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(0, __VA_ARGS__)
42#define ONE_REGISTER_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(1, __VA_ARGS__)
43#define TWO_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(2, __VA_ARGS__)
44#define THREE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(3, __VA_ARGS__)
45#define FOUR_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(4, __VA_ARGS__)
46#define FIVE_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(5, __VA_ARGS__)
47#define SIX_REGISTERS_CODE_ITEM(...) N_REGISTERS_CODE_ITEM(6, __VA_ARGS__)
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010049LiveInterval* BuildInterval(const size_t ranges[][2],
50 size_t number_of_ranges,
51 ArenaAllocator* allocator,
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +000052 int reg = -1,
53 HInstruction* defined_by = nullptr) {
54 LiveInterval* interval = LiveInterval::MakeInterval(allocator, Primitive::kPrimInt, defined_by);
55 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()) {
67 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000068 HInstruction* current = it.Current();
69 if (current->IsSuspendCheck()) {
70 current->GetBlock()->RemoveInstruction(current);
71 }
72 }
73 }
74}
75
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010076inline HGraph* CreateGraph(ArenaAllocator* allocator) {
77 return new (allocator) HGraph(
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 allocator, *reinterpret_cast<DexFile*>(allocator->Alloc(sizeof(DexFile))), -1, false,
79 kRuntimeISA);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010080}
81
Roland Levillainccc07a92014-09-16 14:48:16 +010082// Create a control-flow graph from Dex instructions.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010083inline HGraph* CreateCFG(ArenaAllocator* allocator,
84 const uint16_t* data,
85 Primitive::Type return_type = Primitive::kPrimInt) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010086 HGraph* graph = CreateGraph(allocator);
David Brazdil5e8b1372015-01-23 14:39:08 +000087 HGraphBuilder builder(graph, return_type);
Roland Levillainccc07a92014-09-16 14:48:16 +010088 const DexFile::CodeItem* item =
89 reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000090 bool graph_built = builder.BuildGraph(*item);
91 return graph_built ? graph : nullptr;
Roland Levillainccc07a92014-09-16 14:48:16 +010092}
93
Roland Levillain72bceff2014-09-15 18:29:00 +010094// Naive string diff data type.
95typedef std::list<std::pair<std::string, std::string>> diff_t;
96
97// An alias for the empty string used to make it clear that a line is
98// removed in a diff.
99static const std::string removed = "";
100
101// Naive patch command: apply a diff to a string.
102inline std::string Patch(const std::string& original, const diff_t& diff) {
103 std::string result = original;
104 for (const auto& p : diff) {
105 std::string::size_type pos = result.find(p.first);
106 EXPECT_NE(pos, std::string::npos);
107 result.replace(pos, p.first.size(), p.second);
108 }
109 return result;
110}
111
Mingyao Yangf384f882014-10-22 16:08:18 -0700112// Returns if the instruction is removed from the graph.
113inline bool IsRemoved(HInstruction* instruction) {
114 return instruction->GetBlock() == nullptr;
115}
116
David Brazdil4833f5a2015-12-16 10:37:39 +0000117inline void TransformToSsa(HGraph* graph) {
118 ScopedObjectAccess soa(Thread::Current());
119 StackHandleScopeCollection handles(soa.Self());
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120 EXPECT_EQ(graph->TryBuildingSsa(&handles), kAnalysisSuccess);
David Brazdil4833f5a2015-12-16 10:37:39 +0000121}
122
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100123} // namespace art
124
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125#endif // ART_COMPILER_OPTIMIZING_OPTIMIZING_UNIT_TEST_H_