blob: 013e110b8779bd2c90cea83d60a5d98c9418d3ac [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"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010026#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010027#include "utils/arm/assembler_thumb2.h"
28#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
33namespace art {
34
35// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010036#ifndef ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +010037
Mathieu Chartiere401d142015-04-22 13:56:20 -070038class OptimizingCFITest : public CFITest {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010039 public:
40 // Enable this flag to generate the expected outputs.
41 static constexpr bool kGenerateExpected = false;
42
Vladimir Marko10ef6942015-10-22 15:25:54 +010043 OptimizingCFITest()
44 : pool_(),
45 allocator_(&pool_),
46 opts_(),
47 isa_features_(),
48 graph_(nullptr),
49 code_gen_(),
50 blocks_(allocator_.Adapter()) {}
51
52 void SetUpFrame(InstructionSet isa) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010053 // Setup simple context.
David Srbeckyc6b4dd82015-04-07 20:32:43 +010054 std::string error;
Andreas Gampe0415b4e2015-01-06 15:17:07 -080055 isa_features_ = InstructionSetFeatures::FromVariant(isa, "default", &error);
Vladimir Marko10ef6942015-10-22 15:25:54 +010056 graph_ = CreateGraph(&allocator_);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010057 // Generate simple frame with some spills.
Vladimir Markod58b8372016-04-12 18:51:43 +010058 code_gen_ = CodeGenerator::Create(graph_, isa, *isa_features_, opts_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010059 code_gen_->GetAssembler()->cfi().SetEnabled(true);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010060 const int frame_size = 64;
61 int core_reg = 0;
62 int fp_reg = 0;
63 for (int i = 0; i < 2; i++) { // Two registers of each kind.
64 for (; core_reg < 32; core_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010065 if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010066 auto location = Location::RegisterLocation(core_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010067 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010068 core_reg++;
69 break;
70 }
71 }
72 for (; fp_reg < 32; fp_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010073 if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010074 auto location = Location::FpuRegisterLocation(fp_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010075 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010076 fp_reg++;
77 break;
78 }
79 }
80 }
Vladimir Marko10ef6942015-10-22 15:25:54 +010081 code_gen_->block_order_ = &blocks_;
82 code_gen_->ComputeSpillMask();
83 code_gen_->SetFrameSize(frame_size);
84 code_gen_->GenerateFrameEntry();
85 }
86
87 void Finish() {
88 code_gen_->GenerateFrameExit();
89 code_gen_->Finalize(&code_allocator_);
90 }
91
92 void Check(InstructionSet isa,
93 const char* isa_str,
94 const std::vector<uint8_t>& expected_asm,
95 const std::vector<uint8_t>& expected_cfi) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010096 // Get the outputs.
Vladimir Marko10ef6942015-10-22 15:25:54 +010097 const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory();
98 Assembler* opt_asm = code_gen_->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010099 const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
100
101 if (kGenerateExpected) {
102 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
103 } else {
104 EXPECT_EQ(expected_asm, actual_asm);
105 EXPECT_EQ(expected_cfi, actual_cfi);
106 }
107 }
David Srbecky46325a02015-04-09 22:51:56 +0100108
Vladimir Marko10ef6942015-10-22 15:25:54 +0100109 void TestImpl(InstructionSet isa, const char*
110 isa_str,
111 const std::vector<uint8_t>& expected_asm,
112 const std::vector<uint8_t>& expected_cfi) {
113 SetUpFrame(isa);
114 Finish();
115 Check(isa, isa_str, expected_asm, expected_cfi);
116 }
117
118 CodeGenerator* GetCodeGenerator() {
119 return code_gen_.get();
120 }
121
David Srbecky46325a02015-04-09 22:51:56 +0100122 private:
123 class InternalCodeAllocator : public CodeAllocator {
124 public:
125 InternalCodeAllocator() {}
126
127 virtual uint8_t* Allocate(size_t size) {
128 memory_.resize(size);
129 return memory_.data();
130 }
131
132 const std::vector<uint8_t>& GetMemory() { return memory_; }
133
134 private:
135 std::vector<uint8_t> memory_;
136
137 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
138 };
Vladimir Marko10ef6942015-10-22 15:25:54 +0100139
140 ArenaPool pool_;
141 ArenaAllocator allocator_;
142 CompilerOptions opts_;
143 std::unique_ptr<const InstructionSetFeatures> isa_features_;
144 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 Marko10ef6942015-10-22 15:25:54 +0100150#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(isa, #isa, expected_asm, expected_cfi); \
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100159 }
160
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100161// TODO(VIXL): Support this test for the VIXL backend.
162#if defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_USE_VIXL_ARM_BACKEND)
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100163TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700164#endif
165#ifdef ART_ENABLE_CODEGEN_arm64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100166TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700167#endif
168#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100169TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700170#endif
171#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100172TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700173#endif
174#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100175TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700176#endif
177#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100178TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700179#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100180
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100181// TODO(VIXL): Support this test for the VIXL backend.
182#if defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_USE_VIXL_ARM_BACKEND)
Vladimir Marko10ef6942015-10-22 15:25:54 +0100183TEST_F(OptimizingCFITest, kThumb2Adjust) {
184 std::vector<uint8_t> expected_asm(
185 expected_asm_kThumb2_adjust,
186 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
187 std::vector<uint8_t> expected_cfi(
188 expected_cfi_kThumb2_adjust,
189 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
190 SetUpFrame(kThumb2);
191#define __ down_cast<arm::Thumb2Assembler*>(GetCodeGenerator()->GetAssembler())->
192 Label target;
193 __ CompareAndBranchIfZero(arm::R0, &target);
194 // Push the target out of range of CBZ.
195 for (size_t i = 0; i != 65; ++i) {
196 __ ldr(arm::R0, arm::Address(arm::R0));
197 }
198 __ Bind(&target);
199#undef __
200 Finish();
201 Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
202}
Colin Crossa75b01a2016-08-18 13:45:24 -0700203#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100204
Colin Crossa75b01a2016-08-18 13:45:24 -0700205#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100206TEST_F(OptimizingCFITest, kMipsAdjust) {
207 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
208 static constexpr size_t kNumNops = 1u + (1u << 15);
209 std::vector<uint8_t> expected_asm(
210 expected_asm_kMips_adjust_head,
211 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
212 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
213 expected_asm.insert(
214 expected_asm.end(),
215 expected_asm_kMips_adjust_tail,
216 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
217 std::vector<uint8_t> expected_cfi(
218 expected_cfi_kMips_adjust,
219 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
220 SetUpFrame(kMips);
221#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
222 mips::MipsLabel target;
223 __ Beqz(mips::A0, &target);
224 // Push the target out of range of BEQZ.
225 for (size_t i = 0; i != kNumNops; ++i) {
226 __ Nop();
227 }
228 __ Bind(&target);
229#undef __
230 Finish();
231 Check(kMips, "kMips_adjust", expected_asm, expected_cfi);
232}
Colin Crossa75b01a2016-08-18 13:45:24 -0700233#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100234
Colin Crossa75b01a2016-08-18 13:45:24 -0700235#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700236TEST_F(OptimizingCFITest, kMips64Adjust) {
237 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
238 static constexpr size_t kNumNops = 1u + (1u << 15);
239 std::vector<uint8_t> expected_asm(
240 expected_asm_kMips64_adjust_head,
241 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
242 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
243 expected_asm.insert(
244 expected_asm.end(),
245 expected_asm_kMips64_adjust_tail,
246 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
247 std::vector<uint8_t> expected_cfi(
248 expected_cfi_kMips64_adjust,
249 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
250 SetUpFrame(kMips64);
251#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
252 mips64::Mips64Label target;
253 __ Beqc(mips64::A1, mips64::A2, &target);
254 // Push the target out of range of BEQC.
255 for (size_t i = 0; i != kNumNops; ++i) {
256 __ Nop();
257 }
258 __ Bind(&target);
259#undef __
260 Finish();
261 Check(kMips64, "kMips64_adjust", expected_asm, expected_cfi);
262}
Colin Crossa75b01a2016-08-18 13:45:24 -0700263#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700264
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100265#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100266
267} // namespace art