code_generator_x86_64.h revision 85b62f23fc6dfffe2ddd3ddfa74611666c9ff41d
19cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray/*
29cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
39cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray *
49cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
59cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * you may not use this file except in compliance with the License.
69cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * You may obtain a copy of the License at
79cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray *
89cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
99cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray *
109cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
119cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
129cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * See the License for the specific language governing permissions and
149cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray * limitations under the License.
159cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray */
169cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
179cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
189cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
199cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
209cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "code_generator.h"
2152c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle#include "dex/compiler_enums.h"
22cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle#include "driver/compiler_options.h"
239cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "nodes.h"
24ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray#include "parallel_move_resolver.h"
259cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "utils/x86_64/assembler_x86_64.h"
269cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
279cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraynamespace art {
289cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraynamespace x86_64 {
299cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3086a8d7afc7f00ff0f5ea7b8aaf4d50514250a4e6Nicolas Geoffray// Use a local definition to prevent copying mistakes.
3186a8d7afc7f00ff0f5ea7b8aaf4d50514250a4e6Nicolas Geoffraystatic constexpr size_t kX86_64WordSize = kX86_64PointerSize;
329cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
339cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraystatic constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
347fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffraystatic constexpr FloatRegister kParameterFloatRegisters[] =
357fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray    { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
369cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
379cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraystatic constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
387fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffraystatic constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
399cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
40848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Haostatic constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
41d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffraystatic constexpr size_t kRuntimeParameterCoreRegistersLength =
42d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray    arraysize(kRuntimeParameterCoreRegisters);
43d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffraystatic constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
44d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffraystatic constexpr size_t kRuntimeParameterFpuRegistersLength =
45d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray    arraysize(kRuntimeParameterFpuRegisters);
46d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray
47d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffrayclass InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
48d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray public:
49d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray  InvokeRuntimeCallingConvention()
50d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray      : CallingConvention(kRuntimeParameterCoreRegisters,
51d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray                          kRuntimeParameterCoreRegistersLength,
52d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray                          kRuntimeParameterFpuRegisters,
53e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                          kRuntimeParameterFpuRegistersLength,
54e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                          kX86_64PointerSize) {}
55d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray
56d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray private:
57d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray};
59d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray
607fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffrayclass InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
619cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
627fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray  InvokeDexCallingConvention() : CallingConvention(
637fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray      kParameterCoreRegisters,
647fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray      kParameterCoreRegistersLength,
657fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray      kParameterFloatRegisters,
66e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kParameterFloatRegistersLength,
67e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kX86_64PointerSize) {}
689cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
699cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
709cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
719cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
729cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
732d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillainclass InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
749cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
752d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  InvokeDexCallingConventionVisitorX86_64() {}
762d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  virtual ~InvokeDexCallingConventionVisitorX86_64() {}
779cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
782d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  Location GetNextLocation(Primitive::Type type) OVERRIDE;
79fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
80fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  Location GetMethodLocation() const OVERRIDE;
819cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
829cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
839cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InvokeDexCallingConvention calling_convention;
842d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain
852d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
869cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
879cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
889cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass CodeGeneratorX86_64;
8971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
90ad4450e5c3ffaa9566216cc6fafbf5c11186c467Zheng Xuclass ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
91ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray public:
92ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
93ad4450e5c3ffaa9566216cc6fafbf5c11186c467Zheng Xu      : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
94ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
95de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void EmitMove(size_t index) OVERRIDE;
96de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void EmitSwap(size_t index) OVERRIDE;
97de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void SpillScratch(int reg) OVERRIDE;
98de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void RestoreScratch(int reg) OVERRIDE;
99ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
100ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  X86_64Assembler* GetAssembler() const;
101ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
102ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray private:
103412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange32(CpuRegister reg, int mem);
104102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  void Exchange32(XmmRegister reg, int mem);
105412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange32(int mem1, int mem2);
106412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange64(CpuRegister reg, int mem);
107102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  void Exchange64(XmmRegister reg, int mem);
108412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange64(int mem1, int mem2);
109ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
110ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
111ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
112ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
113ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray};
114ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
1159cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass LocationsBuilderX86_64 : public HGraphVisitor {
1169cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1179cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
1189cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray      : HGraphVisitor(graph), codegen_(codegen) {}
1199cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
120360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray#define DECLARE_VISIT_INSTRUCTION(name, super)     \
121de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Visit##name(H##name* instr) OVERRIDE;
1229cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
123ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
124ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
1259cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1269cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#undef DECLARE_VISIT_INSTRUCTION
1279cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
128ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  void VisitInstruction(HInstruction* instruction) OVERRIDE {
129ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
130ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames               << " (id " << instruction->GetId() << ")";
131ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  }
132ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames
1339574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray private:
134e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray  void HandleInvoke(HInvoke* invoke);
1359574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray  void HandleBitwiseOperation(HBinaryOperation* operation);
1369aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle  void HandleShift(HBinaryOperation* operation);
13752c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
13852c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldGet(HInstruction* instruction);
139e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray
1409cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
1412d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
1429cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1439cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
1449cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
1459cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1469cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass InstructionCodeGeneratorX86_64 : public HGraphVisitor {
1479cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1489cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
1499cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
150360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray#define DECLARE_VISIT_INSTRUCTION(name, super)     \
151de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Visit##name(H##name* instr) OVERRIDE;
1529cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
153ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
154ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
1559cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1569cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#undef DECLARE_VISIT_INSTRUCTION
1579cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
158ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  void VisitInstruction(HInstruction* instruction) OVERRIDE {
159ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
160ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames               << " (id " << instruction->GetId() << ")";
161ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  }
162ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames
1639cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* GetAssembler() const { return assembler_; }
1649cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1659cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
1663c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // Generate code for the given suspend check. If not null, `successor`
1673c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // is the block to branch to if the suspend check is not needed, and after
1683c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // the suspend call.
1693c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
17085b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateClassInitializationCheck(SlowPathCode* slow_path, CpuRegister class_reg);
1719574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray  void HandleBitwiseOperation(HBinaryOperation* operation);
172c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateRemFP(HRem* rem);
1730f88e87085b7cf6544dadff3f555773966a6853eGuillaume Sanchez  void DivRemOneOrMinusOne(HBinaryOperation* instruction);
174b19930c5cba3cf662dce5ee057fcc9829b4cbb9cGuillaume Sanchez  void DivByPowerOfTwo(HDiv* instruction);
1750f88e87085b7cf6544dadff3f555773966a6853eGuillaume Sanchez  void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
176bacfec30ee9f2f6fdfd190f11b105b609938efcaCalin Juravle  void GenerateDivRemIntegral(HBinaryOperation* instruction);
1779aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle  void HandleShift(HBinaryOperation* operation);
17852c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void GenerateMemoryBarrier(MemBarrierKind kind);
17907276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void HandleFieldSet(HInstruction* instruction,
18007276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                      const FieldInfo& field_info,
18107276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                      bool value_can_be_null);
18252c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
183cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  void GenerateImplicitNullCheck(HNullCheck* instruction);
184cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  void GenerateExplicitNullCheck(HNullCheck* instruction);
18524f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell  void PushOntoFPStack(Location source, uint32_t temp_offset,
18624f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell                       uint32_t stack_adjustment, bool is_float);
187d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang  void GenerateTestAndBranch(HInstruction* instruction,
188d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang                             Label* true_target,
189d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang                             Label* false_target,
190d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang                             Label* always_true_target);
191c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateCompareTestAndBranch(HIf* if_inst,
192c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    HCondition* condition,
193c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    Label* true_target,
194c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    Label* false_target,
195c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    Label* always_true_target);
196c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
197fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil  void HandleGoto(HInstruction* got, HBasicBlock* successor);
1983c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray
1999cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* const assembler_;
2009cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
2019cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2029cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
2039cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
2049cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2059cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass CodeGeneratorX86_64 : public CodeGenerator {
2069cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
207fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  CodeGeneratorX86_64(HGraph* graph,
208fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell                  const X86_64InstructionSetFeatures& isa_features,
209ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  const CompilerOptions& compiler_options,
210ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  OptimizingCompilerStats* stats = nullptr);
2119cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  virtual ~CodeGeneratorX86_64() {}
2129cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
213de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameEntry() OVERRIDE;
214de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameExit() OVERRIDE;
215de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Bind(HBasicBlock* block) OVERRIDE;
216de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
217175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void MoveConstant(Location destination, int32_t value) OVERRIDE;
218de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
219de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
220de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
221de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
2229cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2238158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  // Generate code to invoke a runtime entry point.
224175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void InvokeRuntime(QuickEntrypointEnum entrypoint,
225175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     HInstruction* instruction,
226175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     uint32_t dex_pc,
227175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     SlowPathCode* slow_path) OVERRIDE;
228175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
229175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void InvokeRuntime(int32_t entry_point_offset,
2308158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     HInstruction* instruction,
2318158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     uint32_t dex_pc,
2328158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     SlowPathCode* slow_path);
2338158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames
234de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t GetWordSize() const OVERRIDE {
2359cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return kX86_64WordSize;
2369cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2379cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
238f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
239f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell    return kX86_64WordSize;
240f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  }
241f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell
242de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetLocationBuilder() OVERRIDE {
2439cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &location_builder_;
2449cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2459cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
246de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
2479cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &instruction_visitor_;
2489cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2499cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
250de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  X86_64Assembler* GetAssembler() OVERRIDE {
2519cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &assembler_;
2529cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2539cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
254eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  const X86_64Assembler& GetAssembler() const OVERRIDE {
255eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    return assembler_;
256eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
257eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
258f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
259ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray    return &move_resolver_;
260ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  }
261ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
262de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
263de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray    return GetLabelOf(block)->Position();
264de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  }
265de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
266de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
2679cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
268988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray  void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
269de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
270de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
271de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
272f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  void Finalize(CodeAllocator* allocator) OVERRIDE;
2739cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
274de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  InstructionSet GetInstructionSet() const OVERRIDE {
275412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    return InstructionSet::kX86_64;
276412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
277412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
2783c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  // Emit a write barrier.
27907276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void MarkGCCard(CpuRegister temp,
28007276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister card,
28107276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister object,
28207276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister value,
28307276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  bool value_can_be_null);
2843c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
2859cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  // Helper method to move a value between two locations.
2869cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  void Move(Location destination, Location source);
2879cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
28892a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  Label* GetLabelOf(HBasicBlock* block) const {
289dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray    return CommonGetLabelOf<Label>(block_labels_.GetRawStorage(), block);
29092a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
29192a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
292de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Initialize() OVERRIDE {
293fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko    block_labels_.SetSize(GetGraph()->GetBlocks().size());
29492a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
29592a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
296840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
297840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
298840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
299840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
30085b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
30185b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
30285b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe
30385b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
30471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
305581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
306581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
307fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
308fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell    return isa_features_;
309fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  }
310fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell
311f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int ConstantAreaStart() const {
312f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell    return constant_area_start_;
313f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  }
314f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
315f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralDoubleAddress(double v);
316f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralFloatAddress(float v);
317f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt32Address(int32_t v);
318f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt64Address(int64_t v);
319f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
32092e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  // Load a 64 bit value into a register in the most efficient manner.
32192e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  void Load64BitValue(CpuRegister dest, int64_t value);
32292e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell
323cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
324cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  void Store64BitValueToStack(Location dest, int64_t value);
325cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell
3263c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray private:
327581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  struct PcRelativeDexCacheAccessInfo {
328581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
329581550137ee3a068a14224870e71aeee924a0646Vladimir Marko        : target_dex_file(dex_file), element_offset(element_off), label() { }
330581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
331581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    const DexFile& target_dex_file;
332581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    uint32_t element_offset;
333581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    Label label;
334581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  };
335581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
33692a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  // Labels for each block that will be compiled.
33792a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  GrowableArray<Label> block_labels_;
3381cf95287364948689f6a1a320567acd7728e94a3Nicolas Geoffray  Label frame_entry_label_;
3399cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64 location_builder_;
3409cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64 instruction_visitor_;
341ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64 move_resolver_;
3429cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler assembler_;
343fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& isa_features_;
3449cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
34539dcf55a56da746e04f477f89e7b00ba1de03880Mark Mendell  // Offset to the start of the constant area in the assembled code.
346f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  // Used for fixups to the constant area.
347f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int constant_area_start_;
348f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
349581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
350581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> method_patches_;
351581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
352581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // PC-relative DexCache access info.
353581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<PcRelativeDexCacheAccessInfo> pc_rel_dex_cache_patches_;
354581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
355581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // When we don't know the proper offset for the value, we use kDummy32BitOffset.
356581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // We will fix this up in the linker later to have the right value.
357581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  static constexpr int32_t kDummy32BitOffset = 256;
358581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
3599cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
3609cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
3619cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3629cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace x86_64
3639cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace art
3649cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3659cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
366