@@ -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 ;
0 commit comments