blob: a3e4c616b884b5fffad2ce1e9ea022d9ea2e8266 [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"
Vladimir Markoe2727152019-10-10 10:46:42 +010021#include "base/macros.h"
Andreas Gampe2a5d7282018-01-02 11:53:35 -080022#include "base/runtime_debug.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010023#include "cfi_test.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010024#include "driver/compiler_options.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010025#include "gtest/gtest.h"
26#include "optimizing/code_generator.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010027#include "optimizing/optimizing_unit_test.h"
Andreas Gampe217488a2017-09-18 08:34:42 -070028#include "read_barrier_config.h"
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000029#include "utils/arm/assembler_arm_vixl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010031#include "utils/mips/assembler_mips.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010033
34#include "optimizing/optimizing_cfi_test_expected.inc"
35
Scott Wakeling90ab6732016-12-08 10:25:03 +000036namespace vixl32 = vixl::aarch32;
37
Vladimir Markoe2727152019-10-10 10:46:42 +010038namespace art HIDDEN {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010039
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 Chartierfa3db3d2018-01-12 14:42:18 -080043class OptimizingCFITest : public CFITest, public OptimizingUnitTestHelper {
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()
Vladimir Markoa0431112018-06-25 09:32:54 +010049 : graph_(nullptr),
Vladimir Marko10ef6942015-10-22 15:25:54 +010050 code_gen_(),
Vladimir Markoca6fff82017-10-03 14:49:14 +010051 blocks_(GetAllocator()->Adapter()) {}
52
Vladimir Marko10ef6942015-10-22 15:25:54 +010053 void SetUpFrame(InstructionSet isa) {
Vladimir Markoa0431112018-06-25 09:32:54 +010054 OverrideInstructionSetFeatures(isa, "default");
55
Andreas Gampe2a5d7282018-01-02 11:53:35 -080056 // Ensure that slow-debug is off, so that there is no unexpected read-barrier check emitted.
57 SetRuntimeDebugFlagsEnabled(false);
58
David Srbeckyc6b4dd82015-04-07 20:32:43 +010059 // Setup simple context.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080060 graph_ = CreateGraph();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010061 // Generate simple frame with some spills.
Vladimir Markoa0431112018-06-25 09:32:54 +010062 code_gen_ = CodeGenerator::Create(graph_, *compiler_options_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010063 code_gen_->GetAssembler()->cfi().SetEnabled(true);
Vladimir Marko174b2e22017-10-12 13:34:49 +010064 code_gen_->InitializeCodeGenerationData();
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 Markoca1e0382018-04-11 09:58:41 +0000102 ArrayRef<const uint8_t> actual_asm = code_allocator_.GetMemory();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100103 Assembler* opt_asm = code_gen_->GetAssembler();
Vladimir Markoca1e0382018-04-11 09:58:41 +0000104 ArrayRef<const uint8_t> actual_cfi(*(opt_asm->cfi().data()));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100105
106 if (kGenerateExpected) {
107 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
108 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000109 EXPECT_EQ(ArrayRef<const uint8_t>(expected_asm), actual_asm);
110 EXPECT_EQ(ArrayRef<const uint8_t>(expected_cfi), actual_cfi);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100111 }
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
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700132 uint8_t* Allocate(size_t size) override {
David Srbecky46325a02015-04-09 22:51:56 +0100133 memory_.resize(size);
134 return memory_.data();
135 }
136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100137 ArrayRef<const uint8_t> GetMemory() const override { return ArrayRef<const uint8_t>(memory_); }
David Srbecky46325a02015-04-09 22:51:56 +0100138
139 private:
140 std::vector<uint8_t> memory_;
141
142 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
143 };
Vladimir Marko10ef6942015-10-22 15:25:54 +0100144
Vladimir Marko10ef6942015-10-22 15:25:54 +0100145 HGraph* graph_;
146 std::unique_ptr<CodeGenerator> code_gen_;
147 ArenaVector<HBasicBlock*> blocks_;
148 InternalCodeAllocator code_allocator_;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100149};
150
Vladimir Marko33bff252017-11-01 14:35:42 +0000151#define TEST_ISA(isa) \
152 TEST_F(OptimizingCFITest, isa) { \
153 std::vector<uint8_t> expected_asm( \
154 expected_asm_##isa, \
155 expected_asm_##isa + arraysize(expected_asm_##isa)); \
156 std::vector<uint8_t> expected_cfi( \
157 expected_cfi_##isa, \
158 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
159 TestImpl(InstructionSet::isa, #isa, expected_asm, expected_cfi); \
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100160 }
161
Scott Wakeling90ab6732016-12-08 10:25:03 +0000162#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100163TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700164#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100165
Colin Crossa75b01a2016-08-18 13:45:24 -0700166#ifdef ART_ENABLE_CODEGEN_arm64
Roland Levillainaf24def2017-07-12 13:18:01 +0100167// Run the tests for ARM64 only with Baker read barriers, as the
168// expected generated code saves and restore X21 and X22 (instead of
169// X20 and X21), as X20 is used as Marking Register in the Baker read
170// barrier configuration, and as such is removed from the set of
171// callee-save registers in the ARM64 code generator of the Optimizing
172// compiler.
173#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100174TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700175#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100176#endif
177
Colin Crossa75b01a2016-08-18 13:45:24 -0700178#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100179TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700180#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100181
Colin Crossa75b01a2016-08-18 13:45:24 -0700182#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100183TEST_ISA(kX86_64)
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_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100187TEST_ISA(kMips)
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_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100191TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700192#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100193
Scott Wakeling90ab6732016-12-08 10:25:03 +0000194#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Marko10ef6942015-10-22 15:25:54 +0100195TEST_F(OptimizingCFITest, kThumb2Adjust) {
Andreas Gampebd39d142018-07-19 11:14:42 -0700196 using vixl32::r0;
Vladimir Marko10ef6942015-10-22 15:25:54 +0100197 std::vector<uint8_t> expected_asm(
198 expected_asm_kThumb2_adjust,
199 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
200 std::vector<uint8_t> expected_cfi(
201 expected_cfi_kThumb2_adjust,
202 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
Vladimir Marko33bff252017-11-01 14:35:42 +0000203 SetUpFrame(InstructionSet::kThumb2);
Scott Wakeling90ab6732016-12-08 10:25:03 +0000204#define __ down_cast<arm::ArmVIXLAssembler*>(GetCodeGenerator() \
205 ->GetAssembler())->GetVIXLAssembler()->
206 vixl32::Label target;
207 __ CompareAndBranchIfZero(r0, &target);
208 // Push the target out of range of CBZ.
209 for (size_t i = 0; i != 65; ++i) {
210 __ Ldr(r0, vixl32::MemOperand(r0));
211 }
Vladimir Marko10ef6942015-10-22 15:25:54 +0100212 __ Bind(&target);
213#undef __
214 Finish();
Vladimir Marko33bff252017-11-01 14:35:42 +0000215 Check(InstructionSet::kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100216}
Colin Crossa75b01a2016-08-18 13:45:24 -0700217#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100218
Colin Crossa75b01a2016-08-18 13:45:24 -0700219#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100220TEST_F(OptimizingCFITest, kMipsAdjust) {
221 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
222 static constexpr size_t kNumNops = 1u + (1u << 15);
223 std::vector<uint8_t> expected_asm(
224 expected_asm_kMips_adjust_head,
225 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
226 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
227 expected_asm.insert(
228 expected_asm.end(),
229 expected_asm_kMips_adjust_tail,
230 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
231 std::vector<uint8_t> expected_cfi(
232 expected_cfi_kMips_adjust,
233 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
Vladimir Marko33bff252017-11-01 14:35:42 +0000234 SetUpFrame(InstructionSet::kMips);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100235#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
236 mips::MipsLabel target;
237 __ Beqz(mips::A0, &target);
238 // Push the target out of range of BEQZ.
239 for (size_t i = 0; i != kNumNops; ++i) {
240 __ Nop();
241 }
242 __ Bind(&target);
243#undef __
244 Finish();
Vladimir Marko33bff252017-11-01 14:35:42 +0000245 Check(InstructionSet::kMips, "kMips_adjust", expected_asm, expected_cfi);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100246}
Colin Crossa75b01a2016-08-18 13:45:24 -0700247#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100248
Colin Crossa75b01a2016-08-18 13:45:24 -0700249#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700250TEST_F(OptimizingCFITest, kMips64Adjust) {
251 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
252 static constexpr size_t kNumNops = 1u + (1u << 15);
253 std::vector<uint8_t> expected_asm(
254 expected_asm_kMips64_adjust_head,
255 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
256 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
257 expected_asm.insert(
258 expected_asm.end(),
259 expected_asm_kMips64_adjust_tail,
260 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
261 std::vector<uint8_t> expected_cfi(
262 expected_cfi_kMips64_adjust,
263 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
Vladimir Marko33bff252017-11-01 14:35:42 +0000264 SetUpFrame(InstructionSet::kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700265#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
266 mips64::Mips64Label target;
267 __ Beqc(mips64::A1, mips64::A2, &target);
268 // Push the target out of range of BEQC.
269 for (size_t i = 0; i != kNumNops; ++i) {
270 __ Nop();
271 }
272 __ Bind(&target);
273#undef __
274 Finish();
Vladimir Marko33bff252017-11-01 14:35:42 +0000275 Check(InstructionSet::kMips64, "kMips64_adjust", expected_asm, expected_cfi);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700276}
Colin Crossa75b01a2016-08-18 13:45:24 -0700277#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700278
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100279#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100280
281} // namespace art