blob: 280dbbe106419a7bb44bb6c282ad6b9c4bb1e03f [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
27/* Mark a temp register as dead. Does not affect allocation state. */
28inline void Mir2Lir::ClobberBody(RegisterInfo* p) {
buzbeeba574512014-05-12 15:13:16 -070029 DCHECK(p->IsTemp());
buzbee082833c2014-05-17 23:16:26 -070030 if (p->SReg() != INVALID_SREG) {
buzbee091cc402014-03-31 10:14:40 -070031 DCHECK(!(p->IsLive() && p->IsDirty())) << "Live & dirty temp in clobber";
buzbee30adc732014-05-09 15:10:18 -070032 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070033 if (p->IsWide()) {
34 p->SetIsWide(false);
buzbeeb5860fb2014-06-21 15:31:01 -070035 if (p->GetReg().NotExactlyEquals(p->Partner())) {
buzbee091cc402014-03-31 10:14:40 -070036 // Register pair - deal with the other half.
37 p = GetRegInfo(p->Partner());
38 p->SetIsWide(false);
buzbee30adc732014-05-09 15:10:18 -070039 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070040 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070041 }
42 }
43}
44
buzbee0d829482013-10-11 15:24:55 -070045inline LIR* Mir2Lir::RawLIR(DexOffset dalvik_offset, int opcode, int op0,
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 int op1, int op2, int op3, int op4, LIR* target) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000047 LIR* insn = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 insn->dalvik_offset = dalvik_offset;
49 insn->opcode = opcode;
50 insn->operands[0] = op0;
51 insn->operands[1] = op1;
52 insn->operands[2] = op2;
53 insn->operands[3] = op3;
54 insn->operands[4] = op4;
55 insn->target = target;
56 SetupResourceMasks(insn);
57 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
58 (opcode == kPseudoExportedPC)) {
59 // Always make labels scheduling barriers
buzbeeb48819d2013-09-14 16:15:25 -070060 DCHECK(!insn->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010061 insn->u.m.use_mask = insn->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 }
63 return insn;
64}
65
66/*
67 * The following are building blocks to construct low-level IRs with 0 - 4
68 * operands.
69 */
70inline LIR* Mir2Lir::NewLIR0(int opcode) {
buzbee409fe942013-10-11 10:49:56 -070071 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & NO_OPERAND))
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 << GetTargetInstName(opcode) << " " << opcode << " "
73 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
74 << current_dalvik_offset_;
75 LIR* insn = RawLIR(current_dalvik_offset_, opcode);
76 AppendLIR(insn);
77 return insn;
78}
79
80inline LIR* Mir2Lir::NewLIR1(int opcode, int dest) {
buzbee409fe942013-10-11 10:49:56 -070081 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_UNARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 << GetTargetInstName(opcode) << " " << opcode << " "
83 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
84 << current_dalvik_offset_;
85 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest);
86 AppendLIR(insn);
87 return insn;
88}
89
90inline LIR* Mir2Lir::NewLIR2(int opcode, int dest, int src1) {
buzbee409fe942013-10-11 10:49:56 -070091 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 << GetTargetInstName(opcode) << " " << opcode << " "
93 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
94 << current_dalvik_offset_;
95 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1);
96 AppendLIR(insn);
97 return insn;
98}
99
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800100inline LIR* Mir2Lir::NewLIR2NoDest(int opcode, int src, int info) {
Haitao Fenga870bc52014-09-09 15:52:34 +0800101 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800102 << GetTargetInstName(opcode) << " " << opcode << " "
103 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
104 << current_dalvik_offset_;
105 LIR* insn = RawLIR(current_dalvik_offset_, opcode, src, info);
106 AppendLIR(insn);
107 return insn;
108}
109
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110inline LIR* Mir2Lir::NewLIR3(int opcode, int dest, int src1, int src2) {
buzbee409fe942013-10-11 10:49:56 -0700111 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 << GetTargetInstName(opcode) << " " << opcode << " "
113 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
114 << current_dalvik_offset_;
115 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2);
116 AppendLIR(insn);
117 return insn;
118}
119
120inline LIR* Mir2Lir::NewLIR4(int opcode, int dest, int src1, int src2, int info) {
buzbee409fe942013-10-11 10:49:56 -0700121 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUAD_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 << GetTargetInstName(opcode) << " " << opcode << " "
123 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
124 << current_dalvik_offset_;
125 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info);
126 AppendLIR(insn);
127 return insn;
128}
129
130inline LIR* Mir2Lir::NewLIR5(int opcode, int dest, int src1, int src2, int info1,
131 int info2) {
buzbee409fe942013-10-11 10:49:56 -0700132 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUIN_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 << GetTargetInstName(opcode) << " " << opcode << " "
134 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
135 << current_dalvik_offset_;
136 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info1, info2);
137 AppendLIR(insn);
138 return insn;
139}
140
141/*
142 * Mark the corresponding bit(s).
143 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100144inline void Mir2Lir::SetupRegMask(ResourceMask* mask, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700145 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100146 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
147 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
148 *mask = mask->Union(reginfo_map_[reg]->DefUseMask());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149}
150
151/*
Serban Constantinescu63999682014-07-15 17:44:21 +0100152 * Clear the corresponding bit(s).
153 */
154inline void Mir2Lir::ClearRegMask(ResourceMask* mask, int reg) {
155 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100156 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
157 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
158 *mask = mask->ClearBits(reginfo_map_[reg]->DefUseMask());
Serban Constantinescu63999682014-07-15 17:44:21 +0100159}
160
161/*
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 * Set up the proper fields in the resource mask
163 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100164inline void Mir2Lir::SetupResourceMasks(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 int opcode = lir->opcode;
166
buzbee409fe942013-10-11 10:49:56 -0700167 if (IsPseudoLirOp(opcode)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100168 lir->u.m.use_mask = lir->u.m.def_mask = &kEncodeNone;
buzbee409fe942013-10-11 10:49:56 -0700169 if (opcode != kPseudoBarrier) {
170 lir->flags.fixup = kFixupLabel;
171 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 return;
173 }
174
175 uint64_t flags = GetTargetInstFlags(opcode);
176
177 if (flags & NEEDS_FIXUP) {
buzbeeb48819d2013-09-14 16:15:25 -0700178 // Note: target-specific setup may specialize the fixup kind.
179 lir->flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 }
181
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100182 /* Get the starting size of the instruction's template. */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 lir->flags.size = GetInsnSize(lir);
buzbeeb48819d2013-09-14 16:15:25 -0700184 estimated_native_code_size_ += lir->flags.size;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100185
186 /* Set up the mask for resources. */
187 ResourceMask use_mask;
188 ResourceMask def_mask;
189
190 if (flags & (IS_LOAD | IS_STORE)) {
191 /* Set memory reference type (defaults to heap, overridden by ScopedMemRefType). */
192 if (flags & IS_LOAD) {
193 use_mask.SetBit(mem_ref_type_);
194 } else {
195 /* Currently only loads can be marked as kMustNotAlias. */
196 DCHECK(mem_ref_type_ != ResourceMask::kMustNotAlias);
197 }
198 if (flags & IS_STORE) {
199 /* Literals cannot be written to. */
200 DCHECK(mem_ref_type_ != ResourceMask::kLiteral);
201 def_mask.SetBit(mem_ref_type_);
202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 }
204
205 /*
206 * Conservatively assume the branch here will call out a function that in
207 * turn will trash everything.
208 */
209 if (flags & IS_BRANCH) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100210 lir->u.m.def_mask = lir->u.m.use_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 return;
212 }
213
214 if (flags & REG_DEF0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100215 SetupRegMask(&def_mask, lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 }
217
218 if (flags & REG_DEF1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100219 SetupRegMask(&def_mask, lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 }
221
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800222 if (flags & REG_DEF2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100223 SetupRegMask(&def_mask, lir->operands[2]);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800224 }
225
buzbeeb48819d2013-09-14 16:15:25 -0700226 if (flags & REG_USE0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100227 SetupRegMask(&use_mask, lir->operands[0]);
buzbeeb48819d2013-09-14 16:15:25 -0700228 }
229
230 if (flags & REG_USE1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100231 SetupRegMask(&use_mask, lir->operands[1]);
buzbeeb48819d2013-09-14 16:15:25 -0700232 }
233
234 if (flags & REG_USE2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100235 SetupRegMask(&use_mask, lir->operands[2]);
buzbeeb48819d2013-09-14 16:15:25 -0700236 }
237
238 if (flags & REG_USE3) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100239 SetupRegMask(&use_mask, lir->operands[3]);
buzbeeb48819d2013-09-14 16:15:25 -0700240 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241
buzbee17189ac2013-11-08 11:07:02 -0800242 if (flags & REG_USE4) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100243 SetupRegMask(&use_mask, lir->operands[4]);
buzbee17189ac2013-11-08 11:07:02 -0800244 }
245
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 if (flags & SETS_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100247 def_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 }
249
250 if (flags & USES_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100251 use_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253
254 // Handle target-specific actions
Junmo Parkaa839cc2014-08-30 20:13:02 +0900255 SetupTargetResourceMasks(lir, flags, &use_mask, &def_mask);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100256
257 lir->u.m.use_mask = mask_cache_.GetMask(use_mask);
258 lir->u.m.def_mask = mask_cache_.GetMask(def_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259}
260
buzbee091cc402014-03-31 10:14:40 -0700261inline art::Mir2Lir::RegisterInfo* Mir2Lir::GetRegInfo(RegStorage reg) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100262 RegisterInfo* res = reg.IsPair() ? reginfo_map_[reg.GetLowReg()] : reginfo_map_[reg.GetReg()];
buzbee091cc402014-03-31 10:14:40 -0700263 DCHECK(res != nullptr);
264 return res;
buzbeebd663de2013-09-10 15:41:31 -0700265}
266
Andreas Gampe4b537a82014-06-30 22:24:53 -0700267inline void Mir2Lir::CheckRegLocation(RegLocation rl) const {
268 if (kFailOnSizeError || kReportSizeError) {
269 CheckRegLocationImpl(rl, kFailOnSizeError, kReportSizeError);
270 }
271}
272
273inline void Mir2Lir::CheckRegStorage(RegStorage rs, WidenessCheck wide, RefCheck ref, FPCheck fp)
274 const {
275 if (kFailOnSizeError || kReportSizeError) {
276 CheckRegStorageImpl(rs, wide, ref, fp, kFailOnSizeError, kReportSizeError);
277 }
278}
279
Serguei Katkov717a3e42014-11-13 17:19:42 +0600280inline Mir2Lir::ShortyIterator::ShortyIterator(const char* shorty, bool is_static)
281 : cur_(shorty + 1), pending_this_(!is_static), initialized_(false) {
282 DCHECK(shorty != nullptr);
283 DCHECK_NE(*shorty, 0);
284}
285
286inline bool Mir2Lir::ShortyIterator::Next() {
287 if (!initialized_) {
288 initialized_ = true;
289 } else if (pending_this_) {
290 pending_this_ = false;
291 } else if (*cur_ != 0) {
292 cur_++;
293 }
294
295 return *cur_ != 0 || pending_this_;
296}
297
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298} // namespace art
299
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700300#endif // ART_COMPILER_DEX_QUICK_MIR_TO_LIR_INL_H_