blob: 7aef93e7c6198182914f26af114b6eddefa96a55 [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
17#include "dex/compiler_ir.h"
18#include "dex/compiler_internals.h"
19#include "dex/quick/mir_to_lir-inl.h"
20#include "invoke_type.h"
21
22namespace art {
23
24/* This file contains target-independent codegen and support. */
25
26/*
27 * Load an immediate value into a fixed or temp register. Target
28 * register is clobbered, and marked in_use.
29 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030LIR* Mir2Lir::LoadConstant(int r_dest, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070031 if (IsTemp(r_dest)) {
32 Clobber(r_dest);
33 MarkInUse(r_dest);
34 }
35 return LoadConstantNoClobber(r_dest, value);
36}
37
38/*
39 * Temporary workaround for Issue 7250540. If we're loading a constant zero into a
40 * promoted floating point register, also copy a zero into the int/ref identity of
41 * that sreg.
42 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070043void Mir2Lir::Workaround7250540(RegLocation rl_dest, int zero_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 if (rl_dest.fp) {
45 int pmap_index = SRegToPMap(rl_dest.s_reg_low);
46 if (promotion_map_[pmap_index].fp_location == kLocPhysReg) {
47 // Now, determine if this vreg is ever used as a reference. If not, we're done.
48 bool used_as_reference = false;
49 int base_vreg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
50 for (int i = 0; !used_as_reference && (i < mir_graph_->GetNumSSARegs()); i++) {
51 if (mir_graph_->SRegToVReg(mir_graph_->reg_location_[i].s_reg_low) == base_vreg) {
52 used_as_reference |= mir_graph_->reg_location_[i].ref;
53 }
54 }
55 if (!used_as_reference) {
56 return;
57 }
58 int temp_reg = zero_reg;
59 if (temp_reg == INVALID_REG) {
60 temp_reg = AllocTemp();
61 LoadConstant(temp_reg, 0);
62 }
63 if (promotion_map_[pmap_index].core_location == kLocPhysReg) {
64 // Promoted - just copy in a zero
65 OpRegCopy(promotion_map_[pmap_index].core_reg, temp_reg);
66 } else {
67 // Lives in the frame, need to store.
68 StoreBaseDisp(TargetReg(kSp), SRegOffset(rl_dest.s_reg_low), temp_reg, kWord);
69 }
70 if (zero_reg == INVALID_REG) {
71 FreeTemp(temp_reg);
72 }
73 }
74 }
75}
76
77/* Load a word at base + displacement. Displacement must be word multiple */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070078LIR* Mir2Lir::LoadWordDisp(int rBase, int displacement, int r_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 return LoadBaseDisp(rBase, displacement, r_dest, kWord,
80 INVALID_SREG);
81}
82
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070083LIR* Mir2Lir::StoreWordDisp(int rBase, int displacement, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 return StoreBaseDisp(rBase, displacement, r_src, kWord);
85}
86
87/*
88 * Load a Dalvik register into a physical register. Take care when
89 * using this routine, as it doesn't perform any bookkeeping regarding
90 * register liveness. That is the responsibility of the caller.
91 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070092void Mir2Lir::LoadValueDirect(RegLocation rl_src, int r_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 rl_src = UpdateLoc(rl_src);
94 if (rl_src.location == kLocPhysReg) {
95 OpRegCopy(r_dest, rl_src.low_reg);
96 } else if (IsInexpensiveConstant(rl_src)) {
97 LoadConstantNoClobber(r_dest, mir_graph_->ConstantValue(rl_src));
98 } else {
99 DCHECK((rl_src.location == kLocDalvikFrame) ||
100 (rl_src.location == kLocCompilerTemp));
101 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src.s_reg_low), r_dest);
102 }
103}
104
105/*
106 * Similar to LoadValueDirect, but clobbers and allocates the target
107 * register. Should be used when loading to a fixed register (for example,
108 * loading arguments to an out of line call.
109 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700110void Mir2Lir::LoadValueDirectFixed(RegLocation rl_src, int r_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 Clobber(r_dest);
112 MarkInUse(r_dest);
113 LoadValueDirect(rl_src, r_dest);
114}
115
116/*
117 * Load a Dalvik register pair into a physical register[s]. Take care when
118 * using this routine, as it doesn't perform any bookkeeping regarding
119 * register liveness. That is the responsibility of the caller.
120 */
121void Mir2Lir::LoadValueDirectWide(RegLocation rl_src, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700122 int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 rl_src = UpdateLocWide(rl_src);
124 if (rl_src.location == kLocPhysReg) {
125 OpRegCopyWide(reg_lo, reg_hi, rl_src.low_reg, rl_src.high_reg);
126 } else if (IsInexpensiveConstant(rl_src)) {
127 LoadConstantWide(reg_lo, reg_hi, mir_graph_->ConstantValueWide(rl_src));
128 } else {
129 DCHECK((rl_src.location == kLocDalvikFrame) ||
130 (rl_src.location == kLocCompilerTemp));
131 LoadBaseDispWide(TargetReg(kSp), SRegOffset(rl_src.s_reg_low),
132 reg_lo, reg_hi, INVALID_SREG);
133 }
134}
135
136/*
137 * Similar to LoadValueDirect, but clobbers and allocates the target
138 * registers. Should be used when loading to a fixed registers (for example,
139 * loading arguments to an out of line call.
140 */
141void Mir2Lir::LoadValueDirectWideFixed(RegLocation rl_src, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700142 int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 Clobber(reg_lo);
144 Clobber(reg_hi);
145 MarkInUse(reg_lo);
146 MarkInUse(reg_hi);
147 LoadValueDirectWide(rl_src, reg_lo, reg_hi);
148}
149
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700150RegLocation Mir2Lir::LoadValue(RegLocation rl_src, RegisterClass op_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 rl_src = EvalLoc(rl_src, op_kind, false);
152 if (IsInexpensiveConstant(rl_src) || rl_src.location != kLocPhysReg) {
153 LoadValueDirect(rl_src, rl_src.low_reg);
154 rl_src.location = kLocPhysReg;
155 MarkLive(rl_src.low_reg, rl_src.s_reg_low);
156 }
157 return rl_src;
158}
159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160void Mir2Lir::StoreValue(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 /*
162 * Sanity checking - should never try to store to the same
163 * ssa name during the compilation of a single instruction
164 * without an intervening ClobberSReg().
165 */
166 if (kIsDebugBuild) {
167 DCHECK((live_sreg_ == INVALID_SREG) ||
168 (rl_dest.s_reg_low != live_sreg_));
169 live_sreg_ = rl_dest.s_reg_low;
170 }
171 LIR* def_start;
172 LIR* def_end;
173 DCHECK(!rl_dest.wide);
174 DCHECK(!rl_src.wide);
175 rl_src = UpdateLoc(rl_src);
176 rl_dest = UpdateLoc(rl_dest);
177 if (rl_src.location == kLocPhysReg) {
178 if (IsLive(rl_src.low_reg) ||
179 IsPromoted(rl_src.low_reg) ||
180 (rl_dest.location == kLocPhysReg)) {
181 // Src is live/promoted or Dest has assigned reg.
182 rl_dest = EvalLoc(rl_dest, kAnyReg, false);
183 OpRegCopy(rl_dest.low_reg, rl_src.low_reg);
184 } else {
185 // Just re-assign the registers. Dest gets Src's regs
186 rl_dest.low_reg = rl_src.low_reg;
187 Clobber(rl_src.low_reg);
188 }
189 } else {
190 // Load Src either into promoted Dest or temps allocated for Dest
191 rl_dest = EvalLoc(rl_dest, kAnyReg, false);
192 LoadValueDirect(rl_src, rl_dest.low_reg);
193 }
194
195 // Dest is now live and dirty (until/if we flush it to home location)
196 MarkLive(rl_dest.low_reg, rl_dest.s_reg_low);
197 MarkDirty(rl_dest);
198
199
200 ResetDefLoc(rl_dest);
201 if (IsDirty(rl_dest.low_reg) &&
202 oat_live_out(rl_dest.s_reg_low)) {
203 def_start = last_lir_insn_;
204 StoreBaseDisp(TargetReg(kSp), SRegOffset(rl_dest.s_reg_low),
205 rl_dest.low_reg, kWord);
206 MarkClean(rl_dest);
207 def_end = last_lir_insn_;
208 if (!rl_dest.ref) {
209 // Exclude references from store elimination
210 MarkDef(rl_dest, def_start, def_end);
211 }
212 }
213}
214
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700215RegLocation Mir2Lir::LoadValueWide(RegLocation rl_src, RegisterClass op_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 DCHECK(rl_src.wide);
217 rl_src = EvalLoc(rl_src, op_kind, false);
218 if (IsInexpensiveConstant(rl_src) || rl_src.location != kLocPhysReg) {
219 LoadValueDirectWide(rl_src, rl_src.low_reg, rl_src.high_reg);
220 rl_src.location = kLocPhysReg;
221 MarkLive(rl_src.low_reg, rl_src.s_reg_low);
222 MarkLive(rl_src.high_reg, GetSRegHi(rl_src.s_reg_low));
223 }
224 return rl_src;
225}
226
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700227void Mir2Lir::StoreValueWide(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 /*
229 * Sanity checking - should never try to store to the same
230 * ssa name during the compilation of a single instruction
231 * without an intervening ClobberSReg().
232 */
233 if (kIsDebugBuild) {
234 DCHECK((live_sreg_ == INVALID_SREG) ||
235 (rl_dest.s_reg_low != live_sreg_));
236 live_sreg_ = rl_dest.s_reg_low;
237 }
238 LIR* def_start;
239 LIR* def_end;
240 DCHECK_EQ(IsFpReg(rl_src.low_reg), IsFpReg(rl_src.high_reg));
241 DCHECK(rl_dest.wide);
242 DCHECK(rl_src.wide);
Alexei Zavjalovc17ebe82014-02-26 10:38:23 +0700243 rl_src = UpdateLocWide(rl_src);
244 rl_dest = UpdateLocWide(rl_dest);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 if (rl_src.location == kLocPhysReg) {
246 if (IsLive(rl_src.low_reg) ||
247 IsLive(rl_src.high_reg) ||
248 IsPromoted(rl_src.low_reg) ||
249 IsPromoted(rl_src.high_reg) ||
250 (rl_dest.location == kLocPhysReg)) {
251 // Src is live or promoted or Dest has assigned reg.
252 rl_dest = EvalLoc(rl_dest, kAnyReg, false);
253 OpRegCopyWide(rl_dest.low_reg, rl_dest.high_reg,
254 rl_src.low_reg, rl_src.high_reg);
255 } else {
256 // Just re-assign the registers. Dest gets Src's regs
257 rl_dest.low_reg = rl_src.low_reg;
258 rl_dest.high_reg = rl_src.high_reg;
259 Clobber(rl_src.low_reg);
260 Clobber(rl_src.high_reg);
261 }
262 } else {
263 // Load Src either into promoted Dest or temps allocated for Dest
264 rl_dest = EvalLoc(rl_dest, kAnyReg, false);
265 LoadValueDirectWide(rl_src, rl_dest.low_reg, rl_dest.high_reg);
266 }
267
268 // Dest is now live and dirty (until/if we flush it to home location)
269 MarkLive(rl_dest.low_reg, rl_dest.s_reg_low);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000270
271 // Does this wide value live in two registers (or one vector one)?
272 if (rl_dest.low_reg != rl_dest.high_reg) {
273 MarkLive(rl_dest.high_reg, GetSRegHi(rl_dest.s_reg_low));
274 MarkDirty(rl_dest);
275 MarkPair(rl_dest.low_reg, rl_dest.high_reg);
276 } else {
277 // This must be an x86 vector register value,
278 DCHECK(IsFpReg(rl_dest.low_reg) && (cu_->instruction_set == kX86));
279 MarkDirty(rl_dest);
280 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281
282
283 ResetDefLocWide(rl_dest);
284 if ((IsDirty(rl_dest.low_reg) ||
285 IsDirty(rl_dest.high_reg)) &&
286 (oat_live_out(rl_dest.s_reg_low) ||
287 oat_live_out(GetSRegHi(rl_dest.s_reg_low)))) {
288 def_start = last_lir_insn_;
289 DCHECK_EQ((mir_graph_->SRegToVReg(rl_dest.s_reg_low)+1),
290 mir_graph_->SRegToVReg(GetSRegHi(rl_dest.s_reg_low)));
291 StoreBaseDispWide(TargetReg(kSp), SRegOffset(rl_dest.s_reg_low),
292 rl_dest.low_reg, rl_dest.high_reg);
293 MarkClean(rl_dest);
294 def_end = last_lir_insn_;
295 MarkDefWide(rl_dest, def_start, def_end);
296 }
297}
298
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800299void Mir2Lir::StoreFinalValue(RegLocation rl_dest, RegLocation rl_src) {
300 DCHECK_EQ(rl_src.location, kLocPhysReg);
301
302 if (rl_dest.location == kLocPhysReg) {
303 OpRegCopy(rl_dest.low_reg, rl_src.low_reg);
304 } else {
305 // Just re-assign the register. Dest gets Src's reg.
306 rl_dest.low_reg = rl_src.low_reg;
307 rl_dest.location = kLocPhysReg;
308 Clobber(rl_src.low_reg);
309 }
310
311 // Dest is now live and dirty (until/if we flush it to home location)
312 MarkLive(rl_dest.low_reg, rl_dest.s_reg_low);
313 MarkDirty(rl_dest);
314
315
316 ResetDefLoc(rl_dest);
317 if (IsDirty(rl_dest.low_reg) &&
318 oat_live_out(rl_dest.s_reg_low)) {
319 LIR *def_start = last_lir_insn_;
320 StoreBaseDisp(TargetReg(kSp), SRegOffset(rl_dest.s_reg_low),
321 rl_dest.low_reg, kWord);
322 MarkClean(rl_dest);
323 LIR *def_end = last_lir_insn_;
324 if (!rl_dest.ref) {
325 // Exclude references from store elimination
326 MarkDef(rl_dest, def_start, def_end);
327 }
328 }
329}
330
Mark Mendelle02d48f2014-01-15 11:19:23 -0800331void Mir2Lir::StoreFinalValueWide(RegLocation rl_dest, RegLocation rl_src) {
332 DCHECK_EQ(IsFpReg(rl_src.low_reg), IsFpReg(rl_src.high_reg));
333 DCHECK(rl_dest.wide);
334 DCHECK(rl_src.wide);
335 DCHECK_EQ(rl_src.location, kLocPhysReg);
336
337 if (rl_dest.location == kLocPhysReg) {
338 OpRegCopyWide(rl_dest.low_reg, rl_dest.high_reg, rl_src.low_reg, rl_src.high_reg);
339 } else {
340 // Just re-assign the registers. Dest gets Src's regs.
341 rl_dest.low_reg = rl_src.low_reg;
342 rl_dest.high_reg = rl_src.high_reg;
343 rl_dest.location = kLocPhysReg;
344 Clobber(rl_src.low_reg);
345 Clobber(rl_src.high_reg);
346 }
347
348 // Dest is now live and dirty (until/if we flush it to home location).
349 MarkLive(rl_dest.low_reg, rl_dest.s_reg_low);
350
351 // Does this wide value live in two registers (or one vector one)?
352 if (rl_dest.low_reg != rl_dest.high_reg) {
353 MarkLive(rl_dest.high_reg, GetSRegHi(rl_dest.s_reg_low));
354 MarkDirty(rl_dest);
355 MarkPair(rl_dest.low_reg, rl_dest.high_reg);
356 } else {
357 // This must be an x86 vector register value,
358 DCHECK(IsFpReg(rl_dest.low_reg) && (cu_->instruction_set == kX86));
359 MarkDirty(rl_dest);
360 }
361
362 ResetDefLocWide(rl_dest);
363 if ((IsDirty(rl_dest.low_reg) ||
364 IsDirty(rl_dest.high_reg)) &&
365 (oat_live_out(rl_dest.s_reg_low) ||
366 oat_live_out(GetSRegHi(rl_dest.s_reg_low)))) {
367 LIR *def_start = last_lir_insn_;
368 DCHECK_EQ((mir_graph_->SRegToVReg(rl_dest.s_reg_low)+1),
369 mir_graph_->SRegToVReg(GetSRegHi(rl_dest.s_reg_low)));
370 StoreBaseDispWide(TargetReg(kSp), SRegOffset(rl_dest.s_reg_low),
371 rl_dest.low_reg, rl_dest.high_reg);
372 MarkClean(rl_dest);
373 LIR *def_end = last_lir_insn_;
374 MarkDefWide(rl_dest, def_start, def_end);
375 }
376}
377
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378/* Utilities to load the current Method* */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700379void Mir2Lir::LoadCurrMethodDirect(int r_tgt) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 LoadValueDirectFixed(mir_graph_->GetMethodLoc(), r_tgt);
381}
382
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700383RegLocation Mir2Lir::LoadCurrMethod() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 return LoadValue(mir_graph_->GetMethodLoc(), kCoreReg);
385}
386
Mark Mendelle02d48f2014-01-15 11:19:23 -0800387RegLocation Mir2Lir::ForceTemp(RegLocation loc) {
388 DCHECK(!loc.wide);
389 DCHECK(loc.location == kLocPhysReg);
390 DCHECK(!IsFpReg(loc.low_reg));
391 DCHECK(!IsFpReg(loc.high_reg));
392 if (IsTemp(loc.low_reg)) {
393 Clobber(loc.low_reg);
394 } else {
395 int temp_low = AllocTemp();
396 OpRegCopy(temp_low, loc.low_reg);
397 loc.low_reg = temp_low;
398 }
399
400 // Ensure that this doesn't represent the original SR any more.
401 loc.s_reg_low = INVALID_SREG;
402 return loc;
403}
404
405RegLocation Mir2Lir::ForceTempWide(RegLocation loc) {
406 DCHECK(loc.wide);
407 DCHECK(loc.location == kLocPhysReg);
408 DCHECK(!IsFpReg(loc.low_reg));
409 DCHECK(!IsFpReg(loc.high_reg));
410 if (IsTemp(loc.low_reg)) {
411 Clobber(loc.low_reg);
412 } else {
413 int temp_low = AllocTemp();
414 OpRegCopy(temp_low, loc.low_reg);
415 loc.low_reg = temp_low;
416 }
417 if (IsTemp(loc.high_reg)) {
418 Clobber(loc.high_reg);
419 } else {
420 int temp_high = AllocTemp();
421 OpRegCopy(temp_high, loc.high_reg);
422 loc.high_reg = temp_high;
423 }
424
425 // Ensure that this doesn't represent the original SR any more.
426 loc.s_reg_low = INVALID_SREG;
427 return loc;
428}
429
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430} // namespace art