blob: 767fe250d8292c955616d96ba6890579dbd4d445 [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 Marko80afd022015-05-19 18:08:00 +010024#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/* Mark a temp register as dead. Does not affect allocation state. */
29inline void Mir2Lir::ClobberBody(RegisterInfo* p) {
buzbeeba574512014-05-12 15:13:16 -070030 DCHECK(p->IsTemp());
buzbee082833c2014-05-17 23:16:26 -070031 if (p->SReg() != INVALID_SREG) {
buzbee091cc402014-03-31 10:14:40 -070032 DCHECK(!(p->IsLive() && p->IsDirty())) << "Live & dirty temp in clobber";
buzbee30adc732014-05-09 15:10:18 -070033 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070034 if (p->IsWide()) {
35 p->SetIsWide(false);
buzbeeb5860fb2014-06-21 15:31:01 -070036 if (p->GetReg().NotExactlyEquals(p->Partner())) {
buzbee091cc402014-03-31 10:14:40 -070037 // Register pair - deal with the other half.
38 p = GetRegInfo(p->Partner());
39 p->SetIsWide(false);
buzbee30adc732014-05-09 15:10:18 -070040 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070041 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 }
43 }
44}
45
buzbee0d829482013-10-11 15:24:55 -070046inline LIR* Mir2Lir::RawLIR(DexOffset dalvik_offset, int opcode, int op0,
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 int op1, int op2, int op3, int op4, LIR* target) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000048 LIR* insn = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 insn->dalvik_offset = dalvik_offset;
50 insn->opcode = opcode;
51 insn->operands[0] = op0;
52 insn->operands[1] = op1;
53 insn->operands[2] = op2;
54 insn->operands[3] = op3;
55 insn->operands[4] = op4;
56 insn->target = target;
57 SetupResourceMasks(insn);
58 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
59 (opcode == kPseudoExportedPC)) {
60 // Always make labels scheduling barriers
buzbeeb48819d2013-09-14 16:15:25 -070061 DCHECK(!insn->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010062 insn->u.m.use_mask = insn->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 }
64 return insn;
65}
66
67/*
68 * The following are building blocks to construct low-level IRs with 0 - 4
69 * operands.
70 */
71inline LIR* Mir2Lir::NewLIR0(int opcode) {
buzbee409fe942013-10-11 10:49:56 -070072 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & NO_OPERAND))
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 << GetTargetInstName(opcode) << " " << opcode << " "
74 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
75 << current_dalvik_offset_;
76 LIR* insn = RawLIR(current_dalvik_offset_, opcode);
77 AppendLIR(insn);
78 return insn;
79}
80
81inline LIR* Mir2Lir::NewLIR1(int opcode, int dest) {
buzbee409fe942013-10-11 10:49:56 -070082 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_UNARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 << GetTargetInstName(opcode) << " " << opcode << " "
84 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
85 << current_dalvik_offset_;
86 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest);
87 AppendLIR(insn);
88 return insn;
89}
90
91inline LIR* Mir2Lir::NewLIR2(int opcode, int dest, int src1) {
buzbee409fe942013-10-11 10:49:56 -070092 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 << GetTargetInstName(opcode) << " " << opcode << " "
94 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
95 << current_dalvik_offset_;
96 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1);
97 AppendLIR(insn);
98 return insn;
99}
100
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800101inline LIR* Mir2Lir::NewLIR2NoDest(int opcode, int src, int info) {
Haitao Fenga870bc52014-09-09 15:52:34 +0800102 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800103 << GetTargetInstName(opcode) << " " << opcode << " "
104 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
105 << current_dalvik_offset_;
106 LIR* insn = RawLIR(current_dalvik_offset_, opcode, src, info);
107 AppendLIR(insn);
108 return insn;
109}
110
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111inline LIR* Mir2Lir::NewLIR3(int opcode, int dest, int src1, int src2) {
buzbee409fe942013-10-11 10:49:56 -0700112 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 << GetTargetInstName(opcode) << " " << opcode << " "
114 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
115 << current_dalvik_offset_;
116 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2);
117 AppendLIR(insn);
118 return insn;
119}
120
121inline LIR* Mir2Lir::NewLIR4(int opcode, int dest, int src1, int src2, int info) {
buzbee409fe942013-10-11 10:49:56 -0700122 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUAD_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 << GetTargetInstName(opcode) << " " << opcode << " "
124 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
125 << current_dalvik_offset_;
126 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info);
127 AppendLIR(insn);
128 return insn;
129}
130
131inline LIR* Mir2Lir::NewLIR5(int opcode, int dest, int src1, int src2, int info1,
132 int info2) {
buzbee409fe942013-10-11 10:49:56 -0700133 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUIN_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 << GetTargetInstName(opcode) << " " << opcode << " "
135 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
136 << current_dalvik_offset_;
137 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info1, info2);
138 AppendLIR(insn);
139 return insn;
140}
141
142/*
143 * Mark the corresponding bit(s).
144 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100145inline void Mir2Lir::SetupRegMask(ResourceMask* mask, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700146 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100147 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
148 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
149 *mask = mask->Union(reginfo_map_[reg]->DefUseMask());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150}
151
152/*
Serban Constantinescu63999682014-07-15 17:44:21 +0100153 * Clear the corresponding bit(s).
154 */
155inline void Mir2Lir::ClearRegMask(ResourceMask* mask, int reg) {
156 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100157 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
158 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
159 *mask = mask->ClearBits(reginfo_map_[reg]->DefUseMask());
Serban Constantinescu63999682014-07-15 17:44:21 +0100160}
161
162/*
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 * Set up the proper fields in the resource mask
164 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100165inline void Mir2Lir::SetupResourceMasks(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 int opcode = lir->opcode;
167
buzbee409fe942013-10-11 10:49:56 -0700168 if (IsPseudoLirOp(opcode)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100169 lir->u.m.use_mask = lir->u.m.def_mask = &kEncodeNone;
buzbee409fe942013-10-11 10:49:56 -0700170 if (opcode != kPseudoBarrier) {
171 lir->flags.fixup = kFixupLabel;
172 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 return;
174 }
175
176 uint64_t flags = GetTargetInstFlags(opcode);
177
178 if (flags & NEEDS_FIXUP) {
buzbeeb48819d2013-09-14 16:15:25 -0700179 // Note: target-specific setup may specialize the fixup kind.
180 lir->flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 }
182
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100183 /* Get the starting size of the instruction's template. */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 lir->flags.size = GetInsnSize(lir);
buzbeeb48819d2013-09-14 16:15:25 -0700185 estimated_native_code_size_ += lir->flags.size;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100186
187 /* Set up the mask for resources. */
188 ResourceMask use_mask;
189 ResourceMask def_mask;
190
191 if (flags & (IS_LOAD | IS_STORE)) {
192 /* Set memory reference type (defaults to heap, overridden by ScopedMemRefType). */
193 if (flags & IS_LOAD) {
194 use_mask.SetBit(mem_ref_type_);
195 } else {
196 /* Currently only loads can be marked as kMustNotAlias. */
197 DCHECK(mem_ref_type_ != ResourceMask::kMustNotAlias);
198 }
199 if (flags & IS_STORE) {
200 /* Literals cannot be written to. */
201 DCHECK(mem_ref_type_ != ResourceMask::kLiteral);
202 def_mask.SetBit(mem_ref_type_);
203 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 }
205
206 /*
207 * Conservatively assume the branch here will call out a function that in
208 * turn will trash everything.
209 */
210 if (flags & IS_BRANCH) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100211 lir->u.m.def_mask = lir->u.m.use_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 return;
213 }
214
215 if (flags & REG_DEF0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100216 SetupRegMask(&def_mask, lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 }
218
219 if (flags & REG_DEF1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100220 SetupRegMask(&def_mask, lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 }
222
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800223 if (flags & REG_DEF2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100224 SetupRegMask(&def_mask, lir->operands[2]);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800225 }
226
buzbeeb48819d2013-09-14 16:15:25 -0700227 if (flags & REG_USE0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100228 SetupRegMask(&use_mask, lir->operands[0]);
buzbeeb48819d2013-09-14 16:15:25 -0700229 }
230
231 if (flags & REG_USE1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100232 SetupRegMask(&use_mask, lir->operands[1]);
buzbeeb48819d2013-09-14 16:15:25 -0700233 }
234
235 if (flags & REG_USE2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100236 SetupRegMask(&use_mask, lir->operands[2]);
buzbeeb48819d2013-09-14 16:15:25 -0700237 }
238
239 if (flags & REG_USE3) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100240 SetupRegMask(&use_mask, lir->operands[3]);
buzbeeb48819d2013-09-14 16:15:25 -0700241 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242
buzbee17189ac2013-11-08 11:07:02 -0800243 if (flags & REG_USE4) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100244 SetupRegMask(&use_mask, lir->operands[4]);
buzbee17189ac2013-11-08 11:07:02 -0800245 }
246
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 if (flags & SETS_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100248 def_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 }
250
251 if (flags & USES_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100252 use_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 }
254
255 // Handle target-specific actions
Junmo Parkaa839cc2014-08-30 20:13:02 +0900256 SetupTargetResourceMasks(lir, flags, &use_mask, &def_mask);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100257
258 lir->u.m.use_mask = mask_cache_.GetMask(use_mask);
259 lir->u.m.def_mask = mask_cache_.GetMask(def_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260}
261
buzbee091cc402014-03-31 10:14:40 -0700262inline art::Mir2Lir::RegisterInfo* Mir2Lir::GetRegInfo(RegStorage reg) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100263 RegisterInfo* res = reg.IsPair() ? reginfo_map_[reg.GetLowReg()] : reginfo_map_[reg.GetReg()];
buzbee091cc402014-03-31 10:14:40 -0700264 DCHECK(res != nullptr);
265 return res;
buzbeebd663de2013-09-10 15:41:31 -0700266}
267
Andreas Gampe4b537a82014-06-30 22:24:53 -0700268inline void Mir2Lir::CheckRegLocation(RegLocation rl) const {
269 if (kFailOnSizeError || kReportSizeError) {
270 CheckRegLocationImpl(rl, kFailOnSizeError, kReportSizeError);
271 }
272}
273
274inline void Mir2Lir::CheckRegStorage(RegStorage rs, WidenessCheck wide, RefCheck ref, FPCheck fp)
275 const {
276 if (kFailOnSizeError || kReportSizeError) {
277 CheckRegStorageImpl(rs, wide, ref, fp, kFailOnSizeError, kReportSizeError);
278 }
279}
280
Serguei Katkov717a3e42014-11-13 17:19:42 +0600281inline Mir2Lir::ShortyIterator::ShortyIterator(const char* shorty, bool is_static)
282 : cur_(shorty + 1), pending_this_(!is_static), initialized_(false) {
283 DCHECK(shorty != nullptr);
284 DCHECK_NE(*shorty, 0);
285}
286
287inline bool Mir2Lir::ShortyIterator::Next() {
288 if (!initialized_) {
289 initialized_ = true;
290 } else if (pending_this_) {
291 pending_this_ = false;
292 } else if (*cur_ != 0) {
293 cur_++;
294 }
295
296 return *cur_ != 0 || pending_this_;
297}
298
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299} // namespace art
300
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700301#endif // ART_COMPILER_DEX_QUICK_MIR_TO_LIR_INL_H_