code_generator_x86_64.h revision 2ae48182573da7087bffc2873730bc758ec29696
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).
23095e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  //
23195e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  // Location `maybe_temp` is used when generating a read barrier and
23295e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  // shall be a register in that case; it may be an invalid location
23395e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  // otherwise.
2341e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReferenceLoadOneRegister(HInstruction* instruction,
2351e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                        Location out,
2361e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                        uint32_t offset,
23795e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain                                        Location maybe_temp);
2381e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a heap reference load using two different registers
2391e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // `out` and `obj`:
2401e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2411e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //   out <- *(obj + offset)
2421e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2431e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // while honoring heap poisoning and/or read barriers (if any).
24495e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  //
24595e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  // Location `maybe_temp` is used when generating a Baker's (fast
24695e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  // path) read barrier and shall be a register in that case; it may
24795e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain  // be an invalid location otherwise.
2481e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
2491e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         Location out,
2501e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         Location obj,
2511e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                         uint32_t offset,
25295e7ffc28ea4d6deba356e636b16120ae49b62e2Roland Levillain                                         Location maybe_temp);
2531e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a GC root reference load:
2541e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2551e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //   root <- *(obj + offset)
2561e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  //
2571e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // while honoring read barriers (if any).
2581e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateGcRootFieldLoad(HInstruction* instruction,
2591e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location root,
2601e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               CpuRegister obj,
2611e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               uint32_t offset);
2621e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
26324f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell  void PushOntoFPStack(Location source, uint32_t temp_offset,
26424f2dfae084b2382c053f5d688fd6bb26cb8a328Mark Mendell                       uint32_t stack_adjustment, bool is_float);
2657c0b44f180f1b8cf82c568091d250071d1130954Mark Mendell  void GenerateCompareTest(HCondition* condition);
266152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  template<class LabelType>
267d43b3ac88cd46b8815890188c9c2b9a3f1564648Mingyao Yang  void GenerateTestAndBranch(HInstruction* instruction,
2680debae7bc89eb05f7a2bf7dccd223318fad7c88dDavid Brazdil                             size_t condition_input_index,
269152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                             LabelType* true_target,
270152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                             LabelType* false_target);
271152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  template<class LabelType>
2720debae7bc89eb05f7a2bf7dccd223318fad7c88dDavid Brazdil  void GenerateCompareTestAndBranch(HCondition* condition,
273152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                                    LabelType* true_target,
274152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell                                    LabelType* false_target);
275152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  template<class LabelType>
276152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell  void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
277152408f8c2188a7ed950cad04883b2f67dc74e84Mark Mendell
278fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil  void HandleGoto(HInstruction* got, HBasicBlock* successor);
2793c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray
2809cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler* const assembler_;
2819cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  CodeGeneratorX86_64* const codegen_;
2829cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2839cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
2849cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
2859cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
2869c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell// Class for fixups to jump tables.
2879c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendellclass JumpTableRIPFixup;
2889c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell
2899cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffrayclass CodeGeneratorX86_64 : public CodeGenerator {
2909cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray public:
291fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  CodeGeneratorX86_64(HGraph* graph,
292fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell                  const X86_64InstructionSetFeatures& isa_features,
293ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  const CompilerOptions& compiler_options,
294ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu                  OptimizingCompilerStats* stats = nullptr);
2959cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  virtual ~CodeGeneratorX86_64() {}
2969cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
297de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameEntry() OVERRIDE;
298de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void GenerateFrameExit() OVERRIDE;
299de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Bind(HBasicBlock* block) OVERRIDE;
300175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void MoveConstant(Location destination, int32_t value) OVERRIDE;
301e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
302e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
303e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
304de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
305de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
306de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
307de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
3089cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
3098158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  // Generate code to invoke a runtime entry point.
310175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void InvokeRuntime(QuickEntrypointEnum entrypoint,
311175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     HInstruction* instruction,
312175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     uint32_t dex_pc,
313175dc732c80e6f2afd83209348124df349290ba8Calin Juravle                     SlowPathCode* slow_path) OVERRIDE;
314175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
315175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  void InvokeRuntime(int32_t entry_point_offset,
3168158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     HInstruction* instruction,
3178158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     uint32_t dex_pc,
3188158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames                     SlowPathCode* slow_path);
3198158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames
320de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  size_t GetWordSize() const OVERRIDE {
3219cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return kX86_64WordSize;
3229cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3239cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
324f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
325f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell    return kX86_64WordSize;
326f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell  }
327f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell
328de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetLocationBuilder() OVERRIDE {
3299cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &location_builder_;
3309cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3319cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
332de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  HGraphVisitor* GetInstructionVisitor() OVERRIDE {
3339cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &instruction_visitor_;
3349cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3359cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
336de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  X86_64Assembler* GetAssembler() OVERRIDE {
3379cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray    return &assembler_;
3389cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  }
3399cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
340eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  const X86_64Assembler& GetAssembler() const OVERRIDE {
341eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    return assembler_;
342eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
343eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
344f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  ParallelMoveResolverX86_64* GetMoveResolver() OVERRIDE {
345ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray    return &move_resolver_;
346ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  }
347ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray
348de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
349de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray    return GetLabelOf(block)->Position();
350de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  }
351de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
352de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
3539cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
35458282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  void SetupBlockedRegisters() const OVERRIDE;
355de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
356de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
357f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  void Finalize(CodeAllocator* allocator) OVERRIDE;
3589cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
359de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  InstructionSet GetInstructionSet() const OVERRIDE {
360412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    return InstructionSet::kX86_64;
361412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
362412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
3633c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  // Emit a write barrier.
36407276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray  void MarkGCCard(CpuRegister temp,
36507276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister card,
36607276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister object,
36707276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  CpuRegister value,
36807276db28d654594e0e86e9e467cad393f752e6eNicolas Geoffray                  bool value_can_be_null);
3693c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
3701e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateMemoryBarrier(MemBarrierKind kind);
3711e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
3729cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  // Helper method to move a value between two locations.
3739cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  void Move(Location destination, Location source);
3749cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
37592a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  Label* GetLabelOf(HBasicBlock* block) const {
376225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko    return CommonGetLabelOf<Label>(block_labels_, block);
37792a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
37892a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
379de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  void Initialize() OVERRIDE {
380225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko    block_labels_ = CommonInitializeLabels<Label>();
38192a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  }
38292a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray
383840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
384840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
385840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
386840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
387dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  // Check if the desired_dispatch_info is supported. If it is, return it,
388dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  // otherwise return a fall-back info that should be used instead.
389dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
390dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
391dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      MethodReference target_method) OVERRIDE;
392dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko
39385b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
39485b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
39585b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe
39685b62f23fc6dfffe2ddd3ddfa74611666c9ff41dAndreas Gampe  void MoveFromReturnRegister(Location trg, Primitive::Type type) OVERRIDE;
39771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
398581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
399581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
400fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& GetInstructionSetFeatures() const {
401fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell    return isa_features_;
402fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  }
403fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell
4041e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Fast path implementation of ReadBarrier::Barrier for a heap
4051e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // reference field load when Baker's read barriers are used.
4061e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
407e3f43ac79e50a4693ea4d46acf5cffca64910ceeRoland Levillain                                             Location ref,
4081e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             CpuRegister obj,
4091e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             uint32_t offset,
4101e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location temp,
4111e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             bool needs_null_check);
4121e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Fast path implementation of ReadBarrier::Barrier for a heap
4131e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // reference array load when Baker's read barriers are used.
4141e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
415e3f43ac79e50a4693ea4d46acf5cffca64910ceeRoland Levillain                                             Location ref,
4161e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             CpuRegister obj,
4171e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             uint32_t data_offset,
4181e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location index,
4191e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             Location temp,
4201e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                             bool needs_null_check);
4211e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
4221e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a read barrier for a heap reference within `instruction`
4231e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // using a slow path.
4240d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4250d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // A read barrier for an object reference read from the heap is
4260d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // implemented as a call to the artReadBarrierSlow runtime entry
4270d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // point, which is passed the values in locations `ref`, `obj`, and
4280d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // `offset`:
4290d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4300d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //   mirror::Object* artReadBarrierSlow(mirror::Object* ref,
4310d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //                                      mirror::Object* obj,
4320d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //                                      uint32_t offset);
4330d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4340d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // The `out` location contains the value returned by
4350d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // artReadBarrierSlow.
4360d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4370d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // When `index` provided (i.e., when it is different from
4380d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // Location::NoLocation()), the offset value passed to
4390d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // artReadBarrierSlow is adjusted to take `index` into account.
4401e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReadBarrierSlow(HInstruction* instruction,
4411e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location out,
4421e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location ref,
4431e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location obj,
4441e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               uint32_t offset,
4451e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                               Location index = Location::NoLocation());
4461e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
4471e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // If read barriers are enabled, generate a read barrier for a heap
4481e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // reference using a slow path. If heap poisoning is enabled, also
4491e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // unpoison the reference in `out`.
4501e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4511e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location out,
4521e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location ref,
4531e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location obj,
4541e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    uint32_t offset,
4551e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                    Location index = Location::NoLocation());
4561e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
4571e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Generate a read barrier for a GC root within `instruction` using
4581e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // a slow path.
4590d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4600d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // A read barrier for an object reference GC root is implemented as
4610d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // a call to the artReadBarrierForRootSlow runtime entry point,
4620d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // which is passed the value in location `root`:
4630d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4640d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //   mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
4650d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  //
4660d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // The `out` location contains the value returned by
4670d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // artReadBarrierForRootSlow.
4681e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
4690d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
470f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int ConstantAreaStart() const {
471f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell    return constant_area_start_;
472f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  }
473f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
474f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralDoubleAddress(double v);
475f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralFloatAddress(float v);
476f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt32Address(int32_t v);
477f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  Address LiteralInt64Address(int64_t v);
478f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
479a19616e3363276e7f2c471eb2839fb16f1d43f27Aart Bik  // Load a 32/64-bit value into a register in the most efficient manner.
480c5d4754198aadb2ada2d3f5daacd10d79bc13f38Aart Bik  void Load32BitValue(CpuRegister dest, int32_t value);
48192e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell  void Load64BitValue(CpuRegister dest, int64_t value);
4827c0b44f180f1b8cf82c568091d250071d1130954Mark Mendell  void Load32BitValue(XmmRegister dest, int32_t value);
4837c0b44f180f1b8cf82c568091d250071d1130954Mark Mendell  void Load64BitValue(XmmRegister dest, int64_t value);
4847c0b44f180f1b8cf82c568091d250071d1130954Mark Mendell  void Load32BitValue(XmmRegister dest, float value);
4857c0b44f180f1b8cf82c568091d250071d1130954Mark Mendell  void Load64BitValue(XmmRegister dest, double value);
486c5d4754198aadb2ada2d3f5daacd10d79bc13f38Aart Bik
487a19616e3363276e7f2c471eb2839fb16f1d43f27Aart Bik  // Compare a register with a 32/64-bit value in the most efficient manner.
488a19616e3363276e7f2c471eb2839fb16f1d43f27Aart Bik  void Compare32BitValue(CpuRegister dest, int32_t value);
489a19616e3363276e7f2c471eb2839fb16f1d43f27Aart Bik  void Compare64BitValue(CpuRegister dest, int64_t value);
490a19616e3363276e7f2c471eb2839fb16f1d43f27Aart Bik
4919c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell  Address LiteralCaseTable(HPackedSwitch* switch_instr);
49292e83bf8c0b2df8c977ffbc527989631d94b1819Mark Mendell
493cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  // Store a 64 bit value into a DoubleStackSlot in the most efficient manner.
494cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell  void Store64BitValueToStack(Location dest, int64_t value);
495cfa410b0ea561318f74a76c5323f0f6cd8eaaa50Mark Mendell
496ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell  // Assign a 64 bit constant to an address.
497ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell  void MoveInt64ToAddress(const Address& addr_low,
498ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell                          const Address& addr_high,
499ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell                          int64_t v,
500ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell                          HInstruction* instruction);
501ea5af68d6dda832bdfb5978a0c5d6f86a3f67e80Mark Mendell
50217077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // Ensure that prior stores complete to memory before subsequent loads.
50317077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // The locked add implementation will avoid serializing device memory, but will
50417077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // touch (but not change) the top of the stack. The locked add should not be used for
50517077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  // ordering non-temporal stores.
50617077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  void MemoryFence(bool force_mfence = false) {
50717077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell    if (!force_mfence && isa_features_.PrefersLockedAddSynchronization()) {
50817077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell      assembler_.lock()->addl(Address(CpuRegister(RSP), 0), Immediate(0));
50917077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell    } else {
51017077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell      assembler_.mfence();
51117077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell    }
51217077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell  }
51317077d888a6752a2e5f8161eee1b2c3285783d12Mark P Mendell
514c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky  void GenerateNop();
5152ae48182573da7087bffc2873730bc758ec29696Calin Juravle  void GenerateImplicitNullCheck(HNullCheck* instruction);
5162ae48182573da7087bffc2873730bc758ec29696Calin Juravle  void GenerateExplicitNullCheck(HNullCheck* instruction);
517c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky
5183c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray private:
5191e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // Factored implementation of GenerateFieldLoadWithBakerReadBarrier
5201e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  // and GenerateArrayLoadWithBakerReadBarrier.
5211e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain  void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5221e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 Location ref,
5231e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 CpuRegister obj,
5241e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 const Address& src,
5251e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 Location temp,
5261e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain                                                 bool needs_null_check);
5271e7f8db01a929ac816ca122868edc067c3c6cd17Roland Levillain
528581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  struct PcRelativeDexCacheAccessInfo {
529581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    PcRelativeDexCacheAccessInfo(const DexFile& dex_file, uint32_t element_off)
530581550137ee3a068a14224870e71aeee924a0646Vladimir Marko        : target_dex_file(dex_file), element_offset(element_off), label() { }
531581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
532581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    const DexFile& target_dex_file;
533581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    uint32_t element_offset;
534581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    Label label;
535581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  };
536581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
53792a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray  // Labels for each block that will be compiled.
538225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko  Label* block_labels_;  // Indexed by block id.
5391cf95287364948689f6a1a320567acd7728e94a3Nicolas Geoffray  Label frame_entry_label_;
5409cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  LocationsBuilderX86_64 location_builder_;
5419cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  InstructionCodeGeneratorX86_64 instruction_visitor_;
542ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray  ParallelMoveResolverX86_64 move_resolver_;
5439cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  X86_64Assembler assembler_;
544fb8d279bc011b31d0765dc7ca59afea324fd0d0cMark Mendell  const X86_64InstructionSetFeatures& isa_features_;
5459cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
54639dcf55a56da746e04f477f89e7b00ba1de03880Mark Mendell  // Offset to the start of the constant area in the assembled code.
547f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  // Used for fixups to the constant area.
548f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell  int constant_area_start_;
549f55c3e0825cdfc4c5a27730031177d1a0198ec5aMark Mendell
550581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Method patch info. Using ArenaDeque<> which retains element addresses on push/emplace_back().
551581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> method_patches_;
552581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaDeque<MethodPatchInfo<Label>> relative_call_patches_;
553581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // PC-relative DexCache access info.
5540f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko  ArenaDeque<PcRelativeDexCacheAccessInfo> pc_relative_dex_cache_patches_;
555581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
556581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // When we don't know the proper offset for the value, we use kDummy32BitOffset.
557581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // We will fix this up in the linker later to have the right value.
558581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  static constexpr int32_t kDummy32BitOffset = 256;
559581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
5609c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell  // Fixups for jump tables need to be handled specially.
5619c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell  ArenaVector<JumpTableRIPFixup*> fixups_to_jump_tables_;
5629c86b485bc6169eadf846dd5f7cdf0958fe1eb23Mark Mendell
5639cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
5649cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray};
5659cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
5669cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace x86_64
5679cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray}  // namespace art
5689cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray
5699cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
570