code_generator_x86_64.h revision 58282f4510961317b8d5a364a6f740a78926716f
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
2017077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell#include "arch/x86_64/instruction_set_features_x86_64.h"
219cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "code_generator.h"
2252c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle#include "dex/compiler_enums.h"
23cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle#include "driver/compiler_options.h"
249cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "nodes.h"
25ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray#include "parallel_move_resolver.h"
269cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "utils/x86_64/assembler_x86_64.h"
279cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
289cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraynamespace art {
299cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraynamespace x86_64 {
309cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3186a8d7afc7f00ff0f5ea7b8aaf4d50514250a4e6Nicolas Geoffray// Use a local definition to prevent copying mistakes.
3286a8d7afc7f00ff0f5ea7b8aaf4d50514250a4e6Nicolas Geoffraystatic constexpr size_t kX86_64WordSize = kX86_64PointerSize;
339cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
34ee3cf0731d0ef0787bc2947c8e3ca432b513956bNicolas Geoffray// Some x86_64 instructions require a register to be available as temp.
35ee3cf0731d0ef0787bc2947c8e3ca432b513956bNicolas Geoffraystatic constexpr Register TMP = R11;
36ee3cf0731d0ef0787bc2947c8e3ca432b513956bNicolas Geoffray
379cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraystatic constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 };
387fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffraystatic constexpr FloatRegister kParameterFloatRegisters[] =
397fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray    { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
409cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
419cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffraystatic constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
427fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffraystatic constexpr size_t kParameterFloatRegistersLength = arraysize(kParameterFloatRegisters);
439cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
44848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Haostatic constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX, RCX };
45d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffraystatic constexpr size_t kRuntimeParameterCoreRegistersLength =
46d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray    arraysize(kRuntimeParameterCoreRegisters);
47d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffraystatic constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
48d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffraystatic constexpr size_t kRuntimeParameterFpuRegistersLength =
49d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray    arraysize(kRuntimeParameterFpuRegisters);
50d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray
51a4f1220c1518074db18ca1044e9201492975750bMark Mendell// These XMM registers are non-volatile in ART ABI, but volatile in native ABI.
52a4f1220c1518074db18ca1044e9201492975750bMark Mendell// If the ART ABI changes, this list must be updated.  It is used to ensure that
53a4f1220c1518074db18ca1044e9201492975750bMark Mendell// these are not clobbered by any direct call to native code (such as math intrinsics).
54a4f1220c1518074db18ca1044e9201492975750bMark Mendellstatic constexpr FloatRegister non_volatile_xmm_regs[] = { XMM12, XMM13, XMM14, XMM15 };
55a4f1220c1518074db18ca1044e9201492975750bMark Mendell
56a4f1220c1518074db18ca1044e9201492975750bMark Mendell
57d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffrayclass InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
58d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray public:
59d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray  InvokeRuntimeCallingConvention()
60d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray      : CallingConvention(kRuntimeParameterCoreRegisters,
61d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray                          kRuntimeParameterCoreRegistersLength,
62d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray                          kRuntimeParameterFpuRegisters,
63e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                          kRuntimeParameterFpuRegistersLength,
64e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                          kX86_64PointerSize) {}
65d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray
66d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray private:
67d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
68d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray};
69d75948ac93a4a317feaf136cae78823071234ba5Nicolas Geoffray
707fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffrayclass InvokeDexCallingConvention : public CallingConvention<Register, FloatRegister> {
719cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
727fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray  InvokeDexCallingConvention() : CallingConvention(
737fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray      kParameterCoreRegisters,
747fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray      kParameterCoreRegistersLength,
757fb49da8ec62e8a10ed9419ade9f32c6b1174687Nicolas Geoffray      kParameterFloatRegisters,
76e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kParameterFloatRegistersLength,
77e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kX86_64PointerSize) {}
789cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
799cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
809cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
819cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
829cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
83e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravleclass FieldAccessCallingConventionX86_64 : public FieldAccessCallingConvention {
84e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle public:
85e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  FieldAccessCallingConventionX86_64() {}
86e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
87e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  Location GetObjectLocation() const OVERRIDE {
88e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    return Location::RegisterLocation(RSI);
89e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
90e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  Location GetFieldIndexLocation() const OVERRIDE {
91e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    return Location::RegisterLocation(RDI);
92e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
93e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
94e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    return Location::RegisterLocation(RAX);
95e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
96e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
97e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    return Primitive::Is64BitType(type)
98e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle        ? Location::RegisterLocation(RDX)
99e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle        : (is_instance
100e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle            ? Location::RegisterLocation(RDX)
101e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle            : Location::RegisterLocation(RSI));
102e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
103e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
104e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    return Location::FpuRegisterLocation(XMM0);
105e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
106e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
107e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle private:
108e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionX86_64);
109e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle};
110e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
111e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
1122d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillainclass InvokeDexCallingConventionVisitorX86_64 : public InvokeDexCallingConventionVisitor {
1139cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1142d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  InvokeDexCallingConventionVisitorX86_64() {}
1152d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  virtual ~InvokeDexCallingConventionVisitorX86_64() {}
1169cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1172d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  Location GetNextLocation(Primitive::Type type) OVERRIDE;
118fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
119fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  Location GetMethodLocation() const OVERRIDE;
1209cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1219cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
1229cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InvokeDexCallingConvention calling_convention;
1232d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain
1242d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorX86_64);
1259cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
1269cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1279cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass CodeGeneratorX86_64;
12871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
129ad4450e5c3ffaa9566216cc6fafbf5c11186c467Zheng Xuclass ParallelMoveResolverX86_64 : public ParallelMoveResolverWithSwap {
130ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray public:
131ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen)
132ad4450e5c3ffaa9566216cc6fafbf5c11186c467Zheng Xu      : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
133ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
134de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void EmitMove(size_t index) OVERRIDE;
135de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void EmitSwap(size_t index) OVERRIDE;
136de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void SpillScratch(int reg) OVERRIDE;
137de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void RestoreScratch(int reg) OVERRIDE;
138ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
139ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  X86_64Assembler* GetAssembler() const;
140ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
141ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray private:
142412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange32(CpuRegister reg, int mem);
143102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  void Exchange32(XmmRegister reg, int mem);
144412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange32(int mem1, int mem2);
145412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange64(CpuRegister reg, int mem);
146102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  void Exchange64(XmmRegister reg, int mem);
147412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Exchange64(int mem1, int mem2);
148ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
149ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
150ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
151ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
152ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray};
153ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
1549cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass LocationsBuilderX86_64 : public HGraphVisitor {
1559cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1569cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
1579cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray      : HGraphVisitor(graph), codegen_(codegen) {}
1589cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
159360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray#define DECLARE_VISIT_INSTRUCTION(name, super)     \
160de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Visit##name(H##name* instr) OVERRIDE;
1619cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
162ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
163ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
1649cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1659cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#undef DECLARE_VISIT_INSTRUCTION
1669cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
167ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  void VisitInstruction(HInstruction* instruction) OVERRIDE {
168ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
169ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames               << " (id " << instruction->GetId() << ")";
170ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  }
171ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames
1729574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray private:
173e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray  void HandleInvoke(HInvoke* invoke);
1749574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray  void HandleBitwiseOperation(HBinaryOperation* operation);
1755f7b58ea1adfc0639dd605b65f59198d3763f801Vladimir Marko  void HandleCondition(HCondition* condition);
1769aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle  void HandleShift(HBinaryOperation* operation);
17752c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
17852c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldGet(HInstruction* instruction);
179e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray
1809cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
1812d27c8e338af7262dbd4aaa66127bb8fa1758b86Roland Levillain  InvokeDexCallingConventionVisitorX86_64 parameter_visitor_;
1829cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1839cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
1849cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
1859cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
18642249c3602c3d0243396ee3627ffb5906aa77c1eAart Bikclass InstructionCodeGeneratorX86_64 : public InstructionCodeGenerator {
1879cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
1889cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
1899cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
190360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray#define DECLARE_VISIT_INSTRUCTION(name, super)     \
191de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Visit##name(H##name* instr) OVERRIDE;
1929cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
193ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
194ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  FOR_EACH_CONCRETE_INSTRUCTION_X86_64(DECLARE_VISIT_INSTRUCTION)
1959cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
1969cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#undef DECLARE_VISIT_INSTRUCTION
1979cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
198ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  void VisitInstruction(HInstruction* instruction) OVERRIDE {
199ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames    LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
200ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames               << " (id " << instruction->GetId() << ")";
201ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames  }
202ef20f71e16f035a39a329c8524d7e59ca6a11f04Alexandre Rames
2039cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* GetAssembler() const { return assembler_; }
2049cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2059cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray private:
2063c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // Generate code for the given suspend check. If not null, `successor`
2073c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // is the block to branch to if the suspend check is not needed, and after
2083c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  // the suspend call.
2093c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
21085b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateClassInitializationCheck(SlowPathCode* slow_path, CpuRegister class_reg);
2119574c4b5f5ef039d694ac12c97e25ca02eca83c0Nicolas Geoffray  void HandleBitwiseOperation(HBinaryOperation* operation);
212c470193cfc522fc818eb2eaab896aef9caf0c75aMark Mendell  void GenerateRemFP(HRem* rem);
2130f88e87085b7cf6544dadff3f555773966a6853eGuillaume Sanchez  void DivRemOneOrMinusOne(HBinaryOperation* instruction);
214b19930c5cba3cf662dce5ee057fcc9829b4cbb9cGuillaume Sanchez  void DivByPowerOfTwo(HDiv* instruction);
2150f88e87085b7cf6544dadff3f555773966a6853eGuillaume Sanchez  void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
216bacfec30ee9f2f6fdfd190f11b105b609938efcaCalin Juravle  void GenerateDivRemIntegral(HBinaryOperation* instruction);
2175f7b58ea1adfc0639dd605b65f59198d3763f801Vladimir Marko  void HandleCondition(HCondition* condition);
2189aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle  void HandleShift(HBinaryOperation* operation);
2191e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
22007276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void HandleFieldSet(HInstruction* instruction,
22107276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                      const FieldInfo& field_info,
22207276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                      bool value_can_be_null);
22352c489645b6e9ae33623f1ec24143cde5444906eCalin Juravle  void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
2241e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
2251e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a heap reference load using one register `out`:
2261e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2271e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //   out <- *(out + offset)
2281e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2291e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // while honoring heap poisoning and/or read barriers (if any).
2301e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Register `temp` is used when generating a read barrier.
2311e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReferenceLoadOneRegister(HInstruction* instruction,
2321e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                        Location out,
2331e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                        uint32_t offset,
2341e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                        Location temp);
2351e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a heap reference load using two different registers
2361e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // `out` and `obj`:
2371e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2381e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //   out <- *(obj + offset)
2391e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2401e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // while honoring heap poisoning and/or read barriers (if any).
2411e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Register `temp` is used when generating a Baker's read barrier.
2421e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
2431e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         Location out,
2441e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         Location obj,
2451e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         uint32_t offset,
2461e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         Location temp);
2471e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a GC root reference load:
2481e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2491e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //   root <- *(obj + offset)
2501e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2511e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // while honoring read barriers (if any).
2521e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateGcRootFieldLoad(HInstruction* instruction,
2531e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location root,
2541e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               CpuRegister obj,
2551e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               uint32_t offset);
2561e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
257cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  void GenerateImplicitNullCheck(HNullCheck* instruction);
258cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  void GenerateExplicitNullCheck(HNullCheck* instruction);
25924f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell  void PushOntoFPStack(Location source, uint32_t temp_offset,
26024f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell                       uint32_t stack_adjustment, bool is_float);
261152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  template<class LabelType>
262d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang  void GenerateTestAndBranch(HInstruction* instruction,
2630debae7bc89eb05f7a2bf7dccd223318fad7c88dDavid Brazdil                             size_t condition_input_index,
264152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                             LabelType* true_target,
265152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                             LabelType* false_target);
266152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  template<class LabelType>
2670debae7bc89eb05f7a2bf7dccd223318fad7c88dDavid Brazdil  void GenerateCompareTestAndBranch(HCondition* condition,
268152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                                    LabelType* true_target,
269152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                                    LabelType* false_target);
270152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  template<class LabelType>
271152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
272152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell
273fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil  void HandleGoto(HInstruction* got, HBasicBlock* successor);
2743c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray
2759cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* const assembler_;
2769cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
2779cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2789cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
2799cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
2809cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2819c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell// Class for fixups to jump tables.
2829c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendellclass JumpTableRIPFixup;
2839c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell
2849cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass CodeGeneratorX86_64 : public CodeGenerator {
2859cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
286fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  CodeGeneratorX86_64(HGraph* graph,
287fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell                  const X86_64InstructionSetFeatures& isa_features,
288ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  const CompilerOptions& compiler_options,
289ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  OptimizingCompilerStats* stats = nullptr);
2909cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  virtual ~CodeGeneratorX86_64() {}
2919cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
292de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameEntry() OVERRIDE;
293de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameExit() OVERRIDE;
294de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Bind(HBasicBlock* block) OVERRIDE;
295de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
296175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void MoveConstant(Location destination, int32_t value) OVERRIDE;
297e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
298e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
299e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
300de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
301de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
302de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
303de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
3049cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3058158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  // Generate code to invoke a runtime entry point.
306175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void InvokeRuntime(QuickEntrypointEnum entrypoint,
307175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     HInstruction* instruction,
308175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     uint32_t dex_pc,
309175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     SlowPathCode* slow_path) OVERRIDE;
310175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
311175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void InvokeRuntime(int32_t entry_point_offset,
3128158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     HInstruction* instruction,
3138158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     uint32_t dex_pc,
3148158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     SlowPathCode* slow_path);
3158158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames
316de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t GetWordSize() const OVERRIDE {
3179cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return kX86_64WordSize;
3189cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3199cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
320f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
321f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell    return kX86_64WordSize;
322f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  }
323f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell
324de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetLocationBuilder() OVERRIDE {
3259cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &location_builder_;
3269cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3279cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
328de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
3299cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &instruction_visitor_;
3309cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3319cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
332de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  X86_64Assembler* GetAssembler() OVERRIDE {
3339cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &assembler_;
3349cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3359cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
336eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  const X86_64Assembler& GetAssembler() const OVERRIDE {
337eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    return assembler_;
338eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
339eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
340f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
341ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray    return &move_resolver_;
342ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  }
343ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
344de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
345de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray    return GetLabelOf(block)->Position();
346de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  }
347de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
348de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
3499cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
35058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  void SetupBlockedRegisters() const OVERRIDE;
351de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
352de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
353f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  void Finalize(CodeAllocator* allocator) OVERRIDE;
3549cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
355de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  InstructionSet GetInstructionSet() const OVERRIDE {
356412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    return InstructionSet::kX86_64;
357412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
358412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
3593c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  // Emit a write barrier.
36007276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void MarkGCCard(CpuRegister temp,
36107276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister card,
36207276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister object,
36307276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister value,
36407276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  bool value_can_be_null);
3653c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
3661e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateMemoryBarrier(MemBarrierKind kind);
3671e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
3689cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  // Helper method to move a value between two locations.
3699cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  void Move(Location destination, Location source);
3709cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
37192a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  Label* GetLabelOf(HBasicBlock* block) const {
372225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko    return CommonGetLabelOf<Label>(block_labels_, block);
37392a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
37492a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
375de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Initialize() OVERRIDE {
376225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko    block_labels_ = CommonInitializeLabels<Label>();
37792a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
37892a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
379840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
380840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
381840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
382840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
383dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  // Check if the desired_dispatch_info is supported. If it is, return it,
384dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  // otherwise return a fall-back info that should be used instead.
385dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
386dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
387dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      MethodReference target_method) OVERRIDE;
388dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko
38985b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
39085b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
39185b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe
39285b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
39371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
394581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
395581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
396fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
397fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell    return isa_features_;
398fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  }
399fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell
4001e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Fast path implementation of ReadBarrier::Barrier for a heap
4011e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // reference field load when Baker's read barriers are used.
4021e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4031e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location out,
4041e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             CpuRegister obj,
4051e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             uint32_t offset,
4061e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location temp,
4071e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             bool needs_null_check);
4081e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Fast path implementation of ReadBarrier::Barrier for a heap
4091e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // reference array load when Baker's read barriers are used.
4101e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4111e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location out,
4121e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             CpuRegister obj,
4131e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             uint32_t data_offset,
4141e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location index,
4151e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location temp,
4161e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             bool needs_null_check);
4171e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
4181e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a read barrier for a heap reference within `instruction`
4191e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // using a slow path.
4200d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4210d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // A read barrier for an object reference read from the heap is
4220d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // implemented as a call to the artReadBarrierSlow runtime entry
4230d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // point, which is passed the values in locations `ref`, `obj`, and
4240d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // `offset`:
4250d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4260d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //   mirror::Object* artReadBarrierSlow(mirror::Object* ref,
4270d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //                                      mirror::Object* obj,
4280d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //                                      uint32_t offset);
4290d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4300d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // The `out` location contains the value returned by
4310d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // artReadBarrierSlow.
4320d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4330d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // When `index` provided (i.e., when it is different from
4340d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // Location::NoLocation()), the offset value passed to
4350d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // artReadBarrierSlow is adjusted to take `index` into account.
4361e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReadBarrierSlow(HInstruction* instruction,
4371e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location out,
4381e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location ref,
4391e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location obj,
4401e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               uint32_t offset,
4411e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location index = Location::NoLocation());
4421e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
4431e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // If read barriers are enabled, generate a read barrier for a heap
4441e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // reference using a slow path. If heap poisoning is enabled, also
4451e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // unpoison the reference in `out`.
4461e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4471e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location out,
4481e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location ref,
4491e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location obj,
4501e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    uint32_t offset,
4511e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location index = Location::NoLocation());
4521e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
4531e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a read barrier for a GC root within `instruction` using
4541e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // a slow path.
4550d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4560d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // A read barrier for an object reference GC root is implemented as
4570d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // a call to the artReadBarrierForRootSlow runtime entry point,
4580d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // which is passed the value in location `root`:
4590d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4600d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //   mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
4610d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4620d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // The `out` location contains the value returned by
4630d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // artReadBarrierForRootSlow.
4641e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
4650d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
466f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int ConstantAreaStart() const {
467f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell    return constant_area_start_;
468f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  }
469f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
470f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralDoubleAddress(double v);
471f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralFloatAddress(float v);
472f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt32Address(int32_t v);
473f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt64Address(int64_t v);
474f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
47592e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  // Load a 64 bit value into a register in the most efficient manner.
47692e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  void Load64BitValue(CpuRegister dest, int64_t value);
4779c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell  Address LiteralCaseTable(HPackedSwitch* switch_instr);
47892e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell
479cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
480cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  void Store64BitValueToStack(Location dest, int64_t value);
481cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell
482ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell  // Assign a 64 bit constant to an address.
483ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell  void MoveInt64ToAddress(const Address& addr_low,
484ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell                          const Address& addr_high,
485ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell                          int64_t v,
486ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell                          HInstruction* instruction);
487ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell
48817077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // Ensure that prior stores complete to memory before subsequent loads.
48917077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // The locked add implementation will avoid serializing device memory, but will
49017077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // touch (but not change) the top of the stack. The locked add should not be used for
49117077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // ordering non-temporal stores.
49217077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  void MemoryFence(bool force_mfence = false) {
49317077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell    if (!force_mfence && isa_features_.PrefersLockedAddSynchronization()) {
49417077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell      assembler_.lock()->addl(Address(CpuRegister(RSP), 0), Immediate(0));
49517077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell    } else {
49617077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell      assembler_.mfence();
49717077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell    }
49817077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  }
49917077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell
5003c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray private:
5011e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Factored implementation of GenerateFieldLoadWithBakerReadBarrier
5021e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // and GenerateArrayLoadWithBakerReadBarrier.
5031e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5041e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 Location ref,
5051e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 CpuRegister obj,
5061e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 const Address& src,
5071e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 Location temp,
5081e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 bool needs_null_check);
5091e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
510581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  struct PcRelativeDexCacheAccessInfo {
511581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
512581550137ee3a068a14224870e71aeee924a0646Vladimir Marko        : target_dex_file(dex_file), element_offset(element_off), label() { }
513581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
514581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    const DexFile& target_dex_file;
515581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    uint32_t element_offset;
516581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    Label label;
517581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  };
518581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
51992a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  // Labels for each block that will be compiled.
520225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko  Label* block_labels_;  // Indexed by block id.
5211cf95287364948689f6a1a320567acd7728e94a3Nicolas Geoffray  Label frame_entry_label_;
5229cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64 location_builder_;
5239cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64 instruction_visitor_;
524ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64 move_resolver_;
5259cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler assembler_;
526fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& isa_features_;
5279cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
52839dcf55a56da746e04f477f89e7b00ba1de03880Mark Mendell  // Offset to the start of the constant area in the assembled code.
529f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  // Used for fixups to the constant area.
530f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int constant_area_start_;
531f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
532581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
533581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> method_patches_;
534581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
535581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // PC-relative DexCache access info.
5360f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko  ArenaDeque<PcRelativeDexCacheAccessInfo> pc_relative_dex_cache_patches_;
537581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
538581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // When we don't know the proper offset for the value, we use kDummy32BitOffset.
539581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // We will fix this up in the linker later to have the right value.
540581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  static constexpr int32_t kDummy32BitOffset = 256;
541581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
5429c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell  // Fixups for jump tables need to be handled specially.
5439c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell  ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
5449c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell
5459cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
5469cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
5479cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
5489cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace x86_64
5499cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace art
5509cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
5519cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
552