blob: e717638fba8b278df7d80b97ec2edc4d94426f45 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_QUICK_X86_CODEGEN_X86_H_
18#define ART_COMPILER_DEX_QUICK_X86_CODEGEN_X86_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include "dex/compiler_internals.h"
21#include "x86_lir.h"
22
23namespace art {
24
Mark Mendelle87f9b52014-04-30 14:13:18 -040025class X86Mir2Lir : public Mir2Lir {
Brian Carlstrom7940e442013-07-12 13:46:57 -070026 public:
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070027 X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena, bool gen64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
29 // Required for target - codegen helpers.
buzbee11b63d12013-08-27 07:34:17 -070030 bool SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, RegLocation rl_src,
buzbee2700f7e2014-03-07 09:46:20 -080031 RegLocation rl_dest, int lit);
Ian Rogerse2143c02014-03-28 08:47:16 -070032 bool EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) OVERRIDE;
Dave Allisonb373e092014-02-20 16:06:36 -080033 LIR* CheckSuspendUsingLoad() OVERRIDE;
Andreas Gampe2f244e92014-05-08 03:35:25 -070034 RegStorage LoadHelper(ThreadOffset<4> offset) OVERRIDE;
35 RegStorage LoadHelper(ThreadOffset<8> offset) OVERRIDE;
Vladimir Marko674744e2014-04-24 15:18:26 +010036 LIR* LoadBaseDispVolatile(RegStorage r_base, int displacement, RegStorage r_dest,
37 OpSize size) OVERRIDE;
Vladimir Marko3bf7c602014-05-07 14:55:43 +010038 LIR* LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
39 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080040 LIR* LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010041 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080042 LIR* LoadBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale, int displacement,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010043 RegStorage r_dest, OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080044 LIR* LoadConstantNoClobber(RegStorage r_dest, int value);
45 LIR* LoadConstantWide(RegStorage r_dest, int64_t value);
Vladimir Marko674744e2014-04-24 15:18:26 +010046 LIR* StoreBaseDispVolatile(RegStorage r_base, int displacement, RegStorage r_src,
47 OpSize size) OVERRIDE;
Vladimir Marko3bf7c602014-05-07 14:55:43 +010048 LIR* StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
49 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080050 LIR* StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010051 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080052 LIR* StoreBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale, int displacement,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010053 RegStorage r_src, OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080054 void MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070055
56 // Required for target - register utilities.
buzbee2700f7e2014-03-07 09:46:20 -080057 RegStorage AllocTypedTemp(bool fp_hint, int reg_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +000058 RegStorage AllocTypedTempWide(bool fp_hint, int reg_class);
buzbee2700f7e2014-03-07 09:46:20 -080059 RegStorage TargetReg(SpecialTargetRegister reg);
60 RegStorage GetArgMappingToPhysicalReg(int arg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 RegLocation GetReturnAlt();
62 RegLocation GetReturnWideAlt();
63 RegLocation LocCReturn();
64 RegLocation LocCReturnDouble();
65 RegLocation LocCReturnFloat();
66 RegLocation LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -070067 uint64_t GetRegMaskCommon(RegStorage reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 void AdjustSpillMask();
Vladimir Marko31c2aac2013-12-09 16:31:19 +000069 void ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 void FreeCallTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 void LockCallTemps();
buzbee091cc402014-03-31 10:14:40 -070072 void MarkPreservedSingle(int v_reg, RegStorage reg);
73 void MarkPreservedDouble(int v_reg, RegStorage reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 void CompilerInitializeRegAlloc();
75
76 // Required for target - miscellaneous.
buzbeeb48819d2013-09-14 16:15:25 -070077 void AssembleLIR();
78 int AssignInsnOffsets();
79 void AssignOffsets();
buzbee0d829482013-10-11 15:24:55 -070080 AssemblerStatus AssembleInstructions(CodeOffset start_addr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 void DumpResourceMask(LIR* lir, uint64_t mask, const char* prefix);
buzbeeb48819d2013-09-14 16:15:25 -070082 void SetupTargetResourceMasks(LIR* lir, uint64_t flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 const char* GetTargetInstFmt(int opcode);
84 const char* GetTargetInstName(int opcode);
85 std::string BuildInsnString(const char* fmt, LIR* lir, unsigned char* base_addr);
86 uint64_t GetPCUseDefEncoding();
87 uint64_t GetTargetInstFlags(int opcode);
88 int GetInsnSize(LIR* lir);
89 bool IsUnconditionalBranch(LIR* lir);
90
Vladimir Marko674744e2014-04-24 15:18:26 +010091 // Check support for volatile load/store of a given size.
92 bool SupportsVolatileLoadStore(OpSize size) OVERRIDE;
93 // Get the register class for load/store of a field.
94 RegisterClass RegClassForFieldLoadStore(OpSize size, bool is_volatile) OVERRIDE;
95
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 // Required for target - Dalvik-level generators.
buzbee2700f7e2014-03-07 09:46:20 -080097 void GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
98 RegLocation rl_src2);
99 void GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, RegLocation rl_index,
100 RegLocation rl_dest, int scale);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 void GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700102 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 void GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Ian Rogersa9a82542013-10-04 11:17:26 -0700104 RegLocation rl_src1, RegLocation rl_shift);
buzbee2700f7e2014-03-07 09:46:20 -0800105 void GenMulLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
106 RegLocation rl_src2);
107 void GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
108 RegLocation rl_src2);
109 void GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
110 RegLocation rl_src2);
111 void GenArithOpDouble(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 RegLocation rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800113 void GenArithOpFloat(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
114 RegLocation rl_src2);
115 void GenCmpFP(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
116 RegLocation rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 void GenConversion(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src);
Vladimir Marko1c282e22013-11-21 14:49:47 +0000118 bool GenInlinedCas(CallInfo* info, bool is_long, bool is_object);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 bool GenInlinedMinMaxInt(CallInfo* info, bool is_min);
120 bool GenInlinedSqrt(CallInfo* info);
Vladimir Markoe508a202013-11-04 15:24:22 +0000121 bool GenInlinedPeek(CallInfo* info, OpSize size);
122 bool GenInlinedPoke(CallInfo* info, OpSize size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 void GenNegLong(RegLocation rl_dest, RegLocation rl_src);
buzbee2700f7e2014-03-07 09:46:20 -0800124 void GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
125 RegLocation rl_src2);
126 void GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
127 RegLocation rl_src2);
128 void GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
129 RegLocation rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800130 // TODO: collapse reg_lo, reg_hi
131 RegLocation GenDivRem(RegLocation rl_dest, RegStorage reg_lo, RegStorage reg_hi, bool is_div);
132 RegLocation GenDivRemLit(RegLocation rl_dest, RegStorage reg_lo, int lit, bool is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 void GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
Mingyao Yange643a172014-04-08 11:02:52 -0700134 void GenDivZeroCheckWide(RegStorage reg);
Mingyao Yang80365d92014-04-18 12:10:58 -0700135 void GenArrayBoundsCheck(RegStorage index, RegStorage array_base, int32_t len_offset);
136 void GenArrayBoundsCheck(int32_t index, RegStorage array_base, int32_t len_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 void GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method);
138 void GenExitSequence();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800139 void GenSpecialExitSequence();
buzbee0d829482013-10-11 15:24:55 -0700140 void GenFillArrayData(DexOffset table_offset, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 void GenFusedFPCmpBranch(BasicBlock* bb, MIR* mir, bool gt_bias, bool is_double);
142 void GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir);
143 void GenSelect(BasicBlock* bb, MIR* mir);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700144 bool GenMemBarrier(MemBarrierKind barrier_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 void GenMoveException(RegLocation rl_dest);
buzbee2700f7e2014-03-07 09:46:20 -0800146 void GenMultiplyByTwoBitMultiplier(RegLocation rl_src, RegLocation rl_result, int lit,
147 int first_bit, int second_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 void GenNegDouble(RegLocation rl_dest, RegLocation rl_src);
149 void GenNegFloat(RegLocation rl_dest, RegLocation rl_src);
buzbee0d829482013-10-11 15:24:55 -0700150 void GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src);
151 void GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800152
Mark Mendelle02d48f2014-01-15 11:19:23 -0800153 /*
154 * @brief Generate a two address long operation with a constant value
155 * @param rl_dest location of result
156 * @param rl_src constant source operand
157 * @param op Opcode to be generated
158 */
159 void GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op);
160 /*
161 * @brief Generate a three address long operation with a constant value
162 * @param rl_dest location of result
163 * @param rl_src1 source operand
164 * @param rl_src2 constant source operand
165 * @param op Opcode to be generated
166 */
buzbee2700f7e2014-03-07 09:46:20 -0800167 void GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
168 Instruction::Code op);
Mark Mendelle02d48f2014-01-15 11:19:23 -0800169
170 /**
171 * @brief Generate a long arithmetic operation.
172 * @param rl_dest The destination.
173 * @param rl_src1 First operand.
174 * @param rl_src2 Second operand.
175 * @param op The DEX opcode for the operation.
176 * @param is_commutative The sources can be swapped if needed.
177 */
Mark Mendelle87f9b52014-04-30 14:13:18 -0400178 virtual void GenLongArith(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
179 Instruction::Code op, bool is_commutative);
Mark Mendelle02d48f2014-01-15 11:19:23 -0800180
181 /**
182 * @brief Generate a two operand long arithmetic operation.
183 * @param rl_dest The destination.
184 * @param rl_src Second operand.
185 * @param op The DEX opcode for the operation.
186 */
187 void GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op);
188
189 /**
190 * @brief Generate a long operation.
191 * @param rl_dest The destination. Must be in a register
192 * @param rl_src The other operand. May be in a register or in memory.
193 * @param op The DEX opcode for the operation.
194 */
Mark Mendelle87f9b52014-04-30 14:13:18 -0400195 virtual void GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800197 /**
198 * @brief Implement instanceof a final class with x86 specific code.
199 * @param use_declaring_class 'true' if we can use the class itself.
200 * @param type_idx Type index to use if use_declaring_class is 'false'.
201 * @param rl_dest Result to be set to 0 or 1.
202 * @param rl_src Object to be tested.
203 */
buzbee2700f7e2014-03-07 09:46:20 -0800204 void GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
205 RegLocation rl_src);
Mark Mendell6607d972014-02-10 06:54:18 -0800206 /*
207 *
208 * @brief Implement Set up instanceof a class with x86 specific code.
209 * @param needs_access_check 'true' if we must check the access.
210 * @param type_known_final 'true' if the type is known to be a final class.
211 * @param type_known_abstract 'true' if the type is known to be an abstract class.
212 * @param use_declaring_class 'true' if the type can be loaded off the current Method*.
213 * @param can_assume_type_is_in_dex_cache 'true' if the type is known to be in the cache.
214 * @param type_idx Type index to use if use_declaring_class is 'false'.
215 * @param rl_dest Result to be set to 0 or 1.
216 * @param rl_src Object to be tested.
217 */
218 void GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
219 bool type_known_abstract, bool use_declaring_class,
220 bool can_assume_type_is_in_dex_cache,
buzbee2700f7e2014-03-07 09:46:20 -0800221 uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src);
Mark Mendell6607d972014-02-10 06:54:18 -0800222
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 // Single operation generators.
224 LIR* OpUnconditionalBranch(LIR* target);
buzbee2700f7e2014-03-07 09:46:20 -0800225 LIR* OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target);
226 LIR* OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 LIR* OpCondBranch(ConditionCode cc, LIR* target);
buzbee2700f7e2014-03-07 09:46:20 -0800228 LIR* OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target);
229 LIR* OpFpRegCopy(RegStorage r_dest, RegStorage r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 LIR* OpIT(ConditionCode cond, const char* guide);
Dave Allison3da67a52014-04-02 17:03:45 -0700231 void OpEndIT(LIR* it);
buzbee2700f7e2014-03-07 09:46:20 -0800232 LIR* OpMem(OpKind op, RegStorage r_base, int disp);
233 LIR* OpPcRelLoad(RegStorage reg, LIR* target);
234 LIR* OpReg(OpKind op, RegStorage r_dest_src);
buzbee7a11ab02014-04-28 20:02:38 -0700235 void OpRegCopy(RegStorage r_dest, RegStorage r_src);
buzbee2700f7e2014-03-07 09:46:20 -0800236 LIR* OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src);
237 LIR* OpRegImm(OpKind op, RegStorage r_dest_src1, int value);
238 LIR* OpRegMem(OpKind op, RegStorage r_dest, RegStorage r_base, int offset);
239 LIR* OpRegMem(OpKind op, RegStorage r_dest, RegLocation value);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800240 LIR* OpMemReg(OpKind op, RegLocation rl_dest, int value);
buzbee2700f7e2014-03-07 09:46:20 -0800241 LIR* OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2);
242 LIR* OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type);
243 LIR* OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type);
244 LIR* OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src);
245 LIR* OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value);
246 LIR* OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 LIR* OpTestSuspend(LIR* target);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700248 LIR* OpThreadMem(OpKind op, ThreadOffset<4> thread_offset) OVERRIDE;
249 LIR* OpThreadMem(OpKind op, ThreadOffset<8> thread_offset) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -0800250 LIR* OpVldm(RegStorage r_base, int count);
251 LIR* OpVstm(RegStorage r_base, int count);
252 void OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset);
253 void OpRegCopyWide(RegStorage dest, RegStorage src);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700254 void OpTlsCmp(ThreadOffset<4> offset, int val) OVERRIDE;
255 void OpTlsCmp(ThreadOffset<8> offset, int val) OVERRIDE;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256
buzbee091cc402014-03-31 10:14:40 -0700257 void OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700258 void OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<8> thread_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 void SpillCoreRegs();
260 void UnSpillCoreRegs();
261 static const X86EncodingMap EncodingMap[kX86Last];
262 bool InexpensiveConstantInt(int32_t value);
263 bool InexpensiveConstantFloat(int32_t value);
264 bool InexpensiveConstantLong(int64_t value);
265 bool InexpensiveConstantDouble(int64_t value);
266
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800267 /*
Mark Mendelle87f9b52014-04-30 14:13:18 -0400268 * @brief Should try to optimize for two address instructions?
269 * @return true if we try to avoid generating three operand instructions.
270 */
271 virtual bool GenerateTwoOperandInstructions() const { return true; }
272
273 /*
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800274 * @brief x86 specific codegen for int operations.
275 * @param opcode Operation to perform.
276 * @param rl_dest Destination for the result.
277 * @param rl_lhs Left hand operand.
278 * @param rl_rhs Right hand operand.
279 */
buzbee2700f7e2014-03-07 09:46:20 -0800280 void GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_lhs,
281 RegLocation rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800282
Mark Mendell55d0eac2014-02-06 11:02:52 -0800283 /*
284 * @brief Dump a RegLocation using printf
285 * @param loc Register location to dump
286 */
287 static void DumpRegLocation(RegLocation loc);
288
289 /*
290 * @brief Load the Method* of a dex method into the register.
Jeff Hao49161ce2014-03-12 11:05:25 -0700291 * @param target_method The MethodReference of the method to be invoked.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800292 * @param type How the method will be invoked.
293 * @param register that will contain the code address.
294 * @note register will be passed to TargetReg to get physical register.
295 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700296 void LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800297 SpecialTargetRegister symbolic_reg);
298
299 /*
300 * @brief Load the Class* of a Dex Class type into the register.
301 * @param type How the method will be invoked.
302 * @param register that will contain the code address.
303 * @note register will be passed to TargetReg to get physical register.
304 */
305 void LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg);
306
307 /*
308 * @brief Generate a relative call to the method that will be patched at link time.
Jeff Hao49161ce2014-03-12 11:05:25 -0700309 * @param target_method The MethodReference of the method to be invoked.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800310 * @param type How the method will be invoked.
311 * @returns Call instruction
312 */
Mark Mendelle87f9b52014-04-30 14:13:18 -0400313 virtual LIR * CallWithLinkerFixup(const MethodReference& target_method, InvokeType type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800314
315 /*
316 * @brief Handle x86 specific literals
317 */
318 void InstallLiteralPools();
319
Mark Mendellae9fd932014-02-10 16:14:35 -0800320 /*
321 * @brief Generate the debug_frame CFI information.
322 * @returns pointer to vector containing CFE information
323 */
324 static std::vector<uint8_t>* ReturnCommonCallFrameInformation();
325
326 /*
327 * @brief Generate the debug_frame FDE information.
328 * @returns pointer to vector containing CFE information
329 */
330 std::vector<uint8_t>* ReturnCallFrameInformation();
331
Mark Mendelle87f9b52014-04-30 14:13:18 -0400332 protected:
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700333 size_t ComputeSize(const X86EncodingMap* entry, int base, int displacement, bool has_sib);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000334 void EmitPrefix(const X86EncodingMap* entry);
335 void EmitOpcode(const X86EncodingMap* entry);
336 void EmitPrefixAndOpcode(const X86EncodingMap* entry);
337 void EmitDisp(uint8_t base, int disp);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700338 void EmitModrmThread(uint8_t reg_or_opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000339 void EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int disp);
340 void EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index, int scale, int disp);
341 void EmitImm(const X86EncodingMap* entry, int imm);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100342 void EmitOpRegOpcode(const X86EncodingMap* entry, uint8_t reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 void EmitOpReg(const X86EncodingMap* entry, uint8_t reg);
344 void EmitOpMem(const X86EncodingMap* entry, uint8_t base, int disp);
buzbee2700f7e2014-03-07 09:46:20 -0800345 void EmitOpArray(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 void EmitMemReg(const X86EncodingMap* entry, uint8_t base, int disp, uint8_t reg);
Mark Mendell343adb52013-12-18 06:02:17 -0800347 void EmitMemImm(const X86EncodingMap* entry, uint8_t base, int disp, int32_t imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 void EmitRegMem(const X86EncodingMap* entry, uint8_t reg, uint8_t base, int disp);
349 void EmitRegArray(const X86EncodingMap* entry, uint8_t reg, uint8_t base, uint8_t index,
350 int scale, int disp);
351 void EmitArrayReg(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp,
352 uint8_t reg);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400353 void EmitArrayImm(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp,
354 int32_t imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 void EmitRegThread(const X86EncodingMap* entry, uint8_t reg, int disp);
356 void EmitRegReg(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2);
357 void EmitRegRegImm(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2, int32_t imm);
Mark Mendell4708dcd2014-01-22 09:05:18 -0800358 void EmitRegRegImmRev(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2, int32_t imm);
buzbee2700f7e2014-03-07 09:46:20 -0800359 void EmitRegMemImm(const X86EncodingMap* entry, uint8_t reg1, uint8_t base, int disp,
360 int32_t imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400361 void EmitMemRegImm(const X86EncodingMap* entry, uint8_t base, int disp, uint8_t reg1, int32_t imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 void EmitRegImm(const X86EncodingMap* entry, uint8_t reg, int imm);
363 void EmitThreadImm(const X86EncodingMap* entry, int disp, int imm);
364 void EmitMovRegImm(const X86EncodingMap* entry, uint8_t reg, int imm);
365 void EmitShiftRegImm(const X86EncodingMap* entry, uint8_t reg, int imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400366 void EmitShiftMemImm(const X86EncodingMap* entry, uint8_t base, int disp, int imm);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800367 void EmitShiftMemCl(const X86EncodingMap* entry, uint8_t base, int displacement, uint8_t cl);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 void EmitShiftRegCl(const X86EncodingMap* entry, uint8_t reg, uint8_t cl);
369 void EmitRegCond(const X86EncodingMap* entry, uint8_t reg, uint8_t condition);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400370 void EmitMemCond(const X86EncodingMap* entry, uint8_t base, int displacement, uint8_t condition);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800371
372 /**
373 * @brief Used for encoding conditional register to register operation.
374 * @param entry The entry in the encoding map for the opcode.
375 * @param reg1 The first physical register.
376 * @param reg2 The second physical register.
377 * @param condition The condition code for operation.
378 */
379 void EmitRegRegCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2, uint8_t condition);
380
Mark Mendell2637f2e2014-04-30 10:10:47 -0400381 /**
382 * @brief Used for encoding conditional register to memory operation.
383 * @param entry The entry in the encoding map for the opcode.
384 * @param reg1 The first physical register.
385 * @param base The memory base register.
386 * @param displacement The memory displacement.
387 * @param condition The condition code for operation.
388 */
389 void EmitRegMemCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t base, int displacement, uint8_t condition);
390
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 void EmitJmp(const X86EncodingMap* entry, int rel);
392 void EmitJcc(const X86EncodingMap* entry, int rel, uint8_t cc);
393 void EmitCallMem(const X86EncodingMap* entry, uint8_t base, int disp);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800394 void EmitCallImmediate(const X86EncodingMap* entry, int disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 void EmitCallThread(const X86EncodingMap* entry, int disp);
396 void EmitPcRel(const X86EncodingMap* entry, uint8_t reg, int base_or_table, uint8_t index,
397 int scale, int table_or_disp);
398 void EmitMacro(const X86EncodingMap* entry, uint8_t reg, int offset);
399 void EmitUnimplemented(const X86EncodingMap* entry, LIR* lir);
Mark Mendell412d4f82013-12-18 13:32:36 -0800400 void GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
401 int64_t val, ConditionCode ccode);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000402 void GenConstWide(RegLocation rl_dest, int64_t value);
Mark Mendelle02d48f2014-01-15 11:19:23 -0800403
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800404 static bool ProvidesFullMemoryBarrier(X86OpCode opcode);
405
Mark Mendelle02d48f2014-01-15 11:19:23 -0800406 /*
Mark Mendelle87f9b52014-04-30 14:13:18 -0400407 * @brief Ensure that a temporary register is byte addressable.
408 * @returns a temporary guarenteed to be byte addressable.
409 */
410 virtual RegStorage AllocateByteRegister();
411
412 /*
Mark Mendell4028a6c2014-02-19 20:06:20 -0800413 * @brief generate inline code for fast case of Strng.indexOf.
414 * @param info Call parameters
415 * @param zero_based 'true' if the index into the string is 0.
416 * @returns 'true' if the call was inlined, 'false' if a regular call needs to be
417 * generated.
418 */
419 bool GenInlinedIndexOf(CallInfo* info, bool zero_based);
420
421 /*
Mark Mendelld65c51a2014-04-29 16:55:20 -0400422 * @brief Load 128 bit constant into vector register.
423 * @param bb The basic block in which the MIR is from.
424 * @param mir The MIR whose opcode is kMirConstVector
425 * @note vA is the TypeSize for the register.
426 * @note vB is the destination XMM register. arg[0..3] are 32 bit constant values.
427 */
428 void GenConst128(BasicBlock* bb, MIR* mir);
429
430 /*
431 * @brief Generate code for a vector opcode.
432 * @param bb The basic block in which the MIR is from.
433 * @param mir The MIR whose opcode is a non-standard opcode.
434 */
435 void GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir);
436
437 /*
Mark Mendelle02d48f2014-01-15 11:19:23 -0800438 * @brief Return the correct x86 opcode for the Dex operation
439 * @param op Dex opcode for the operation
440 * @param loc Register location of the operand
441 * @param is_high_op 'true' if this is an operation on the high word
442 * @param value Immediate value for the operation. Used for byte variants
443 * @returns the correct x86 opcode to perform the operation
444 */
445 X86OpCode GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op, int32_t value);
446
447 /*
448 * @brief Return the correct x86 opcode for the Dex operation
449 * @param op Dex opcode for the operation
450 * @param dest location of the destination. May be register or memory.
451 * @param rhs Location for the rhs of the operation. May be in register or memory.
452 * @param is_high_op 'true' if this is an operation on the high word
453 * @returns the correct x86 opcode to perform the operation
454 * @note at most one location may refer to memory
455 */
456 X86OpCode GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
457 bool is_high_op);
458
459 /*
460 * @brief Is this operation a no-op for this opcode and value
461 * @param op Dex opcode for the operation
462 * @param value Immediate value for the operation.
463 * @returns 'true' if the operation will have no effect
464 */
465 bool IsNoOp(Instruction::Code op, int32_t value);
466
Mark Mendell2bf31e62014-01-23 12:13:40 -0800467 /**
468 * @brief Calculate magic number and shift for a given divisor
469 * @param divisor divisor number for calculation
470 * @param magic hold calculated magic number
471 * @param shift hold calculated shift
472 */
473 void CalculateMagicAndShift(int divisor, int& magic, int& shift);
474
475 /*
476 * @brief Generate an integer div or rem operation.
477 * @param rl_dest Destination Location.
478 * @param rl_src1 Numerator Location.
479 * @param rl_src2 Divisor Location.
480 * @param is_div 'true' if this is a division, 'false' for a remainder.
481 * @param check_zero 'true' if an exception should be generated if the divisor is 0.
482 */
buzbee2700f7e2014-03-07 09:46:20 -0800483 RegLocation GenDivRem(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
484 bool is_div, bool check_zero);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800485
486 /*
487 * @brief Generate an integer div or rem operation by a literal.
488 * @param rl_dest Destination Location.
489 * @param rl_src Numerator Location.
490 * @param lit Divisor.
491 * @param is_div 'true' if this is a division, 'false' for a remainder.
492 */
493 RegLocation GenDivRemLit(RegLocation rl_dest, RegLocation rl_src, int lit, bool is_div);
Mark Mendell4708dcd2014-01-22 09:05:18 -0800494
495 /*
496 * Generate code to implement long shift operations.
497 * @param opcode The DEX opcode to specify the shift type.
498 * @param rl_dest The destination.
499 * @param rl_src The value to be shifted.
500 * @param shift_amount How much to shift.
501 * @returns the RegLocation of the result.
502 */
503 RegLocation GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
504 RegLocation rl_src, int shift_amount);
505 /*
506 * Generate an imul of a register by a constant or a better sequence.
507 * @param dest Destination Register.
508 * @param src Source Register.
509 * @param val Constant multiplier.
510 */
buzbee2700f7e2014-03-07 09:46:20 -0800511 void GenImulRegImm(RegStorage dest, RegStorage src, int val);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800512
Mark Mendell4708dcd2014-01-22 09:05:18 -0800513 /*
514 * Generate an imul of a memory location by a constant or a better sequence.
515 * @param dest Destination Register.
516 * @param sreg Symbolic register.
517 * @param displacement Displacement on stack of Symbolic Register.
518 * @param val Constant multiplier.
519 */
buzbee2700f7e2014-03-07 09:46:20 -0800520 void GenImulMemImm(RegStorage dest, int sreg, int displacement, int val);
Mark Mendell766e9292014-01-27 07:55:47 -0800521
522 /*
523 * @brief Compare memory to immediate, and branch if condition true.
524 * @param cond The condition code that when true will branch to the target.
525 * @param temp_reg A temporary register that can be used if compare memory is not
526 * supported by the architecture.
527 * @param base_reg The register holding the base address.
528 * @param offset The offset from the base.
529 * @param check_value The immediate to compare to.
530 */
buzbee2700f7e2014-03-07 09:46:20 -0800531 LIR* OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Mark Mendell766e9292014-01-27 07:55:47 -0800532 int offset, int check_value, LIR* target);
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800533
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800534 /*
535 * Can this operation be using core registers without temporaries?
536 * @param rl_lhs Left hand operand.
537 * @param rl_rhs Right hand operand.
538 * @returns 'true' if the operation can proceed without needing temporary regs.
539 */
540 bool IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs);
Mark Mendell67c39c42014-01-31 17:28:00 -0800541
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800542 /**
543 * @brief Generates inline code for conversion of long to FP by using x87/
544 * @param rl_dest The destination of the FP.
545 * @param rl_src The source of the long.
546 * @param is_double 'true' if dealing with double, 'false' for float.
547 */
Mark Mendelle87f9b52014-04-30 14:13:18 -0400548 virtual void GenLongToFP(RegLocation rl_dest, RegLocation rl_src, bool is_double);
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800549
Mark Mendell67c39c42014-01-31 17:28:00 -0800550 /*
551 * @brief Perform MIR analysis before compiling method.
552 * @note Invokes Mir2LiR::Materialize after analysis.
553 */
554 void Materialize();
555
556 /*
buzbee30adc732014-05-09 15:10:18 -0700557 * Mir2Lir's UpdateLoc() looks to see if the Dalvik value is currently live in any temp register
558 * without regard to data type. In practice, this can result in UpdateLoc returning a
559 * location record for a Dalvik float value in a core register, and vis-versa. For targets
560 * which can inexpensively move data between core and float registers, this can often be a win.
561 * However, for x86 this is generally not a win. These variants of UpdateLoc()
562 * take a register class argument - and will return an in-register location record only if
563 * the value is live in a temp register of the correct class. Additionally, if the value is in
564 * a temp register of the wrong register class, it will be clobbered.
565 */
566 RegLocation UpdateLocTyped(RegLocation loc, int reg_class);
567 RegLocation UpdateLocWideTyped(RegLocation loc, int reg_class);
568
569 /*
Mark Mendell67c39c42014-01-31 17:28:00 -0800570 * @brief Analyze MIR before generating code, to prepare for the code generation.
571 */
572 void AnalyzeMIR();
573
574 /*
575 * @brief Analyze one basic block.
576 * @param bb Basic block to analyze.
577 */
578 void AnalyzeBB(BasicBlock * bb);
579
580 /*
581 * @brief Analyze one extended MIR instruction
582 * @param opcode MIR instruction opcode.
583 * @param bb Basic block containing instruction.
584 * @param mir Extended instruction to analyze.
585 */
586 void AnalyzeExtendedMIR(int opcode, BasicBlock * bb, MIR *mir);
587
588 /*
589 * @brief Analyze one MIR instruction
590 * @param opcode MIR instruction opcode.
591 * @param bb Basic block containing instruction.
592 * @param mir Instruction to analyze.
593 */
Mark Mendelle87f9b52014-04-30 14:13:18 -0400594 virtual void AnalyzeMIR(int opcode, BasicBlock * bb, MIR *mir);
Mark Mendell67c39c42014-01-31 17:28:00 -0800595
596 /*
597 * @brief Analyze one MIR float/double instruction
598 * @param opcode MIR instruction opcode.
599 * @param bb Basic block containing instruction.
600 * @param mir Instruction to analyze.
601 */
602 void AnalyzeFPInstruction(int opcode, BasicBlock * bb, MIR *mir);
603
604 /*
605 * @brief Analyze one use of a double operand.
606 * @param rl_use Double RegLocation for the operand.
607 */
608 void AnalyzeDoubleUse(RegLocation rl_use);
609
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700610 bool Gen64Bit() const { return gen64bit_; }
611
Mark Mendell67c39c42014-01-31 17:28:00 -0800612 // Information derived from analysis of MIR
613
Mark Mendell55d0eac2014-02-06 11:02:52 -0800614 // The compiler temporary for the code address of the method.
615 CompilerTemp *base_of_code_;
616
Mark Mendell67c39c42014-01-31 17:28:00 -0800617 // Have we decided to compute a ptr to code and store in temporary VR?
618 bool store_method_addr_;
619
Mark Mendell55d0eac2014-02-06 11:02:52 -0800620 // Have we used the stored method address?
621 bool store_method_addr_used_;
622
623 // Instructions to remove if we didn't use the stored method address.
624 LIR* setup_method_address_[2];
625
626 // Instructions needing patching with Method* values.
627 GrowableArray<LIR*> method_address_insns_;
628
629 // Instructions needing patching with Class Type* values.
630 GrowableArray<LIR*> class_type_address_insns_;
631
632 // Instructions needing patching with PC relative code addresses.
633 GrowableArray<LIR*> call_method_insns_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800634
635 // Prologue decrement of stack pointer.
636 LIR* stack_decrement_;
637
638 // Epilogue increment of stack pointer.
639 LIR* stack_increment_;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700640
641 // 64-bit mode
642 bool gen64bit_;
Mark Mendelld65c51a2014-04-29 16:55:20 -0400643
644 // The list of const vector literals.
645 LIR *const_vectors_;
646
647 /*
648 * @brief Search for a matching vector literal
649 * @param mir A kMirOpConst128b MIR instruction to match.
650 * @returns pointer to matching LIR constant, or nullptr if not found.
651 */
652 LIR *ScanVectorLiteral(MIR *mir);
653
654 /*
655 * @brief Add a constant vector literal
656 * @param mir A kMirOpConst128b MIR instruction to match.
657 */
658 LIR *AddVectorLiteral(MIR *mir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659};
660
661} // namespace art
662
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700663#endif // ART_COMPILER_DEX_QUICK_X86_CODEGEN_X86_H_