Skip to content

Commit 658fb89

Browse files
rth7680Michael Tokarev
authored andcommitted
tcg/loongarch64: Fix tcg_out_movi vs some pcrel pointers
Simplify the logic for two-part, 32-bit pc-relative addresses. Rather than assume all such fit in int32_t, do some arithmetic and assert a result, do some arithmetic first and then check to see if the pieces are in range. Cc: [email protected] Fixes: dacc517 ("tcg/loongarch64: Implement tcg_out_mov and tcg_out_movi") Reviewed-by: Song Gao <[email protected]> Reported-by: Song Gao <[email protected]> Signed-off-by: Richard Henderson <[email protected]> (cherry picked from commit 521d7fb) Signed-off-by: Michael Tokarev <[email protected]>
1 parent c8fdbb5 commit 658fb89

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

tcg/loongarch64/tcg-target.c.inc

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
366366
* back to the slow path.
367367
*/
368368

369-
intptr_t pc_offset;
370-
tcg_target_long val_lo, val_hi, pc_hi, offset_hi;
369+
intptr_t src_rx, pc_offset;
371370
tcg_target_long hi12, hi32, hi52;
372371

373372
/* Value fits in signed i32. */
@@ -377,24 +376,23 @@ static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
377376
}
378377

379378
/* PC-relative cases. */
380-
pc_offset = tcg_pcrel_diff(s, (void *)val);
381-
if (pc_offset == sextreg(pc_offset, 0, 22) && (pc_offset & 3) == 0) {
382-
/* Single pcaddu2i. */
383-
tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2);
384-
return;
379+
src_rx = (intptr_t)tcg_splitwx_to_rx(s->code_ptr);
380+
if ((val & 3) == 0) {
381+
pc_offset = val - src_rx;
382+
if (pc_offset == sextreg(pc_offset, 0, 22)) {
383+
/* Single pcaddu2i. */
384+
tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2);
385+
return;
386+
}
385387
}
386388

387-
if (pc_offset == (int32_t)pc_offset) {
388-
/* Offset within 32 bits; load with pcalau12i + ori. */
389-
val_lo = sextreg(val, 0, 12);
390-
val_hi = val >> 12;
391-
pc_hi = (val - pc_offset) >> 12;
392-
offset_hi = val_hi - pc_hi;
393-
394-
tcg_debug_assert(offset_hi == sextreg(offset_hi, 0, 20));
395-
tcg_out_opc_pcalau12i(s, rd, offset_hi);
389+
pc_offset = (val >> 12) - (src_rx >> 12);
390+
if (pc_offset == sextreg(pc_offset, 0, 20)) {
391+
/* Load with pcalau12i + ori. */
392+
tcg_target_long val_lo = val & 0xfff;
393+
tcg_out_opc_pcalau12i(s, rd, pc_offset);
396394
if (val_lo != 0) {
397-
tcg_out_opc_ori(s, rd, rd, val_lo & 0xfff);
395+
tcg_out_opc_ori(s, rd, rd, val_lo);
398396
}
399397
return;
400398
}

0 commit comments

Comments
 (0)