blob: c24f1de93d3480e0cef20d66cdb3c2bc61cfa290 [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"
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000026#include "utils/arm/assembler_arm_vixl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010028#include "utils/mips/assembler_mips.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070029#include "utils/mips64/assembler_mips64.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010030
31#include "optimizing/optimizing_cfi_test_expected.inc"
32
Scott Wakeling90ab6732016-12-08 10:25:03 +000033namespace vixl32 = vixl::aarch32;
34
35using vixl32::r0;
Scott Wakeling90ab6732016-12-08 10:25:03 +000036
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 Chartiere401d142015-04-22 13:56:20 -070042class OptimizingCFITest : public CFITest {
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()
48 : pool_(),
49 allocator_(&pool_),
50 opts_(),
51 isa_features_(),
52 graph_(nullptr),
53 code_gen_(),
54 blocks_(allocator_.Adapter()) {}
55
56 void SetUpFrame(InstructionSet isa) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010057 // Setup simple context.
David Srbeckyc6b4dd82015-04-07 20:32:43 +010058 std::string error;
Andreas Gampe0415b4e2015-01-06 15:17:07 -080059 isa_features_ = InstructionSetFeatures::FromVariant(isa, "default", &error);
Vladimir Marko10ef6942015-10-22 15:25:54 +010060 graph_ = CreateGraph(&allocator_);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010061 // Generate simple frame with some spills.
Vladimir Markod58b8372016-04-12 18:51:43 +010062 code_gen_ = CodeGenerator::Create(graph_, isa, *isa_features_, opts_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010063 code_gen_->GetAssembler()->cfi().SetEnabled(true);
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 Marko10ef6942015-10-22 15:25:54 +0100101 const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory();
102 Assembler* opt_asm = code_gen_->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100103 const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
104
105 if (kGenerateExpected) {
106 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
107 } else {
108 EXPECT_EQ(expected_asm, actual_asm);
109 EXPECT_EQ(expected_cfi, actual_cfi);
110 }
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
136 const std::vector<uint8_t>& GetMemory() { return memory_; }
137
138 private:
139 std::vector<uint8_t> memory_;
140
141 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
142 };
Vladimir Marko10ef6942015-10-22 15:25:54 +0100143
144 ArenaPool pool_;
145 ArenaAllocator allocator_;
146 CompilerOptions opts_;
147 std::unique_ptr<const InstructionSetFeatures> isa_features_;
148 HGraph* graph_;
149 std::unique_ptr<CodeGenerator> code_gen_;
150 ArenaVector<HBasicBlock*> blocks_;
151 InternalCodeAllocator code_allocator_;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100152};
153
Vladimir Marko10ef6942015-10-22 15:25:54 +0100154#define TEST_ISA(isa) \
155 TEST_F(OptimizingCFITest, isa) { \
156 std::vector<uint8_t> expected_asm( \
157 expected_asm_##isa, \
158 expected_asm_##isa + arraysize(expected_asm_##isa)); \
159 std::vector<uint8_t> expected_cfi( \
160 expected_cfi_##isa, \
161 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
162 TestImpl(isa, #isa, expected_asm, expected_cfi); \
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100163 }
164
Scott Wakeling90ab6732016-12-08 10:25:03 +0000165#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100166TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700167#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100168
Colin Crossa75b01a2016-08-18 13:45:24 -0700169#ifdef ART_ENABLE_CODEGEN_arm64
Roland Levillainaf24def2017-07-12 13:18:01 +0100170// Run the tests for ARM64 only with Baker read barriers, as the
171// expected generated code saves and restore X21 and X22 (instead of
172// X20 and X21), as X20 is used as Marking Register in the Baker read
173// barrier configuration, and as such is removed from the set of
174// callee-save registers in the ARM64 code generator of the Optimizing
175// compiler.
176#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100177TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700178#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100179#endif
180
Colin Crossa75b01a2016-08-18 13:45:24 -0700181#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100182TEST_ISA(kX86)
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_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100186TEST_ISA(kX86_64)
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_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100190TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700191#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100192
Colin Crossa75b01a2016-08-18 13:45:24 -0700193#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100194TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700195#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100196
Scott Wakeling90ab6732016-12-08 10:25:03 +0000197#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Marko10ef6942015-10-22 15:25:54 +0100198TEST_F(OptimizingCFITest, kThumb2Adjust) {
199 std::vector<uint8_t> expected_asm(
200 expected_asm_kThumb2_adjust,
201 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
202 std::vector<uint8_t> expected_cfi(
203 expected_cfi_kThumb2_adjust,
204 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
205 SetUpFrame(kThumb2);
Scott Wakeling90ab6732016-12-08 10:25:03 +0000206#define __ down_cast<arm::ArmVIXLAssembler*>(GetCodeGenerator() \
207 ->GetAssembler())->GetVIXLAssembler()->
208 vixl32::Label target;
209 __ CompareAndBranchIfZero(r0, &target);
210 // Push the target out of range of CBZ.
211 for (size_t i = 0; i != 65; ++i) {
212 __ Ldr(r0, vixl32::MemOperand(r0));
213 }
Vladimir Marko10ef6942015-10-22 15:25:54 +0100214 __ Bind(&target);
215#undef __
216 Finish();
217 Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
218}
Colin Crossa75b01a2016-08-18 13:45:24 -0700219#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100220
Colin Crossa75b01a2016-08-18 13:45:24 -0700221#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100222TEST_F(OptimizingCFITest, kMipsAdjust) {
223 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
224 static constexpr size_t kNumNops = 1u + (1u << 15);
225 std::vector<uint8_t> expected_asm(
226 expected_asm_kMips_adjust_head,
227 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
228 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
229 expected_asm.insert(
230 expected_asm.end(),
231 expected_asm_kMips_adjust_tail,
232 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
233 std::vector<uint8_t> expected_cfi(
234 expected_cfi_kMips_adjust,
235 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
236 SetUpFrame(kMips);
237#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
238 mips::MipsLabel target;
239 __ Beqz(mips::A0, &target);
240 // Push the target out of range of BEQZ.
241 for (size_t i = 0; i != kNumNops; ++i) {
242 __ Nop();
243 }
244 __ Bind(&target);
245#undef __
246 Finish();
247 Check(kMips, "kMips_adjust", expected_asm, expected_cfi);
248}
Colin Crossa75b01a2016-08-18 13:45:24 -0700249#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100250
Colin Crossa75b01a2016-08-18 13:45:24 -0700251#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700252TEST_F(OptimizingCFITest, kMips64Adjust) {
253 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
254 static constexpr size_t kNumNops = 1u + (1u << 15);
255 std::vector<uint8_t> expected_asm(
256 expected_asm_kMips64_adjust_head,
257 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
258 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
259 expected_asm.insert(
260 expected_asm.end(),
261 expected_asm_kMips64_adjust_tail,
262 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
263 std::vector<uint8_t> expected_cfi(
264 expected_cfi_kMips64_adjust,
265 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
266 SetUpFrame(kMips64);
267#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
268 mips64::Mips64Label target;
269 __ Beqc(mips64::A1, mips64::A2, &target);
270 // Push the target out of range of BEQC.
271 for (size_t i = 0; i != kNumNops; ++i) {
272 __ Nop();
273 }
274 __ Bind(&target);
275#undef __
276 Finish();
277 Check(kMips64, "kMips64_adjust", expected_asm, expected_cfi);
278}
Colin Crossa75b01a2016-08-18 13:45:24 -0700279#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700280
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100281#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100282
283} // namespace art