blob: 04301f536678d5dc827c72cf10a4dd3ca4155373 [file] [log] [blame]
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001/*
2 * Copyright (C) 2015 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#include <memory>
18#include <vector>
19
20#include "arch/instruction_set.h"
Andreas Gampe2a5d7282018-01-02 11:53:35 -080021#include "base/runtime_debug.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010022#include "cfi_test.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "driver/compiler_options.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010024#include "gtest/gtest.h"
25#include "optimizing/code_generator.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010026#include "optimizing/optimizing_unit_test.h"
Andreas Gampe217488a2017-09-18 08:34:42 -070027#include "read_barrier_config.h"
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000028#include "utils/arm/assembler_arm_vixl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010030#include "utils/mips/assembler_mips.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010032
33#include "optimizing/optimizing_cfi_test_expected.inc"
34
Scott Wakeling90ab6732016-12-08 10:25:03 +000035namespace vixl32 = vixl::aarch32;
36
David Srbeckyc6b4dd82015-04-07 20:32:43 +010037namespace art {
38
39// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010040#ifndef ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +010041
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080042class OptimizingCFITest : public CFITest, public OptimizingUnitTestHelper {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010043 public:
44 // Enable this flag to generate the expected outputs.
45 static constexpr bool kGenerateExpected = false;
46
Vladimir Marko10ef6942015-10-22 15:25:54 +010047 OptimizingCFITest()
Vladimir Markoa0431112018-06-25 09:32:54 +010048 : graph_(nullptr),
Vladimir Marko10ef6942015-10-22 15:25:54 +010049 code_gen_(),
Vladimir Markoca6fff82017-10-03 14:49:14 +010050 blocks_(GetAllocator()->Adapter()) {}
51
Vladimir Marko10ef6942015-10-22 15:25:54 +010052 void SetUpFrame(InstructionSet isa) {
Vladimir Markoa0431112018-06-25 09:32:54 +010053 OverrideInstructionSetFeatures(isa, "default");
54
Andreas Gampe2a5d7282018-01-02 11:53:35 -080055 // Ensure that slow-debug is off, so that there is no unexpected read-barrier check emitted.
56 SetRuntimeDebugFlagsEnabled(false);
57
David Srbeckyc6b4dd82015-04-07 20:32:43 +010058 // Setup simple context.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080059 graph_ = CreateGraph();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010060 // Generate simple frame with some spills.
Vladimir Markoa0431112018-06-25 09:32:54 +010061 code_gen_ = CodeGenerator::Create(graph_, *compiler_options_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010062 code_gen_->GetAssembler()->cfi().SetEnabled(true);
Vladimir Marko174b2e22017-10-12 13:34:49 +010063 code_gen_->InitializeCodeGenerationData();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010064 const int frame_size = 64;
65 int core_reg = 0;
66 int fp_reg = 0;
67 for (int i = 0; i < 2; i++) { // Two registers of each kind.
68 for (; core_reg < 32; core_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010069 if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010070 auto location = Location::RegisterLocation(core_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010071 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010072 core_reg++;
73 break;
74 }
75 }
76 for (; fp_reg < 32; fp_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010077 if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010078 auto location = Location::FpuRegisterLocation(fp_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010079 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010080 fp_reg++;
81 break;
82 }
83 }
84 }
Vladimir Marko10ef6942015-10-22 15:25:54 +010085 code_gen_->block_order_ = &blocks_;
86 code_gen_->ComputeSpillMask();
87 code_gen_->SetFrameSize(frame_size);
88 code_gen_->GenerateFrameEntry();
89 }
90
91 void Finish() {
92 code_gen_->GenerateFrameExit();
93 code_gen_->Finalize(&code_allocator_);
94 }
95
96 void Check(InstructionSet isa,
97 const char* isa_str,
98 const std::vector<uint8_t>& expected_asm,
99 const std::vector<uint8_t>& expected_cfi) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100100 // Get the outputs.
Vladimir Markoca1e0382018-04-11 09:58:41 +0000101 ArrayRef<const uint8_t> actual_asm = code_allocator_.GetMemory();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100102 Assembler* opt_asm = code_gen_->GetAssembler();
Vladimir Markoca1e0382018-04-11 09:58:41 +0000103 ArrayRef<const uint8_t> actual_cfi(*(opt_asm->cfi().data()));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100104
105 if (kGenerateExpected) {
106 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
107 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000108 EXPECT_EQ(ArrayRef<const uint8_t>(expected_asm), actual_asm);
109 EXPECT_EQ(ArrayRef<const uint8_t>(expected_cfi), actual_cfi);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100110 }
111 }
David Srbecky46325a02015-04-09 22:51:56 +0100112
Vladimir Marko10ef6942015-10-22 15:25:54 +0100113 void TestImpl(InstructionSet isa, const char*
114 isa_str,
115 const std::vector<uint8_t>& expected_asm,
116 const std::vector<uint8_t>& expected_cfi) {
117 SetUpFrame(isa);
118 Finish();
119 Check(isa, isa_str, expected_asm, expected_cfi);
120 }
121
122 CodeGenerator* GetCodeGenerator() {
123 return code_gen_.get();
124 }
125
David Srbecky46325a02015-04-09 22:51:56 +0100126 private:
127 class InternalCodeAllocator : public CodeAllocator {
128 public:
129 InternalCodeAllocator() {}
130
131 virtual uint8_t* Allocate(size_t size) {
132 memory_.resize(size);
133 return memory_.data();
134 }
135
Vladimir Markoca1e0382018-04-11 09:58:41 +0000136 ArrayRef<const uint8_t> GetMemory() const OVERRIDE { return ArrayRef<const uint8_t>(memory_); }
David Srbecky46325a02015-04-09 22:51:56 +0100137
138 private:
139 std::vector<uint8_t> memory_;
140
141 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
142 };
Vladimir Marko10ef6942015-10-22 15:25:54 +0100143
Vladimir Marko10ef6942015-10-22 15:25:54 +0100144 HGraph* graph_;
145 std::unique_ptr<CodeGenerator> code_gen_;
146 ArenaVector<HBasicBlock*> blocks_;
147 InternalCodeAllocator code_allocator_;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100148};
149
Vladimir Marko33bff252017-11-01 14:35:42 +0000150#define TEST_ISA(isa) \
151 TEST_F(OptimizingCFITest, isa) { \
152 std::vector<uint8_t> expected_asm( \
153 expected_asm_##isa, \
154 expected_asm_##isa + arraysize(expected_asm_##isa)); \
155 std::vector<uint8_t> expected_cfi( \
156 expected_cfi_##isa, \
157 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
158 TestImpl(InstructionSet::isa, #isa, expected_asm, expected_cfi); \
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100159 }
160
Scott Wakeling90ab6732016-12-08 10:25:03 +0000161#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100162TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700163#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100164
Colin Crossa75b01a2016-08-18 13:45:24 -0700165#ifdef ART_ENABLE_CODEGEN_arm64
Roland Levillainaf24def2017-07-12 13:18:01 +0100166// Run the tests for ARM64 only with Baker read barriers, as the
167// expected generated code saves and restore X21 and X22 (instead of
168// X20 and X21), as X20 is used as Marking Register in the Baker read
169// barrier configuration, and as such is removed from the set of
170// callee-save registers in the ARM64 code generator of the Optimizing
171// compiler.
172#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100173TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700174#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100175#endif
176
Colin Crossa75b01a2016-08-18 13:45:24 -0700177#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100178TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700179#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100180
Colin Crossa75b01a2016-08-18 13:45:24 -0700181#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100182TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700183#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100184
Colin Crossa75b01a2016-08-18 13:45:24 -0700185#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100186TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700187#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100188
Colin Crossa75b01a2016-08-18 13:45:24 -0700189#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100190TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700191#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100192
Scott Wakeling90ab6732016-12-08 10:25:03 +0000193#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Marko10ef6942015-10-22 15:25:54 +0100194TEST_F(OptimizingCFITest, kThumb2Adjust) {
Andreas Gampebd39d142018-07-19 11:14:42 -0700195 using vixl32::r0;
Vladimir Marko10ef6942015-10-22 15:25:54 +0100196 std::vector<uint8_t> expected_asm(
197 expected_asm_kThumb2_adjust,
198 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
199 std::vector<uint8_t> expected_cfi(
200 expected_cfi_kThumb2_adjust,
201 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
Vladimir Marko33bff252017-11-01 14:35:42 +0000202 SetUpFrame(InstructionSet::kThumb2);
Scott Wakeling90ab6732016-12-08 10:25:03 +0000203#define __ down_cast<arm::ArmVIXLAssembler*>(GetCodeGenerator() \
204 ->GetAssembler())->GetVIXLAssembler()->
205 vixl32::Label target;
206 __ CompareAndBranchIfZero(r0, &target);
207 // Push the target out of range of CBZ.
208 for (size_t i = 0; i != 65; ++i) {
209 __ Ldr(r0, vixl32::MemOperand(r0));
210 }
Vladimir Marko10ef6942015-10-22 15:25:54 +0100211 __ Bind(&target);
212#undef __
213 Finish();
Vladimir Marko33bff252017-11-01 14:35:42 +0000214 Check(InstructionSet::kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100215}
Colin Crossa75b01a2016-08-18 13:45:24 -0700216#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100217
Colin Crossa75b01a2016-08-18 13:45:24 -0700218#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100219TEST_F(OptimizingCFITest, kMipsAdjust) {
220 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
221 static constexpr size_t kNumNops = 1u + (1u << 15);
222 std::vector<uint8_t> expected_asm(
223 expected_asm_kMips_adjust_head,
224 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
225 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
226 expected_asm.insert(
227 expected_asm.end(),
228 expected_asm_kMips_adjust_tail,
229 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
230 std::vector<uint8_t> expected_cfi(
231 expected_cfi_kMips_adjust,
232 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
Vladimir Marko33bff252017-11-01 14:35:42 +0000233 SetUpFrame(InstructionSet::kMips);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100234#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
235 mips::MipsLabel target;
236 __ Beqz(mips::A0, &target);
237 // Push the target out of range of BEQZ.
238 for (size_t i = 0; i != kNumNops; ++i) {
239 __ Nop();
240 }
241 __ Bind(&target);
242#undef __
243 Finish();
Vladimir Marko33bff252017-11-01 14:35:42 +0000244 Check(InstructionSet::kMips, "kMips_adjust", expected_asm, expected_cfi);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100245}
Colin Crossa75b01a2016-08-18 13:45:24 -0700246#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100247
Colin Crossa75b01a2016-08-18 13:45:24 -0700248#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700249TEST_F(OptimizingCFITest, kMips64Adjust) {
250 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
251 static constexpr size_t kNumNops = 1u + (1u << 15);
252 std::vector<uint8_t> expected_asm(
253 expected_asm_kMips64_adjust_head,
254 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
255 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
256 expected_asm.insert(
257 expected_asm.end(),
258 expected_asm_kMips64_adjust_tail,
259 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
260 std::vector<uint8_t> expected_cfi(
261 expected_cfi_kMips64_adjust,
262 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
Vladimir Marko33bff252017-11-01 14:35:42 +0000263 SetUpFrame(InstructionSet::kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700264#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
265 mips64::Mips64Label target;
266 __ Beqc(mips64::A1, mips64::A2, &target);
267 // Push the target out of range of BEQC.
268 for (size_t i = 0; i != kNumNops; ++i) {
269 __ Nop();
270 }
271 __ Bind(&target);
272#undef __
273 Finish();
Vladimir Marko33bff252017-11-01 14:35:42 +0000274 Check(InstructionSet::kMips64, "kMips64_adjust", expected_asm, expected_cfi);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700275}
Colin Crossa75b01a2016-08-18 13:45:24 -0700276#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700277
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100278#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100279
280} // namespace art