blob: 0e0231167205d56f1ee32942b703876f12af8b56 [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"
Scott Wakeling90ab6732016-12-08 10:25:03 +000027#ifdef ART_USE_VIXL_ARM_BACKEND
28#include "utils/arm/assembler_arm_vixl.h"
29#else
Vladimir Marko10ef6942015-10-22 15:25:54 +010030#include "utils/arm/assembler_thumb2.h"
Scott Wakeling90ab6732016-12-08 10:25:03 +000031#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +010032#include "utils/mips/assembler_mips.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070033#include "utils/mips64/assembler_mips64.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010034
35#include "optimizing/optimizing_cfi_test_expected.inc"
36
Scott Wakeling90ab6732016-12-08 10:25:03 +000037#ifdef ART_USE_VIXL_ARM_BACKEND
38namespace vixl32 = vixl::aarch32;
39
40using vixl32::r0;
41#endif
42
David Srbeckyc6b4dd82015-04-07 20:32:43 +010043namespace art {
44
45// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010046#ifndef ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +010047
Mathieu Chartiere401d142015-04-22 13:56:20 -070048class OptimizingCFITest : public CFITest {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010049 public:
50 // Enable this flag to generate the expected outputs.
51 static constexpr bool kGenerateExpected = false;
52
Vladimir Marko10ef6942015-10-22 15:25:54 +010053 OptimizingCFITest()
54 : pool_(),
55 allocator_(&pool_),
56 opts_(),
57 isa_features_(),
58 graph_(nullptr),
59 code_gen_(),
60 blocks_(allocator_.Adapter()) {}
61
62 void SetUpFrame(InstructionSet isa) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010063 // Setup simple context.
David Srbeckyc6b4dd82015-04-07 20:32:43 +010064 std::string error;
Andreas Gampe0415b4e2015-01-06 15:17:07 -080065 isa_features_ = InstructionSetFeatures::FromVariant(isa, "default", &error);
Vladimir Marko10ef6942015-10-22 15:25:54 +010066 graph_ = CreateGraph(&allocator_);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010067 // Generate simple frame with some spills.
Vladimir Markod58b8372016-04-12 18:51:43 +010068 code_gen_ = CodeGenerator::Create(graph_, isa, *isa_features_, opts_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010069 code_gen_->GetAssembler()->cfi().SetEnabled(true);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010070 const int frame_size = 64;
71 int core_reg = 0;
72 int fp_reg = 0;
73 for (int i = 0; i < 2; i++) { // Two registers of each kind.
74 for (; core_reg < 32; core_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010075 if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010076 auto location = Location::RegisterLocation(core_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010077 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010078 core_reg++;
79 break;
80 }
81 }
82 for (; fp_reg < 32; fp_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010083 if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010084 auto location = Location::FpuRegisterLocation(fp_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010085 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010086 fp_reg++;
87 break;
88 }
89 }
90 }
Vladimir Marko10ef6942015-10-22 15:25:54 +010091 code_gen_->block_order_ = &blocks_;
92 code_gen_->ComputeSpillMask();
93 code_gen_->SetFrameSize(frame_size);
94 code_gen_->GenerateFrameEntry();
95 }
96
97 void Finish() {
98 code_gen_->GenerateFrameExit();
99 code_gen_->Finalize(&code_allocator_);
100 }
101
102 void Check(InstructionSet isa,
103 const char* isa_str,
104 const std::vector<uint8_t>& expected_asm,
105 const std::vector<uint8_t>& expected_cfi) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100106 // Get the outputs.
Vladimir Marko10ef6942015-10-22 15:25:54 +0100107 const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory();
108 Assembler* opt_asm = code_gen_->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100109 const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
110
111 if (kGenerateExpected) {
112 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
113 } else {
114 EXPECT_EQ(expected_asm, actual_asm);
115 EXPECT_EQ(expected_cfi, actual_cfi);
116 }
117 }
David Srbecky46325a02015-04-09 22:51:56 +0100118
Vladimir Marko10ef6942015-10-22 15:25:54 +0100119 void TestImpl(InstructionSet isa, const char*
120 isa_str,
121 const std::vector<uint8_t>& expected_asm,
122 const std::vector<uint8_t>& expected_cfi) {
123 SetUpFrame(isa);
124 Finish();
125 Check(isa, isa_str, expected_asm, expected_cfi);
126 }
127
128 CodeGenerator* GetCodeGenerator() {
129 return code_gen_.get();
130 }
131
David Srbecky46325a02015-04-09 22:51:56 +0100132 private:
133 class InternalCodeAllocator : public CodeAllocator {
134 public:
135 InternalCodeAllocator() {}
136
137 virtual uint8_t* Allocate(size_t size) {
138 memory_.resize(size);
139 return memory_.data();
140 }
141
142 const std::vector<uint8_t>& GetMemory() { return memory_; }
143
144 private:
145 std::vector<uint8_t> memory_;
146
147 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
148 };
Vladimir Marko10ef6942015-10-22 15:25:54 +0100149
150 ArenaPool pool_;
151 ArenaAllocator allocator_;
152 CompilerOptions opts_;
153 std::unique_ptr<const InstructionSetFeatures> isa_features_;
154 HGraph* graph_;
155 std::unique_ptr<CodeGenerator> code_gen_;
156 ArenaVector<HBasicBlock*> blocks_;
157 InternalCodeAllocator code_allocator_;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100158};
159
Vladimir Marko10ef6942015-10-22 15:25:54 +0100160#define TEST_ISA(isa) \
161 TEST_F(OptimizingCFITest, isa) { \
162 std::vector<uint8_t> expected_asm( \
163 expected_asm_##isa, \
164 expected_asm_##isa + arraysize(expected_asm_##isa)); \
165 std::vector<uint8_t> expected_cfi( \
166 expected_cfi_##isa, \
167 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
168 TestImpl(isa, #isa, expected_asm, expected_cfi); \
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100169 }
170
Scott Wakeling90ab6732016-12-08 10:25:03 +0000171#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100172TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700173#endif
174#ifdef ART_ENABLE_CODEGEN_arm64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100175TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700176#endif
177#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100178TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700179#endif
180#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100181TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700182#endif
183#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100184TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700185#endif
186#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100187TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700188#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100189
Scott Wakeling90ab6732016-12-08 10:25:03 +0000190#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Marko10ef6942015-10-22 15:25:54 +0100191TEST_F(OptimizingCFITest, kThumb2Adjust) {
192 std::vector<uint8_t> expected_asm(
193 expected_asm_kThumb2_adjust,
194 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
195 std::vector<uint8_t> expected_cfi(
196 expected_cfi_kThumb2_adjust,
197 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
198 SetUpFrame(kThumb2);
Scott Wakeling90ab6732016-12-08 10:25:03 +0000199#ifdef ART_USE_VIXL_ARM_BACKEND
200#define __ down_cast<arm::ArmVIXLAssembler*>(GetCodeGenerator() \
201 ->GetAssembler())->GetVIXLAssembler()->
202 vixl32::Label target;
203 __ CompareAndBranchIfZero(r0, &target);
204 // Push the target out of range of CBZ.
205 for (size_t i = 0; i != 65; ++i) {
206 __ Ldr(r0, vixl32::MemOperand(r0));
207 }
208#else
Vladimir Marko10ef6942015-10-22 15:25:54 +0100209#define __ down_cast<arm::Thumb2Assembler*>(GetCodeGenerator()->GetAssembler())->
210 Label target;
211 __ CompareAndBranchIfZero(arm::R0, &target);
212 // Push the target out of range of CBZ.
213 for (size_t i = 0; i != 65; ++i) {
214 __ ldr(arm::R0, arm::Address(arm::R0));
215 }
Scott Wakeling90ab6732016-12-08 10:25:03 +0000216#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100217 __ Bind(&target);
218#undef __
219 Finish();
220 Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
221}
Colin Crossa75b01a2016-08-18 13:45:24 -0700222#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100223
Colin Crossa75b01a2016-08-18 13:45:24 -0700224#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100225TEST_F(OptimizingCFITest, kMipsAdjust) {
226 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
227 static constexpr size_t kNumNops = 1u + (1u << 15);
228 std::vector<uint8_t> expected_asm(
229 expected_asm_kMips_adjust_head,
230 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
231 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
232 expected_asm.insert(
233 expected_asm.end(),
234 expected_asm_kMips_adjust_tail,
235 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
236 std::vector<uint8_t> expected_cfi(
237 expected_cfi_kMips_adjust,
238 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
239 SetUpFrame(kMips);
240#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
241 mips::MipsLabel target;
242 __ Beqz(mips::A0, &target);
243 // Push the target out of range of BEQZ.
244 for (size_t i = 0; i != kNumNops; ++i) {
245 __ Nop();
246 }
247 __ Bind(&target);
248#undef __
249 Finish();
250 Check(kMips, "kMips_adjust", expected_asm, expected_cfi);
251}
Colin Crossa75b01a2016-08-18 13:45:24 -0700252#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100253
Colin Crossa75b01a2016-08-18 13:45:24 -0700254#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700255TEST_F(OptimizingCFITest, kMips64Adjust) {
256 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
257 static constexpr size_t kNumNops = 1u + (1u << 15);
258 std::vector<uint8_t> expected_asm(
259 expected_asm_kMips64_adjust_head,
260 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
261 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
262 expected_asm.insert(
263 expected_asm.end(),
264 expected_asm_kMips64_adjust_tail,
265 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
266 std::vector<uint8_t> expected_cfi(
267 expected_cfi_kMips64_adjust,
268 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
269 SetUpFrame(kMips64);
270#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
271 mips64::Mips64Label target;
272 __ Beqc(mips64::A1, mips64::A2, &target);
273 // Push the target out of range of BEQC.
274 for (size_t i = 0; i != kNumNops; ++i) {
275 __ Nop();
276 }
277 __ Bind(&target);
278#undef __
279 Finish();
280 Check(kMips64, "kMips64_adjust", expected_asm, expected_cfi);
281}
Colin Crossa75b01a2016-08-18 13:45:24 -0700282#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700283
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100284#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100285
286} // namespace art