code_generator_x86_64.h revision ecc4366670e12b4812ef1653f7c8d52234ca1b1f
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
9071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampeclass SlowPathCodeX86_64 : public SlowPathCode {
9171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe public:
9271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
9371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
9471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  Label* GetEntryLabel() { return &entry_label_; }
9571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  Label* GetExitLabel() { return &exit_label_; }
9671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
9771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe private:
9871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  Label entry_label_;
9971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  Label exit_label_;
10071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
10171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
10271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe};
1039cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
104ad4450e5c3ffaa9566216cc6fafbf5c11186c467Zheng Xuclass ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
105ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray public:
106ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
107ad4450e5c3ffaa9566216cc6fafbf5c11186c467Zheng Xu      : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
108ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
109de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void EmitMove(size_t index) OVERRIDE;
110de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void EmitSwap(size_t index) OVERRIDE;
111de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void SpillScratch(int reg) OVERRIDE;
112de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void RestoreScratch(int reg) OVERRIDE;
113ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
114ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  X86_64Assembler* GetAssembler() const;
115ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
116ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray private:
117412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange32(CpuRegister reg, int mem);
118102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  void Exchange32(XmmRegister reg, int mem);
119412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange32(int mem1, int mem2);
120412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange64(CpuRegister reg, int mem);
121102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  void Exchange64(XmmRegister reg, int mem);
122412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange64(int mem1, int mem2);
123ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
124ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
125ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
126ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
127ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray};
128ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
1299cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass LocationsBuilderX86_64 : public HGraphVisitor {
1309cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1319cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
1329cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray      : HGraphVisitor(graph), codegen_(codegen) {}
1339cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
134360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray#define DECLARE_VISIT_INSTRUCTION(name, super)     \
135de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Visit##name(H##name* instr) OVERRIDE;
1369cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
137ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
138ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
1399cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1409cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#undef DECLARE_VISIT_INSTRUCTION
1419cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
142ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  void VisitInstruction(HInstruction* instruction) OVERRIDE {
143ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
144ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames               << " (id " << instruction->GetId() << ")";
145ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  }
146ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames
1479574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray private:
148e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray  void HandleInvoke(HInvoke* invoke);
1499574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray  void HandleBitwiseOperation(HBinaryOperation* operation);
1509aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle  void HandleShift(HBinaryOperation* operation);
15152c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
15252c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldGet(HInstruction* instruction);
153e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray
1549cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
1552d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
1569cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1579cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
1589cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
1599cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1609cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass InstructionCodeGeneratorX86_64 : public HGraphVisitor {
1619cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1629cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
1639cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
164360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray#define DECLARE_VISIT_INSTRUCTION(name, super)     \
165de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Visit##name(H##name* instr) OVERRIDE;
1669cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
167ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
168ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
1699cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1709cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#undef DECLARE_VISIT_INSTRUCTION
1719cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
172ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  void VisitInstruction(HInstruction* instruction) OVERRIDE {
173ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
174ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames               << " (id " << instruction->GetId() << ")";
175ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  }
176ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames
1779cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* GetAssembler() const { return assembler_; }
1789cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1799cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
1803c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // Generate code for the given suspend check. If not null, `successor`
1813c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // is the block to branch to if the suspend check is not needed, and after
1823c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // the suspend call.
1833c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
184424f676379f2f872acd1478672022f19f3240fc1Nicolas Geoffray  void GenerateClassInitializationCheck(SlowPathCodeX86_64* slow_path, CpuRegister class_reg);
1859574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray  void HandleBitwiseOperation(HBinaryOperation* operation);
186c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateRemFP(HRem* rem);
1870f88e87085b7cf6544dadff3f555773966a6853eGuillaume Sanchez  void DivRemOneOrMinusOne(HBinaryOperation* instruction);
188b19930c5cba3cf662dce5ee057fcc9829b4cbb9cGuillaume Sanchez  void DivByPowerOfTwo(HDiv* instruction);
1890f88e87085b7cf6544dadff3f555773966a6853eGuillaume Sanchez  void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
190bacfec30ee9f2f6fdfd190f11b105b609938efcaCalin Juravle  void GenerateDivRemIntegral(HBinaryOperation* instruction);
1919aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle  void HandleShift(HBinaryOperation* operation);
19252c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void GenerateMemoryBarrier(MemBarrierKind kind);
19307276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void HandleFieldSet(HInstruction* instruction,
19407276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                      const FieldInfo& field_info,
19507276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                      bool value_can_be_null);
19652c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
197cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  void GenerateImplicitNullCheck(HNullCheck* instruction);
198cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  void GenerateExplicitNullCheck(HNullCheck* instruction);
19924f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell  void PushOntoFPStack(Location source, uint32_t temp_offset,
20024f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell                       uint32_t stack_adjustment, bool is_float);
201d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang  void GenerateTestAndBranch(HInstruction* instruction,
202d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang                             Label* true_target,
203d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang                             Label* false_target,
204d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang                             Label* always_true_target);
205c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateCompareTestAndBranch(HIf* if_inst,
206c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    HCondition* condition,
207c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    Label* true_target,
208c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    Label* false_target,
209c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell                                    Label* always_true_target);
210c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
211fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil  void HandleGoto(HInstruction* got, HBasicBlock* successor);
2123c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray
2139cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* const assembler_;
2149cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
2159cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2169cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
2179cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
2189cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2199cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass CodeGeneratorX86_64 : public CodeGenerator {
2209cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
221fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  CodeGeneratorX86_64(HGraph* graph,
222fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell                  const X86_64InstructionSetFeatures& isa_features,
223ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  const CompilerOptions& compiler_options,
224ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  OptimizingCompilerStats* stats = nullptr);
2259cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  virtual ~CodeGeneratorX86_64() {}
2269cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
227de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameEntry() OVERRIDE;
228de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameExit() OVERRIDE;
229de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Bind(HBasicBlock* block) OVERRIDE;
230de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
231de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
232de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
233de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
234de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
2359cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2368158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  // Generate code to invoke a runtime entry point.
2378158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  void InvokeRuntime(Address entry_point,
2388158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     HInstruction* instruction,
2398158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     uint32_t dex_pc,
2408158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     SlowPathCode* slow_path);
2418158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames
242de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t GetWordSize() const OVERRIDE {
2439cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return kX86_64WordSize;
2449cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2459cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
246f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
247f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell    return kX86_64WordSize;
248f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  }
249f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell
250de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetLocationBuilder() OVERRIDE {
2519cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &location_builder_;
2529cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2539cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
254de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
2559cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &instruction_visitor_;
2569cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2579cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
258de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  X86_64Assembler* GetAssembler() OVERRIDE {
2599cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &assembler_;
2609cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
2619cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
262eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  const X86_64Assembler& GetAssembler() const OVERRIDE {
263eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    return assembler_;
264eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
265eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
266f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
267ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray    return &move_resolver_;
268ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  }
269ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
270de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
271de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray    return GetLabelOf(block)->Position();
272de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  }
273de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
274de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
2759cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
276988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray  void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
277de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
278de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
279de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
280f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  void Finalize(CodeAllocator* allocator) OVERRIDE;
2819cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
282de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  InstructionSet GetInstructionSet() const OVERRIDE {
283412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    return InstructionSet::kX86_64;
284412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
285412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
2863c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  // Emit a write barrier.
28707276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void MarkGCCard(CpuRegister temp,
28807276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister card,
28907276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister object,
29007276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister value,
29107276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  bool value_can_be_null);
2923c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
2939cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  // Helper method to move a value between two locations.
2949cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  void Move(Location destination, Location source);
2959cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
29692a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  Label* GetLabelOf(HBasicBlock* block) const {
297dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray    return CommonGetLabelOf<Label>(block_labels_.GetRawStorage(), block);
29892a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
29992a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
300de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Initialize() OVERRIDE {
30192a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray    block_labels_.SetSize(GetGraph()->GetBlocks().Size());
30292a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
30392a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
304840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
305840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
306840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
307840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
30838207af82afb6f99c687f64b15601ed20d82220aNicolas Geoffray  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
30971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
310581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
311581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
312fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
313fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell    return isa_features_;
314fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  }
315fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell
316f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int ConstantAreaStart() const {
317f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell    return constant_area_start_;
318f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  }
319f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
320f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralDoubleAddress(double v);
321f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralFloatAddress(float v);
322f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt32Address(int32_t v);
323f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt64Address(int64_t v);
324f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
32592e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  // Load a 64 bit value into a register in the most efficient manner.
32692e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  void Load64BitValue(CpuRegister dest, int64_t value);
32792e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell
328cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
329cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  void Store64BitValueToStack(Location dest, int64_t value);
330cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell
3313c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray private:
332581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  struct PcRelativeDexCacheAccessInfo {
333581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
334581550137ee3a068a14224870e71aeee924a0646Vladimir Marko        : target_dex_file(dex_file), element_offset(element_off), label() { }
335581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
336581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    const DexFile& target_dex_file;
337581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    uint32_t element_offset;
338581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    Label label;
339581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  };
340581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
34192a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  // Labels for each block that will be compiled.
34292a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  GrowableArray<Label> block_labels_;
3431cf95287364948689f6a1a320567acd7728e94a3Nicolas Geoffray  Label frame_entry_label_;
3449cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64 location_builder_;
3459cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64 instruction_visitor_;
346ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64 move_resolver_;
3479cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler assembler_;
348fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& isa_features_;
3499cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
35039dcf55a56da746e04f477f89e7b00ba1de03880Mark Mendell  // Offset to the start of the constant area in the assembled code.
351f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  // Used for fixups to the constant area.
352f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int constant_area_start_;
353f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
354581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
355581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> method_patches_;
356581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
357581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // PC-relative DexCache access info.
358581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<PcRelativeDexCacheAccessInfo> pc_rel_dex_cache_patches_;
359581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
360581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // When we don't know the proper offset for the value, we use kDummy32BitOffset.
361581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // We will fix this up in the linker later to have the right value.
362581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  static constexpr int32_t kDummy32BitOffset = 256;
363581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
3649cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
3659cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
3669cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3679cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace x86_64
3689cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace art
3699cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3709cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
371