blob: 99d5284714f3944d1ca4cb2f2b95b242bbc5283d [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"
21#include "cfi_test.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010022#include "driver/compiler_options.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010023#include "gtest/gtest.h"
24#include "optimizing/code_generator.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010025#include "optimizing/optimizing_unit_test.h"
Andreas Gampe217488a2017-09-18 08:34:42 -070026#include "read_barrier_config.h"
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000027#include "utils/arm/assembler_arm_vixl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010029#include "utils/mips/assembler_mips.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070030#include "utils/mips64/assembler_mips64.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010031
32#include "optimizing/optimizing_cfi_test_expected.inc"
33
Scott Wakeling90ab6732016-12-08 10:25:03 +000034namespace vixl32 = vixl::aarch32;
35
36using vixl32::r0;
Scott Wakeling90ab6732016-12-08 10:25:03 +000037
David Srbeckyc6b4dd82015-04-07 20:32:43 +010038namespace art {
39
40// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010041#ifndef ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +010042
Mathieu Chartiere401d142015-04-22 13:56:20 -070043class OptimizingCFITest : public CFITest {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010044 public:
45 // Enable this flag to generate the expected outputs.
46 static constexpr bool kGenerateExpected = false;
47
Vladimir Marko10ef6942015-10-22 15:25:54 +010048 OptimizingCFITest()
49 : pool_(),
50 allocator_(&pool_),
51 opts_(),
52 isa_features_(),
53 graph_(nullptr),
54 code_gen_(),
55 blocks_(allocator_.Adapter()) {}
56
57 void SetUpFrame(InstructionSet isa) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010058 // Setup simple context.
David Srbeckyc6b4dd82015-04-07 20:32:43 +010059 std::string error;
Andreas Gampe0415b4e2015-01-06 15:17:07 -080060 isa_features_ = InstructionSetFeatures::FromVariant(isa, "default", &error);
Vladimir Marko10ef6942015-10-22 15:25:54 +010061 graph_ = CreateGraph(&allocator_);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010062 // Generate simple frame with some spills.
Vladimir Markod58b8372016-04-12 18:51:43 +010063 code_gen_ = CodeGenerator::Create(graph_, isa, *isa_features_, opts_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010064 code_gen_->GetAssembler()->cfi().SetEnabled(true);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010065 const int frame_size = 64;
66 int core_reg = 0;
67 int fp_reg = 0;
68 for (int i = 0; i < 2; i++) { // Two registers of each kind.
69 for (; core_reg < 32; core_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010070 if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010071 auto location = Location::RegisterLocation(core_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010072 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010073 core_reg++;
74 break;
75 }
76 }
77 for (; fp_reg < 32; fp_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010078 if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010079 auto location = Location::FpuRegisterLocation(fp_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010080 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010081 fp_reg++;
82 break;
83 }
84 }
85 }
Vladimir Marko10ef6942015-10-22 15:25:54 +010086 code_gen_->block_order_ = &blocks_;
87 code_gen_->ComputeSpillMask();
88 code_gen_->SetFrameSize(frame_size);
89 code_gen_->GenerateFrameEntry();
90 }
91
92 void Finish() {
93 code_gen_->GenerateFrameExit();
94 code_gen_->Finalize(&code_allocator_);
95 }
96
97 void Check(InstructionSet isa,
98 const char* isa_str,
99 const std::vector<uint8_t>& expected_asm,
100 const std::vector<uint8_t>& expected_cfi) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100101 // Get the outputs.
Vladimir Marko10ef6942015-10-22 15:25:54 +0100102 const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory();
103 Assembler* opt_asm = code_gen_->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100104 const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
105
106 if (kGenerateExpected) {
107 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
108 } else {
109 EXPECT_EQ(expected_asm, actual_asm);
110 EXPECT_EQ(expected_cfi, actual_cfi);
111 }
112 }
David Srbecky46325a02015-04-09 22:51:56 +0100113
Vladimir Marko10ef6942015-10-22 15:25:54 +0100114 void TestImpl(InstructionSet isa, const char*
115 isa_str,
116 const std::vector<uint8_t>& expected_asm,
117 const std::vector<uint8_t>& expected_cfi) {
118 SetUpFrame(isa);
119 Finish();
120 Check(isa, isa_str, expected_asm, expected_cfi);
121 }
122
123 CodeGenerator* GetCodeGenerator() {
124 return code_gen_.get();
125 }
126
David Srbecky46325a02015-04-09 22:51:56 +0100127 private:
128 class InternalCodeAllocator : public CodeAllocator {
129 public:
130 InternalCodeAllocator() {}
131
132 virtual uint8_t* Allocate(size_t size) {
133 memory_.resize(size);
134 return memory_.data();
135 }
136
137 const std::vector<uint8_t>& GetMemory() { return memory_; }
138
139 private:
140 std::vector<uint8_t> memory_;
141
142 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
143 };
Vladimir Marko10ef6942015-10-22 15:25:54 +0100144
145 ArenaPool pool_;
146 ArenaAllocator allocator_;
147 CompilerOptions opts_;
148 std::unique_ptr<const InstructionSetFeatures> isa_features_;
149 HGraph* graph_;
150 std::unique_ptr<CodeGenerator> code_gen_;
151 ArenaVector<HBasicBlock*> blocks_;
152 InternalCodeAllocator code_allocator_;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100153};
154
Vladimir Marko10ef6942015-10-22 15:25:54 +0100155#define TEST_ISA(isa) \
156 TEST_F(OptimizingCFITest, isa) { \
157 std::vector<uint8_t> expected_asm( \
158 expected_asm_##isa, \
159 expected_asm_##isa + arraysize(expected_asm_##isa)); \
160 std::vector<uint8_t> expected_cfi( \
161 expected_cfi_##isa, \
162 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
163 TestImpl(isa, #isa, expected_asm, expected_cfi); \
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100164 }
165
Scott Wakeling90ab6732016-12-08 10:25:03 +0000166#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100167TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700168#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100169
Colin Crossa75b01a2016-08-18 13:45:24 -0700170#ifdef ART_ENABLE_CODEGEN_arm64
Roland Levillainaf24def2017-07-12 13:18:01 +0100171// Run the tests for ARM64 only with Baker read barriers, as the
172// expected generated code saves and restore X21 and X22 (instead of
173// X20 and X21), as X20 is used as Marking Register in the Baker read
174// barrier configuration, and as such is removed from the set of
175// callee-save registers in the ARM64 code generator of the Optimizing
176// compiler.
177#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100178TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700179#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100180#endif
181
Colin Crossa75b01a2016-08-18 13:45:24 -0700182#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100183TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700184#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100185
Colin Crossa75b01a2016-08-18 13:45:24 -0700186#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100187TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700188#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100189
Colin Crossa75b01a2016-08-18 13:45:24 -0700190#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100191TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700192#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100193
Colin Crossa75b01a2016-08-18 13:45:24 -0700194#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100195TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700196#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100197
Scott Wakeling90ab6732016-12-08 10:25:03 +0000198#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Marko10ef6942015-10-22 15:25:54 +0100199TEST_F(OptimizingCFITest, kThumb2Adjust) {
200 std::vector<uint8_t> expected_asm(
201 expected_asm_kThumb2_adjust,
202 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
203 std::vector<uint8_t> expected_cfi(
204 expected_cfi_kThumb2_adjust,
205 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
206 SetUpFrame(kThumb2);
Scott Wakeling90ab6732016-12-08 10:25:03 +0000207#define __ down_cast<arm::ArmVIXLAssembler*>(GetCodeGenerator() \
208 ->GetAssembler())->GetVIXLAssembler()->
209 vixl32::Label target;
210 __ CompareAndBranchIfZero(r0, &target);
211 // Push the target out of range of CBZ.
212 for (size_t i = 0; i != 65; ++i) {
213 __ Ldr(r0, vixl32::MemOperand(r0));
214 }
Vladimir Marko10ef6942015-10-22 15:25:54 +0100215 __ Bind(&target);
216#undef __
217 Finish();
218 Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
219}
Colin Crossa75b01a2016-08-18 13:45:24 -0700220#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100221
Colin Crossa75b01a2016-08-18 13:45:24 -0700222#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100223TEST_F(OptimizingCFITest, kMipsAdjust) {
224 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
225 static constexpr size_t kNumNops = 1u + (1u << 15);
226 std::vector<uint8_t> expected_asm(
227 expected_asm_kMips_adjust_head,
228 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
229 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
230 expected_asm.insert(
231 expected_asm.end(),
232 expected_asm_kMips_adjust_tail,
233 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
234 std::vector<uint8_t> expected_cfi(
235 expected_cfi_kMips_adjust,
236 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
237 SetUpFrame(kMips);
238#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
239 mips::MipsLabel target;
240 __ Beqz(mips::A0, &target);
241 // Push the target out of range of BEQZ.
242 for (size_t i = 0; i != kNumNops; ++i) {
243 __ Nop();
244 }
245 __ Bind(&target);
246#undef __
247 Finish();
248 Check(kMips, "kMips_adjust", expected_asm, expected_cfi);
249}
Colin Crossa75b01a2016-08-18 13:45:24 -0700250#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100251
Colin Crossa75b01a2016-08-18 13:45:24 -0700252#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700253TEST_F(OptimizingCFITest, kMips64Adjust) {
254 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
255 static constexpr size_t kNumNops = 1u + (1u << 15);
256 std::vector<uint8_t> expected_asm(
257 expected_asm_kMips64_adjust_head,
258 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
259 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
260 expected_asm.insert(
261 expected_asm.end(),
262 expected_asm_kMips64_adjust_tail,
263 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
264 std::vector<uint8_t> expected_cfi(
265 expected_cfi_kMips64_adjust,
266 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
267 SetUpFrame(kMips64);
268#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
269 mips64::Mips64Label target;
270 __ Beqc(mips64::A1, mips64::A2, &target);
271 // Push the target out of range of BEQC.
272 for (size_t i = 0; i != kNumNops; ++i) {
273 __ Nop();
274 }
275 __ Bind(&target);
276#undef __
277 Finish();
278 Check(kMips64, "kMips64_adjust", expected_asm, expected_cfi);
279}
Colin Crossa75b01a2016-08-18 13:45:24 -0700280#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700281
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100282#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100283
284} // namespace art