blob: f96816c3d947a23c844632d9a4a2c30eb9a73a77 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2013 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_MIR_TO_LIR_INL_H_
18#define ART_COMPILER_DEX_QUICK_MIR_TO_LIR_INL_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include "mir_to_lir.h"
21
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "base/logging.h"
23#include "dex/compiler_ir.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010024#include "gc_root.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
29/* Mark a temp register as dead. Does not affect allocation state. */
30inline void Mir2Lir::ClobberBody(RegisterInfo* p) {
buzbeeba574512014-05-12 15:13:16 -070031 DCHECK(p->IsTemp());
buzbee082833c2014-05-17 23:16:26 -070032 if (p->SReg() != INVALID_SREG) {
buzbee091cc402014-03-31 10:14:40 -070033 DCHECK(!(p->IsLive() && p->IsDirty())) << "Live & dirty temp in clobber";
buzbee30adc732014-05-09 15:10:18 -070034 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070035 if (p->IsWide()) {
36 p->SetIsWide(false);
buzbeeb5860fb2014-06-21 15:31:01 -070037 if (p->GetReg().NotExactlyEquals(p->Partner())) {
buzbee091cc402014-03-31 10:14:40 -070038 // Register pair - deal with the other half.
39 p = GetRegInfo(p->Partner());
40 p->SetIsWide(false);
buzbee30adc732014-05-09 15:10:18 -070041 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070042 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 }
44 }
45}
46
buzbee0d829482013-10-11 15:24:55 -070047inline LIR* Mir2Lir::RawLIR(DexOffset dalvik_offset, int opcode, int op0,
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 int op1, int op2, int op3, int op4, LIR* target) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000049 LIR* insn = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070050 insn->dalvik_offset = dalvik_offset;
51 insn->opcode = opcode;
52 insn->operands[0] = op0;
53 insn->operands[1] = op1;
54 insn->operands[2] = op2;
55 insn->operands[3] = op3;
56 insn->operands[4] = op4;
57 insn->target = target;
58 SetupResourceMasks(insn);
59 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
60 (opcode == kPseudoExportedPC)) {
61 // Always make labels scheduling barriers
buzbeeb48819d2013-09-14 16:15:25 -070062 DCHECK(!insn->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010063 insn->u.m.use_mask = insn->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 }
65 return insn;
66}
67
68/*
69 * The following are building blocks to construct low-level IRs with 0 - 4
70 * operands.
71 */
72inline LIR* Mir2Lir::NewLIR0(int opcode) {
buzbee409fe942013-10-11 10:49:56 -070073 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & NO_OPERAND))
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 << GetTargetInstName(opcode) << " " << opcode << " "
75 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
76 << current_dalvik_offset_;
77 LIR* insn = RawLIR(current_dalvik_offset_, opcode);
78 AppendLIR(insn);
79 return insn;
80}
81
82inline LIR* Mir2Lir::NewLIR1(int opcode, int dest) {
buzbee409fe942013-10-11 10:49:56 -070083 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_UNARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 << GetTargetInstName(opcode) << " " << opcode << " "
85 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
86 << current_dalvik_offset_;
87 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest);
88 AppendLIR(insn);
89 return insn;
90}
91
92inline LIR* Mir2Lir::NewLIR2(int opcode, int dest, int src1) {
buzbee409fe942013-10-11 10:49:56 -070093 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 << GetTargetInstName(opcode) << " " << opcode << " "
95 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
96 << current_dalvik_offset_;
97 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1);
98 AppendLIR(insn);
99 return insn;
100}
101
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800102inline LIR* Mir2Lir::NewLIR2NoDest(int opcode, int src, int info) {
Haitao Fenga870bc52014-09-09 15:52:34 +0800103 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800104 << GetTargetInstName(opcode) << " " << opcode << " "
105 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
106 << current_dalvik_offset_;
107 LIR* insn = RawLIR(current_dalvik_offset_, opcode, src, info);
108 AppendLIR(insn);
109 return insn;
110}
111
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112inline LIR* Mir2Lir::NewLIR3(int opcode, int dest, int src1, int src2) {
buzbee409fe942013-10-11 10:49:56 -0700113 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 << GetTargetInstName(opcode) << " " << opcode << " "
115 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
116 << current_dalvik_offset_;
117 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2);
118 AppendLIR(insn);
119 return insn;
120}
121
122inline LIR* Mir2Lir::NewLIR4(int opcode, int dest, int src1, int src2, int info) {
buzbee409fe942013-10-11 10:49:56 -0700123 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUAD_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 << GetTargetInstName(opcode) << " " << opcode << " "
125 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
126 << current_dalvik_offset_;
127 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info);
128 AppendLIR(insn);
129 return insn;
130}
131
132inline LIR* Mir2Lir::NewLIR5(int opcode, int dest, int src1, int src2, int info1,
133 int info2) {
buzbee409fe942013-10-11 10:49:56 -0700134 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUIN_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 << GetTargetInstName(opcode) << " " << opcode << " "
136 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
137 << current_dalvik_offset_;
138 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info1, info2);
139 AppendLIR(insn);
140 return insn;
141}
142
143/*
144 * Mark the corresponding bit(s).
145 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100146inline void Mir2Lir::SetupRegMask(ResourceMask* mask, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700147 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100148 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
149 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
150 *mask = mask->Union(reginfo_map_[reg]->DefUseMask());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151}
152
153/*
Serban Constantinescu63999682014-07-15 17:44:21 +0100154 * Clear the corresponding bit(s).
155 */
156inline void Mir2Lir::ClearRegMask(ResourceMask* mask, int reg) {
157 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100158 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
159 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
160 *mask = mask->ClearBits(reginfo_map_[reg]->DefUseMask());
Serban Constantinescu63999682014-07-15 17:44:21 +0100161}
162
163/*
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 * Set up the proper fields in the resource mask
165 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100166inline void Mir2Lir::SetupResourceMasks(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 int opcode = lir->opcode;
168
buzbee409fe942013-10-11 10:49:56 -0700169 if (IsPseudoLirOp(opcode)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100170 lir->u.m.use_mask = lir->u.m.def_mask = &kEncodeNone;
buzbee409fe942013-10-11 10:49:56 -0700171 if (opcode != kPseudoBarrier) {
172 lir->flags.fixup = kFixupLabel;
173 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 return;
175 }
176
177 uint64_t flags = GetTargetInstFlags(opcode);
178
179 if (flags & NEEDS_FIXUP) {
buzbeeb48819d2013-09-14 16:15:25 -0700180 // Note: target-specific setup may specialize the fixup kind.
181 lir->flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 }
183
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100184 /* Get the starting size of the instruction's template. */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 lir->flags.size = GetInsnSize(lir);
buzbeeb48819d2013-09-14 16:15:25 -0700186 estimated_native_code_size_ += lir->flags.size;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100187
188 /* Set up the mask for resources. */
189 ResourceMask use_mask;
190 ResourceMask def_mask;
191
192 if (flags & (IS_LOAD | IS_STORE)) {
193 /* Set memory reference type (defaults to heap, overridden by ScopedMemRefType). */
194 if (flags & IS_LOAD) {
195 use_mask.SetBit(mem_ref_type_);
196 } else {
197 /* Currently only loads can be marked as kMustNotAlias. */
198 DCHECK(mem_ref_type_ != ResourceMask::kMustNotAlias);
199 }
200 if (flags & IS_STORE) {
201 /* Literals cannot be written to. */
202 DCHECK(mem_ref_type_ != ResourceMask::kLiteral);
203 def_mask.SetBit(mem_ref_type_);
204 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 }
206
207 /*
208 * Conservatively assume the branch here will call out a function that in
209 * turn will trash everything.
210 */
211 if (flags & IS_BRANCH) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100212 lir->u.m.def_mask = lir->u.m.use_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 return;
214 }
215
216 if (flags & REG_DEF0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100217 SetupRegMask(&def_mask, lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 }
219
220 if (flags & REG_DEF1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100221 SetupRegMask(&def_mask, lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 }
223
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800224 if (flags & REG_DEF2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100225 SetupRegMask(&def_mask, lir->operands[2]);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800226 }
227
buzbeeb48819d2013-09-14 16:15:25 -0700228 if (flags & REG_USE0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100229 SetupRegMask(&use_mask, lir->operands[0]);
buzbeeb48819d2013-09-14 16:15:25 -0700230 }
231
232 if (flags & REG_USE1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100233 SetupRegMask(&use_mask, lir->operands[1]);
buzbeeb48819d2013-09-14 16:15:25 -0700234 }
235
236 if (flags & REG_USE2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100237 SetupRegMask(&use_mask, lir->operands[2]);
buzbeeb48819d2013-09-14 16:15:25 -0700238 }
239
240 if (flags & REG_USE3) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100241 SetupRegMask(&use_mask, lir->operands[3]);
buzbeeb48819d2013-09-14 16:15:25 -0700242 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243
buzbee17189ac2013-11-08 11:07:02 -0800244 if (flags & REG_USE4) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100245 SetupRegMask(&use_mask, lir->operands[4]);
buzbee17189ac2013-11-08 11:07:02 -0800246 }
247
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 if (flags & SETS_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100249 def_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 }
251
252 if (flags & USES_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100253 use_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 }
255
256 // Handle target-specific actions
Junmo Parkaa839cc2014-08-30 20:13:02 +0900257 SetupTargetResourceMasks(lir, flags, &use_mask, &def_mask);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100258
259 lir->u.m.use_mask = mask_cache_.GetMask(use_mask);
260 lir->u.m.def_mask = mask_cache_.GetMask(def_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261}
262
buzbee091cc402014-03-31 10:14:40 -0700263inline art::Mir2Lir::RegisterInfo* Mir2Lir::GetRegInfo(RegStorage reg) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100264 RegisterInfo* res = reg.IsPair() ? reginfo_map_[reg.GetLowReg()] : reginfo_map_[reg.GetReg()];
buzbee091cc402014-03-31 10:14:40 -0700265 DCHECK(res != nullptr);
266 return res;
buzbeebd663de2013-09-10 15:41:31 -0700267}
268
Andreas Gampe4b537a82014-06-30 22:24:53 -0700269inline void Mir2Lir::CheckRegLocation(RegLocation rl) const {
270 if (kFailOnSizeError || kReportSizeError) {
271 CheckRegLocationImpl(rl, kFailOnSizeError, kReportSizeError);
272 }
273}
274
275inline void Mir2Lir::CheckRegStorage(RegStorage rs, WidenessCheck wide, RefCheck ref, FPCheck fp)
276 const {
277 if (kFailOnSizeError || kReportSizeError) {
278 CheckRegStorageImpl(rs, wide, ref, fp, kFailOnSizeError, kReportSizeError);
279 }
280}
281
Vladimir Marko05792b92015-08-03 11:56:49 +0100282inline size_t Mir2Lir::GetCacheOffset(uint32_t index) {
283 return sizeof(GcRoot<mirror::Object>) * index;
284}
285
286inline size_t Mir2Lir::GetCachePointerOffset(uint32_t index, size_t pointer_size) {
287 return pointer_size * index;
288}
289
Serguei Katkov717a3e42014-11-13 17:19:42 +0600290inline Mir2Lir::ShortyIterator::ShortyIterator(const char* shorty, bool is_static)
291 : cur_(shorty + 1), pending_this_(!is_static), initialized_(false) {
292 DCHECK(shorty != nullptr);
293 DCHECK_NE(*shorty, 0);
294}
295
296inline bool Mir2Lir::ShortyIterator::Next() {
297 if (!initialized_) {
298 initialized_ = true;
299 } else if (pending_this_) {
300 pending_this_ = false;
301 } else if (*cur_ != 0) {
302 cur_++;
303 }
304
305 return *cur_ != 0 || pending_this_;
306}
307
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308} // namespace art
309
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700310#endif // ART_COMPILER_DEX_QUICK_MIR_TO_LIR_INL_H_