Skip to content

Commit 1fbc6f3

Browse files
committed
Refactor
1 parent 4679573 commit 1fbc6f3

File tree

9 files changed

+35
-31
lines changed

9 files changed

+35
-31
lines changed

lib/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ inline i64 sign_extend(u64 val, i64 n) {
378378
return (i64)(val << (64 - n)) >> (64 - n);
379379
}
380380

381+
inline bool is_int(u64 val, i64 n) {
382+
return sign_extend(val, n) == val;
383+
}
384+
381385
template <typename T, typename Compare = std::less<T>>
382386
void update_minimum(std::atomic<T> &atomic, u64 new_val, Compare cmp = {}) {
383387
T old_val = atomic.load(std::memory_order_relaxed);

src/arch-arm32.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
304304
i64 val1 = S + A - P;
305305
i64 val2 = align_to(S + A - P, 4);
306306

307-
if (T && sign_extend(val1, 25) == val1) {
307+
if (T && is_int(val1, 25)) {
308308
*(ul16 *)(loc + 2) |= 0x1000; // BL
309309
write_thm_b_imm(loc, val1);
310-
} else if (!T && sign_extend(val2, 25) == val2) {
310+
} else if (!T && is_int(val2, 25)) {
311311
*(ul16 *)(loc + 2) &= ~0x1000; // BLX
312312
write_thm_b_imm(loc, val2);
313313
} else {
@@ -343,7 +343,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
343343
Fatal(ctx) << *this << ": R_ARM_CALL refers to neither BL nor BLX";
344344

345345
i64 val = S + A - P;
346-
if (sign_extend(val, 26) == val) {
346+
if (is_int(val, 26)) {
347347
if (T) {
348348
*(ul32 *)loc = 0xfa00'0000; // BLX
349349
*(ul32 *)loc |= (bit(val, 1) << 24) | bits(val, 25, 2);
@@ -370,7 +370,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
370370
// required, we jump to a linker-synthesized thunk which does the
371371
// job with a longer code sequence.
372372
i64 val = S + A - P;
373-
if (sign_extend(val, 26) != val || T)
373+
if (T || !is_int(val, 26) )
374374
val = get_arm_thunk_addr() + A - P;
375375
*(ul32 *)loc = (*(ul32 *)loc & 0xff00'0000) | bits(val, 25, 2);
376376
break;
@@ -416,7 +416,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
416416
// Just like R_ARM_JUMP24, we need to jump to a thunk if we need to
417417
// switch processor mode.
418418
i64 val = S + A - P;
419-
if (sign_extend(val, 25) != val || !T)
419+
if (!T || !is_int(val, 25))
420420
val = get_thumb_thunk_addr() + A - P;
421421
write_thm_b_imm(loc, val);
422422
break;

src/arch-arm64.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
238238
i + 1 < rels.size()) {
239239
i64 val = S + A - P - 4;
240240
const ElfRel<E> &rel2 = rels[i + 1];
241-
if (sign_extend(val, 21) == val &&
241+
if (is_int(val, 21) &&
242242
rel2.r_type == R_AARCH64_ADD_ABS_LO12_NC &&
243243
rel2.r_sym == rel.r_sym &&
244244
rel2.r_offset == rel.r_offset + 4 &&

src/arch-loongarch.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
600600
case R_LARCH_TLS_DESC_PC_LO12:
601601
if (sym.has_tlsdesc(ctx) && removed_bytes == 0) {
602602
i64 dist = sym.get_tlsdesc_addr(ctx) + A - P;
603-
if (sign_extend(dist, 22) == dist) {
603+
if (is_int(dist, 22)) {
604604
// If we can directly materialize the PC-relative address
605605
// with pcaddi, do that.
606606
*(ul32 *)loc = 0x1800'0000 | get_rd(*(ul32 *)loc); // pcaddi
@@ -649,7 +649,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
649649

650650
// Rewrite `addi.d $t0, $t0, <offset>` with `addi.d $t0, $tp, <offset>`
651651
// if the offset is directly accessible using tp. tp is r2.
652-
if (sign_extend(val, 12) == val)
652+
if (is_int(val, 12))
653653
set_rj(loc, 2);
654654
break;
655655
}
@@ -931,7 +931,7 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
931931
//
932932
// addi.d $t0, $tp, <tp-offset>
933933
if (i64 val = sym.get_addr(ctx) + r.r_addend - ctx.tp_addr;
934-
sign_extend(val, 12) == val)
934+
is_int(val, 12))
935935
remove(4);
936936
break;
937937
case R_LARCH_PCALA_HI20:
@@ -954,7 +954,7 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
954954
u32 insn2 = *(ul32 *)(buf + rels[i].r_offset + 4);
955955
bool is_addi_d = (insn2 & 0xffc0'0000) == 0x02c0'0000;
956956

957-
if ((dist & 0b11) == 0 && sign_extend(dist, 22) == dist &&
957+
if ((dist & 0b11) == 0 && is_int(dist, 22) &&
958958
is_addi_d && get_rd(insn1) == get_rd(insn2) &&
959959
get_rd(insn2) == get_rj(insn2))
960960
remove(4);
@@ -970,7 +970,7 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
970970
// If the displacement is PC ± 128 MiB, we can use B or BL instead.
971971
// Note that $zero is $r0 and $ra is $r1.
972972
if (i64 dist = compute_distance(ctx, sym, isec, r);
973-
sign_extend(dist, 28) == dist)
973+
is_int(dist, 28))
974974
if (u32 jirl = *(ul32 *)(buf + rels[i].r_offset + 4);
975975
get_rd(jirl) == 0 || get_rd(jirl) == 1)
976976
remove(4);
@@ -988,15 +988,15 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
988988
// pcaddi $t0, <offset>
989989
if (is_relaxable_got_load(ctx, isec, i)) {
990990
i64 dist = compute_distance(ctx, sym, isec, r);
991-
if ((dist & 0b11) == 0 && sign_extend(dist, 22) == dist)
991+
if ((dist & 0b11) == 0 && is_int(dist, 22))
992992
remove(4);
993993
}
994994
break;
995995
case R_LARCH_TLS_DESC_PC_HI20:
996996
if (sym.has_tlsdesc(ctx)) {
997997
u64 P = isec.get_addr() + r.r_offset;
998998
i64 dist = sym.get_tlsdesc_addr(ctx) + r.r_addend - P;
999-
if (sign_extend(dist, 22) == dist)
999+
if (is_int(dist, 22))
10001000
remove(4);
10011001
} else {
10021002
remove(4);

src/arch-ppc32.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
213213
case R_PPC_REL24:
214214
case R_PPC_LOCAL24PC: {
215215
i64 val = S + A - P;
216-
if (sign_extend(val, 26) != val)
216+
if (!is_int(val, 26))
217217
val = sym.get_thunk_addr(ctx, P) - P;
218218
*(ub32 *)loc |= bits(val, 25, 2) << 2;
219219
break;
220220
}
221221
case R_PPC_PLTREL24: {
222222
i64 val = S - P;
223-
if (sym.has_plt(ctx) || sign_extend(val, 26) != val)
223+
if (sym.has_plt(ctx) || !is_int(val, 26))
224224
val = sym.get_thunk_addr(ctx, P) - P;
225225
*(ub32 *)loc |= bits(val, 25, 2) << 2;
226226
break;

src/arch-ppc64v1.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
194194
break;
195195
case R_PPC64_REL24: {
196196
i64 val = sym.get_addr(ctx, NO_OPD) + A - P;
197-
if (sym.has_plt(ctx) || sign_extend(val, 26) != val)
197+
if (sym.has_plt(ctx) || !is_int(val, 26))
198198
val = sym.get_thunk_addr(ctx, P) + A - P;
199199

200200
check(val, -(1 << 25), 1 << 25);

src/arch-ppc64v2.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
227227
*(ul32 *)(loc + 4) = 0xe841'0018; // ld r2, 24(r1)
228228
} else {
229229
i64 val = S + get_local_entry_offset(ctx, sym) + A - P;
230-
if (sign_extend(val, 26) != val)
230+
if (!is_int(val, 26))
231231
val = no_r2save_thunk_addr() + A - P;
232232
*(ul32 *)loc |= bits(val, 25, 2) << 2;
233233
}
@@ -238,7 +238,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
238238
*(ul32 *)loc |= bits(val, 25, 2) << 2;
239239
} else {
240240
i64 val = S + A - P;
241-
if (sign_extend(val, 26) != val)
241+
if (!is_int(val, 26))
242242
val = no_r2save_thunk_addr() + A - P;
243243
*(ul32 *)loc |= bits(val, 25, 2) << 2;
244244
}

src/arch-riscv.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
433433
// Rewrite `lw t1, 0(t0)` with `lw t1, 0(x0)` if the address is
434434
// accessible relative to the zero register because if that's the
435435
// case, corresponding LUI might have been removed by relaxation.
436-
if (sign_extend(S + A, 12) == S + A)
436+
if (is_int(S + A, 12))
437437
set_rs1(loc, 0);
438438
break;
439439
case R_RISCV_TPREL_HI20:
@@ -457,7 +457,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
457457

458458
// Rewrite `lw t1, 0(t0)` with `lw t1, 0(tp)` if the address is
459459
// directly accessible using tp. tp is x4.
460-
if (sign_extend(val, 12) == val)
460+
if (is_int(val, 12))
461461
set_rs1(loc, 4);
462462
break;
463463
}
@@ -540,7 +540,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
540540
write_itype(loc, sym2.get_gottp_addr(ctx) + A - P);
541541
} else {
542542
i64 val = S + A - ctx.tp_addr;
543-
if (sign_extend(val, 12) == val)
543+
if (is_int(val, 12))
544544
*(ul32 *)loc = 0x513; // addi a0,zero,<lo12>
545545
else
546546
*(ul32 *)loc = 0x50513; // addi a0,a0,<lo12>
@@ -888,15 +888,15 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
888888

889889
i64 rd = get_rd(buf + r.r_offset + 4);
890890

891-
if (use_rvc && rd == 0 && sign_extend(dist, 12) == dist) {
891+
if (use_rvc && rd == 0 && is_int(dist, 12)) {
892892
// If rd is x0 and the jump target is within ±2 KiB, we can use
893893
// C.J, saving 6 bytes.
894894
remove(6);
895-
} else if (use_rvc && !E::is_64 && rd == 1 && sign_extend(dist, 12) == dist) {
895+
} else if (use_rvc && !E::is_64 && rd == 1 && is_int(dist, 12)) {
896896
// If rd is x1 and the jump target is within ±2 KiB, we can use
897897
// C.JAL. This is RV32 only because C.JAL is RV32-only instruction.
898898
remove(6);
899-
} else if (sign_extend(dist, 21) == dist) {
899+
} else if (is_int(dist, 21)) {
900900
// If the jump target is within ±1 MiB, we can use JAL.
901901
remove(4);
902902
}
@@ -919,10 +919,10 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
919919
if (rd == get_rd(buf + r.r_offset + 4)) {
920920
u64 val = sym.get_addr(ctx) + r.r_addend;
921921

922-
if (use_rvc && rd != 0 && sign_extend(val, 6) == val) {
922+
if (use_rvc && rd != 0 && is_int(val, 6)) {
923923
// Replace AUIPC + LD with C.LI.
924924
remove(6);
925-
} else if (sign_extend(val, 12) == val) {
925+
} else if (is_int(val, 12)) {
926926
// Replace AUIPC + LD with ADDI.
927927
remove(4);
928928
}
@@ -934,13 +934,13 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
934934
u64 val = sym.get_addr(ctx) + r.r_addend;
935935
i64 rd = get_rd(buf + r.r_offset);
936936

937-
if (sign_extend(val, 12) == val) {
937+
if (is_int(val, 12)) {
938938
// We can replace `lui t0, %hi(foo)` and `add t0, t0, %lo(foo)`
939939
// instruction pair with `add t0, x0, %lo(foo)` if foo's bits
940940
// [32:11] are all one or all zero.
941941
remove(4);
942942
} else if (use_rvc && rd != 0 && rd != 2 &&
943-
sign_extend(val + 0x800, 18) == val + 0x800) {
943+
is_int(val + 0x800, 18)) {
944944
// If the upper 20 bits can actually be represented in 6 bits,
945945
// we can use C.LUI instead of LUI.
946946
remove(2);
@@ -969,7 +969,7 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
969969
//
970970
// Here, we remove `lui` and `add` if the offset is within ±2 KiB.
971971
if (i64 val = sym.get_addr(ctx) + r.r_addend - ctx.tp_addr;
972-
sign_extend(val, 12) == val)
972+
is_int(val, 12))
973973
remove(4);
974974
break;
975975
case R_RISCV_TLSDESC_HI20:
@@ -988,7 +988,7 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
988988
assert(r.r_type == R_RISCV_TLSDESC_ADD_LO12);
989989
if (!sym2.has_tlsdesc(ctx) && !sym2.has_gottp(ctx))
990990
if (i64 val = sym2.get_addr(ctx) + rel2.r_addend - ctx.tp_addr;
991-
sign_extend(val, 12) == val)
991+
is_int(val, 12))
992992
remove(4);
993993
}
994994
break;

src/arch-s390x.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
257257
u64 op = *(ub16 *)(loc - 2);
258258
u64 val = S + A - P;
259259
if ((op & 0xff0f) == 0xc408 && A == 2 && (val & 1) == 0 &&
260-
sign_extend(val, 33) == val) {
260+
is_int(val, 33)) {
261261
*(ub16 *)(loc - 2) = 0xc000 | (op & 0x00f0);
262262
*(ub32 *)loc = val >> 1;
263263
break;

0 commit comments

Comments
 (0)