blob: 144790e9d7afb64c9cfddcd42a32f4020b3c4793 [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
22#include "dex/compiler_internals.h"
23
24namespace art {
25
26/* Mark a temp register as dead. Does not affect allocation state. */
27inline void Mir2Lir::ClobberBody(RegisterInfo* p) {
buzbeeba574512014-05-12 15:13:16 -070028 DCHECK(p->IsTemp());
buzbee082833c2014-05-17 23:16:26 -070029 if (p->SReg() != INVALID_SREG) {
buzbee091cc402014-03-31 10:14:40 -070030 DCHECK(!(p->IsLive() && p->IsDirty())) << "Live & dirty temp in clobber";
buzbee30adc732014-05-09 15:10:18 -070031 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070032 if (p->IsWide()) {
33 p->SetIsWide(false);
buzbeeb5860fb2014-06-21 15:31:01 -070034 if (p->GetReg().NotExactlyEquals(p->Partner())) {
buzbee091cc402014-03-31 10:14:40 -070035 // Register pair - deal with the other half.
36 p = GetRegInfo(p->Partner());
37 p->SetIsWide(false);
buzbee30adc732014-05-09 15:10:18 -070038 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -070039 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 }
41 }
42}
43
buzbee0d829482013-10-11 15:24:55 -070044inline LIR* Mir2Lir::RawLIR(DexOffset dalvik_offset, int opcode, int op0,
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 int op1, int op2, int op3, int op4, LIR* target) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000046 LIR* insn = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 insn->dalvik_offset = dalvik_offset;
48 insn->opcode = opcode;
49 insn->operands[0] = op0;
50 insn->operands[1] = op1;
51 insn->operands[2] = op2;
52 insn->operands[3] = op3;
53 insn->operands[4] = op4;
54 insn->target = target;
55 SetupResourceMasks(insn);
56 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
57 (opcode == kPseudoExportedPC)) {
58 // Always make labels scheduling barriers
buzbeeb48819d2013-09-14 16:15:25 -070059 DCHECK(!insn->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010060 insn->u.m.use_mask = insn->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 }
62 return insn;
63}
64
65/*
66 * The following are building blocks to construct low-level IRs with 0 - 4
67 * operands.
68 */
69inline LIR* Mir2Lir::NewLIR0(int opcode) {
buzbee409fe942013-10-11 10:49:56 -070070 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & NO_OPERAND))
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 << GetTargetInstName(opcode) << " " << opcode << " "
72 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
73 << current_dalvik_offset_;
74 LIR* insn = RawLIR(current_dalvik_offset_, opcode);
75 AppendLIR(insn);
76 return insn;
77}
78
79inline LIR* Mir2Lir::NewLIR1(int opcode, int dest) {
buzbee409fe942013-10-11 10:49:56 -070080 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_UNARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 << GetTargetInstName(opcode) << " " << opcode << " "
82 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
83 << current_dalvik_offset_;
84 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest);
85 AppendLIR(insn);
86 return insn;
87}
88
89inline LIR* Mir2Lir::NewLIR2(int opcode, int dest, int src1) {
buzbee409fe942013-10-11 10:49:56 -070090 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 << GetTargetInstName(opcode) << " " << opcode << " "
92 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
93 << current_dalvik_offset_;
94 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1);
95 AppendLIR(insn);
96 return insn;
97}
98
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -080099inline LIR* Mir2Lir::NewLIR2NoDest(int opcode, int src, int info) {
Haitao Fenga870bc52014-09-09 15:52:34 +0800100 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800101 << GetTargetInstName(opcode) << " " << opcode << " "
102 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
103 << current_dalvik_offset_;
104 LIR* insn = RawLIR(current_dalvik_offset_, opcode, src, info);
105 AppendLIR(insn);
106 return insn;
107}
108
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109inline LIR* Mir2Lir::NewLIR3(int opcode, int dest, int src1, int src2) {
buzbee409fe942013-10-11 10:49:56 -0700110 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 << GetTargetInstName(opcode) << " " << opcode << " "
112 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
113 << current_dalvik_offset_;
114 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2);
115 AppendLIR(insn);
116 return insn;
117}
118
119inline LIR* Mir2Lir::NewLIR4(int opcode, int dest, int src1, int src2, int info) {
buzbee409fe942013-10-11 10:49:56 -0700120 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUAD_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 << GetTargetInstName(opcode) << " " << opcode << " "
122 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
123 << current_dalvik_offset_;
124 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info);
125 AppendLIR(insn);
126 return insn;
127}
128
129inline LIR* Mir2Lir::NewLIR5(int opcode, int dest, int src1, int src2, int info1,
130 int info2) {
buzbee409fe942013-10-11 10:49:56 -0700131 DCHECK(IsPseudoLirOp(opcode) || (GetTargetInstFlags(opcode) & IS_QUIN_OP))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 << GetTargetInstName(opcode) << " " << opcode << " "
133 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
134 << current_dalvik_offset_;
135 LIR* insn = RawLIR(current_dalvik_offset_, opcode, dest, src1, src2, info1, info2);
136 AppendLIR(insn);
137 return insn;
138}
139
140/*
141 * Mark the corresponding bit(s).
142 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100143inline void Mir2Lir::SetupRegMask(ResourceMask* mask, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700144 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100145 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
146 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
147 *mask = mask->Union(reginfo_map_[reg]->DefUseMask());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148}
149
150/*
Serban Constantinescu63999682014-07-15 17:44:21 +0100151 * Clear the corresponding bit(s).
152 */
153inline void Mir2Lir::ClearRegMask(ResourceMask* mask, int reg) {
154 DCHECK_EQ((reg & ~RegStorage::kRegValMask), 0);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100155 DCHECK_LT(static_cast<size_t>(reg), reginfo_map_.size());
156 DCHECK(reginfo_map_[reg] != nullptr) << "No info for 0x" << reg;
157 *mask = mask->ClearBits(reginfo_map_[reg]->DefUseMask());
Serban Constantinescu63999682014-07-15 17:44:21 +0100158}
159
160/*
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 * Set up the proper fields in the resource mask
162 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100163inline void Mir2Lir::SetupResourceMasks(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 int opcode = lir->opcode;
165
buzbee409fe942013-10-11 10:49:56 -0700166 if (IsPseudoLirOp(opcode)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100167 lir->u.m.use_mask = lir->u.m.def_mask = &kEncodeNone;
buzbee409fe942013-10-11 10:49:56 -0700168 if (opcode != kPseudoBarrier) {
169 lir->flags.fixup = kFixupLabel;
170 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 return;
172 }
173
174 uint64_t flags = GetTargetInstFlags(opcode);
175
176 if (flags & NEEDS_FIXUP) {
buzbeeb48819d2013-09-14 16:15:25 -0700177 // Note: target-specific setup may specialize the fixup kind.
178 lir->flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 }
180
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100181 /* Get the starting size of the instruction's template. */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 lir->flags.size = GetInsnSize(lir);
buzbeeb48819d2013-09-14 16:15:25 -0700183 estimated_native_code_size_ += lir->flags.size;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100184
185 /* Set up the mask for resources. */
186 ResourceMask use_mask;
187 ResourceMask def_mask;
188
189 if (flags & (IS_LOAD | IS_STORE)) {
190 /* Set memory reference type (defaults to heap, overridden by ScopedMemRefType). */
191 if (flags & IS_LOAD) {
192 use_mask.SetBit(mem_ref_type_);
193 } else {
194 /* Currently only loads can be marked as kMustNotAlias. */
195 DCHECK(mem_ref_type_ != ResourceMask::kMustNotAlias);
196 }
197 if (flags & IS_STORE) {
198 /* Literals cannot be written to. */
199 DCHECK(mem_ref_type_ != ResourceMask::kLiteral);
200 def_mask.SetBit(mem_ref_type_);
201 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 }
203
204 /*
205 * Conservatively assume the branch here will call out a function that in
206 * turn will trash everything.
207 */
208 if (flags & IS_BRANCH) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100209 lir->u.m.def_mask = lir->u.m.use_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 return;
211 }
212
213 if (flags & REG_DEF0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100214 SetupRegMask(&def_mask, lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 }
216
217 if (flags & REG_DEF1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100218 SetupRegMask(&def_mask, lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 }
220
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800221 if (flags & REG_DEF2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100222 SetupRegMask(&def_mask, lir->operands[2]);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800223 }
224
buzbeeb48819d2013-09-14 16:15:25 -0700225 if (flags & REG_USE0) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100226 SetupRegMask(&use_mask, lir->operands[0]);
buzbeeb48819d2013-09-14 16:15:25 -0700227 }
228
229 if (flags & REG_USE1) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100230 SetupRegMask(&use_mask, lir->operands[1]);
buzbeeb48819d2013-09-14 16:15:25 -0700231 }
232
233 if (flags & REG_USE2) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100234 SetupRegMask(&use_mask, lir->operands[2]);
buzbeeb48819d2013-09-14 16:15:25 -0700235 }
236
237 if (flags & REG_USE3) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100238 SetupRegMask(&use_mask, lir->operands[3]);
buzbeeb48819d2013-09-14 16:15:25 -0700239 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240
buzbee17189ac2013-11-08 11:07:02 -0800241 if (flags & REG_USE4) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100242 SetupRegMask(&use_mask, lir->operands[4]);
buzbee17189ac2013-11-08 11:07:02 -0800243 }
244
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 if (flags & SETS_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100246 def_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 }
248
249 if (flags & USES_CCODES) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100250 use_mask.SetBit(ResourceMask::kCCode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 }
252
253 // Handle target-specific actions
Junmo Parkaa839cc2014-08-30 20:13:02 +0900254 SetupTargetResourceMasks(lir, flags, &use_mask, &def_mask);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100255
256 lir->u.m.use_mask = mask_cache_.GetMask(use_mask);
257 lir->u.m.def_mask = mask_cache_.GetMask(def_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258}
259
buzbee091cc402014-03-31 10:14:40 -0700260inline art::Mir2Lir::RegisterInfo* Mir2Lir::GetRegInfo(RegStorage reg) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100261 RegisterInfo* res = reg.IsPair() ? reginfo_map_[reg.GetLowReg()] : reginfo_map_[reg.GetReg()];
buzbee091cc402014-03-31 10:14:40 -0700262 DCHECK(res != nullptr);
263 return res;
buzbeebd663de2013-09-10 15:41:31 -0700264}
265
Andreas Gampe4b537a82014-06-30 22:24:53 -0700266inline void Mir2Lir::CheckRegLocation(RegLocation rl) const {
267 if (kFailOnSizeError || kReportSizeError) {
268 CheckRegLocationImpl(rl, kFailOnSizeError, kReportSizeError);
269 }
270}
271
272inline void Mir2Lir::CheckRegStorage(RegStorage rs, WidenessCheck wide, RefCheck ref, FPCheck fp)
273 const {
274 if (kFailOnSizeError || kReportSizeError) {
275 CheckRegStorageImpl(rs, wide, ref, fp, kFailOnSizeError, kReportSizeError);
276 }
277}
278
Serguei Katkov717a3e42014-11-13 17:19:42 +0600279inline Mir2Lir::ShortyIterator::ShortyIterator(const char* shorty, bool is_static)
280 : cur_(shorty + 1), pending_this_(!is_static), initialized_(false) {
281 DCHECK(shorty != nullptr);
282 DCHECK_NE(*shorty, 0);
283}
284
285inline bool Mir2Lir::ShortyIterator::Next() {
286 if (!initialized_) {
287 initialized_ = true;
288 } else if (pending_this_) {
289 pending_this_ = false;
290 } else if (*cur_ != 0) {
291 cur_++;
292 }
293
294 return *cur_ != 0 || pending_this_;
295}
296
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297} // namespace art
298
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700299#endif // ART_COMPILER_DEX_QUICK_MIR_TO_LIR_INL_H_